Passing args to rails dbconsole - mysql

rails dbconsole detects the native DB client and loads it up with all the proper credentials and parameters passed. Is there a way to pass additional parameters to the DB client?
For instance, if you're using mysql, it will load up mysql client. I'd like to be able to pass arguments such as mysql -e 'SELECT NOW();' or whatever custom query I would like.

No, what you're asking is not possible. You can pass some options though, check
$ rails dbconsole -h
Usage: rails dbconsole [environment] [options]
-p, --include-password Automatically provide the password from database.yml
--mode [MODE] Automatically put the sqlite3 database in the specified mode (html, list, line, column).
--header
-h, --help Show this help message.
-e, --environment=name Specifies the environment to run this console under (test/development/production).
Default: development

What you're asking is not perfectly possible!
echo -e "SELECT NOW();\n" | rails dbconsole

Related

Setting envs or globals in bash script [NOT A duplicate]

I'm looking for a way (preferably cross-platform compatible) to set something globally accessible from a bash script.
My company is using a bash script to request access credentials to a mysql database. This returns username, password and db domain that I end up having to copy paste in my terminal to run and connect to our mysql db.
I thought i'd amend the script to set environment variables and make use of these in an alias with the credentials set in my bashrc but turns out you can't set environment variables in a bash script.
So i tried to set the mysql alias with the username password and domain pre-filled in that same script but same issue. Can't set an alias in a bash script.
I essentially want to be able to run the script that gives me the credentials and then not have to do manual copy pasting all over the place.
What I tried was (if it give more context):
#!/bin/bash
# Script gets the credentials
# Script now has username, password, endpoint variables
export MYSQL_USER=$username
export MYSQL_PASSWORD=$password
export MYSQL_ENDPOINT=$endpoint
# Script finishes
and in my bashrc:
alias mysqlenv="mysql -h $MYSQL_ENDPOINT -u $MYSQL_USER -p'$MYSQL_PASSWORD'"
I appreciate this is not working and that might not be the best solution so i'm open to other options.
PS: Forgot to mention the credentials expire every 24H which is why i want to smoothen the process
PS2: I can't source the script that gives me the credentials because it's not just exporting environment variables, it's taking params from the cli and getting me to log in to my company system on my browser, etc.
PS3: I know putting password for mysql on the command line is bad practice but this is a non-issue as that password is being printed there in the first place by the script that give me the credential (written by someone else in the company)
Since you can already parse the credentials, I'd use your awk code to output shell commands:
getMysqlCredentials() {
credential_script.sh | awk '
{parse the output}
END {
printf "local MYSQL_USER=\"%s\"\n", username
printf "local MYSQL_PASSWORD=\"%s\"\n", password
printf "local MYSQL_ENDPOINT=\"%s\"\n", endpoint
}
'
}
then, I'd have a wrapper function around mysql where you invoke that function and source the output
mysql() {
source <(getMysqlCredentials)
command mysql -h "$MYSQL_ENDPOINT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$#"
}

How can I use an environment variable in the MySQL console

I can connect to the console as root using an environment variable:
mysql -u root -p${MYSQL_ROOT_PASSWORD}
I’d like to be able to use a different environment variable in a statement
CREATE USER ${MYSQL_USER} IDENTIFIED BY ${MYSQL_USER_PASSWORD};
Which obviously doesn’t work (or why would I be asking). Is there any way I can use an environment variable in the console?
I don't think there's any way to access environment variables from MySQL queries. But you can construct the MySQL query and substitute variables in the shell.
mysql -e "CREATE USER '{$MYSQL_USER}' IDENTIFIED BY '${MYSQL_USER_PASSWORD}'"
The shell will substitute the variables into the string before it's passed to the mysql command. For longer queries you can use a here-doc:
mysql <<EOF
CREATE USER '{$MYSQL_USER}'
IDENTIFIED BY '${MYSQL_USER_PASSWORD}'
EOF

How to setup mysql (#Ubuntu-Server) auto connect to other host?

I am using Ubuntu-server. Can I setup / configure 'mysql' to auto-connect to another host? I mean if I type mysql on my terminal, it will connect automatically to specific host.
Thanks.
There are two parts for the answer.
First - how to make an alias, a word that if you type it, a specific command will execute (taken from here:
Create ~/.bash_aliases if not exists
touch ~/.bash_aliases
Open ~/.bash_aliases in a editor and append alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT"
or use command echo 'alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT"' >> ~/.bash_aliases
Save it
Use reload source ~/.bash_profile command to reload profile or reopen the terminal
Second part - the command you want to run mysql command, it will be something like:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
You can write a simple wrapper like this:
#!/bin/sh
/usr/bin/mysql --user $MYSQL_USER -p $MYSQL_PASSWORD --host $MYSQL_HOST $*
Where that should work almost identically to the default mysql command with a few tiny exceptions, like how LOAD DATA INFILE will read files only on the server, not your local machine.
You may need to do the same for mysqldump and other related commands like mysqladmin if you use those.
Be sure to specify the actual path to the mysql binary you want to run. I'm using /usr/bin/mysql here but it could be something else.

Can I enter password once for multiple mysql command line invocations, where the queries are not known upfront?

You can avoid re-entering mysql command line password by putting the queries into a file.
In my case, the later queries are not determined until after the first queries have finished.
This happens in a non-interactive script so running a mysql console is not an option.
Is there any notion of a session for mysql command line interactions? Or can I set it up to listen for commands on a local unix socket (the output is required to be returned)? Or something like that?
User #smcjones mentions using the .my.cnf file or mysql_config_editor. Those are good suggestions, I give my +1 vote to him.
Another solution is to put the credentials in any file of your choosing and then specify that file when you invoke MySQL tools:
mysql --defaults-extra-file=my_special.cnf ...other arguments...
And finally, just for completeness, you can use environment variables for some options, like host and password. But strangely, not the user. See http://dev.mysql.com/doc/refman/5.7/en/environment-variables.html
export MYSQL_HOST="mydbserver"
export MYSQL_PWD="Xyzzy"
mysql ...other arguments...
I don't really recommend using an environment variable for the password, since anyone who can run ps on your client host can see the environment variables for the mysql client process.
There are a few ways to handle this in MySQL.
Put password in hidden .my.cnf in the home directory of the user the script is running as.
[client]
user=USER
password=PASSWORD
Use mysql_config_editor
mysql_config_editor set --login-path=client --host=localhost
--user=localuser --password
When prompted to enter your password, enter it like you otherwise would.
IMO this is the worst option, but I'll add it for the sake of completeness.
You could always create a function wrapper for MySQL that appends your set password.
#! /bin/bash
local_mysql_do_file() {
mysql -u localuser -h localhost -pPASSWORD_NO_SPACE < $1
}
# usage
local_mysql_do_file file.sql

How to create scripts which will across windows/linux environments

We have a simple script to connect to the database (mysql -u root -p -h localhost). I don't want to create 2 scripts (.bat, .sh), how do I just create a single script to address this issue.
You might want to give MSYS a try: http://www.mingw.org/wiki/MSYS
you can use perl scripts. Perl is powerful scripting medium which is platform independent.
use can insert batch command in a variable in perl.
for eg
#!C:\Perl64\bin\perl
my $cmd = 'mysql - u root -p -h localhost';
system $cmd;
This should work fine.