I have a shell script which calls the mysql command with one parameter from external file, it looks like this (also I saw this example in other resources):
mysql --user=root --password=`cat /root/.mysql`
Bit it not working:
Failed to connect to MySQL server: Access denied for user 'root'#'localhost' (using password: YES).
I tried different quotes without success. How to pass it?
UPDATE 1:
Found that I can pass password without space symbol. The problem in this, my root pass contains spaces.
Store your password in a protected mysql cnf file:
install -m 700 -d /srv/secrets/
install -m 600 /dev/null /srv/secrets/root#localhost.cnf
editor /srv/secrets/root#localhost.cnf
Store the password in the client.password ini property
[client]
password="password"
Include this file as the first argument in your mysql command:
mysql \
--defaults-extra-file=/srv/secrets/root#localhost.cnf \
--user=root \
--host=localhost \
--no-auto-rehash
Use mysql_config_editor which is installed with the mysql client
mysql_config_editor set --login-path=dev --user=dbuser --host=localhost -p
Enter the password and then you can log in like this
mysql --login-path=dev
You should use the mysql_config_editor for this.
$ mysql_config_editor set \
--login-path=name_of_connection \
--host=server.example.com \
--user=login_as_user \
--password
this will prompt for a password, and save to a .mylogin.cnf file in your homedir.
mysql --login-path=name_of_connection dbname will connect to dbname on server.example.com as login_as_user
If --login-path is used with other options, ex. --silent, --login-path has to be the first argument/option
Finally this line working:
mysql --user=root --password="$(cat /root/.mysql)"
or:
mysql --user=root --password="$(< /root/.mysql)"
Root password must be without quotes: bla bla bla
If your password not contains spaces you can use:
mysql --user=root --password=`cat /root/.mysql`
I am not sure if that is possible, but you could definitely use configuration
file.
Try:
if [ $MYSQL_PASS ]
then
mysql -u "$MYSQL_ROOT" -p "$MYSQL_PASS" -e "SHOW DATABASES"
else
mysql -u "$MYSQL_ROOT" -e "SHOW DATABASES"
fi
Related
I have password stored in a variable $db_pwd and I want to pass it to mysql_config_editor in a shell script. I can not use config file or db_pwd environment variable.
I am doing this
mysql_config_editor set --login-path=local --host=localhost --user=username --password
(https://stackoverflow.com/a/20854048/6487831) .
What it does is ask for password "Enter Password", but I wish to supply the password using variable.
I tried this :
mysql_config_editor set --login-path=local --host=localhost --user=username --password $db_pwd
and
mysql_config_editor set --login-path=local --host=localhost --user=username --password | echo $db_pwd
and
echo "$db_pwd" | mysql_config_editor set --login-path=local --host=localhost --user=username --password
and
expect. But this leads to error in case there are warnings like "This path already exists, rewrite (y/n).
options file, but they still give me variable not found error even when I am using it as the first argument. Is options file compatible with mysql config editor?
Any way to do this? Or should I revert to using mysql instead of mysql_config_editor?
I found the other suggested answer did not work when there was no TTY. So I used this bash script that works in places like Terraform/ssh that doesn't have a terminal:
#!/bin/bash
if [ $# -ne 4 ]; then
echo "Incorrect number of input arguments: $0 $*"
echo "Usage: $0 <login> <host> <username> <password>"
echo "Example: $0 test 10.1.2.3 myuser mypassword"
exit 1
fi
login=$1
host=$2
user=$3
pass=$4
unbuffer expect -c "
spawn mysql_config_editor set --login-path=$login --host=$host --user=$user --password
expect -nocase \"Enter password:\" {send \"$pass\r\"; interact}
"
Test it:
./mysql_config.sh login 10.1.2.3 myuser mypass < /dev/null
The --password argument is designed to explicitly avoid passing a password on the command line, as this is considered bad security.
That being said, you could try to feed the password to mysql_config_editor anyway:
echo "$db_pwd" | mysql_config_editor set --login-path=local --host=localhost --user=username --password
(This may not work if mysql_config_editor insists on getting input from the current terminal instead of standard in; if that is the case, you don't have a way to provide the password from a variable).
As the answer you linked to states, you can use mysql directly to supply the password. Using mysql_config_editor is meant for storing the password in .mylogin.cnf in an encrypted form (i.e. you supply the password once from the terminal, it is then encrypted and saved in the config file, and mysql can use it from there).
Reference: https://dev.mysql.com/doc/refman/5.7/en/mysql-config-editor.html
Update: You may be able to trick mysql_config_editor into thinking it is reading from an interactive terminal, by using the unbuffer utility:
echo "$db_pwd" | unbuffer -p mysql_config_editor set --login-path=local --host=localhost --user=username --password
Ran into this problem as well.
My solution was to run the command once, and supply the password on prompt
mysql_config_editor set --login-path=local --host=localhost --user=username --password
This generates the file .mylogin.cnf in users home directory.
Just copying this file (can be done from a bash script) to the user you want to give access using the --login-path option does the trick.
As I understand .mylogin.cnf is just an obfuscated way of storing a username and password for that particular --login-path
In a simple script like this one:
set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then
c1="CREATE DATABASE $db"
c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'#'localhost' IDENTIFIED BY '$password'"
c3="FLUSH PRIVILEGES"
mysql -u root -p -e "$c1; $c2; $c3"
else
echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi
I am asked each time, bash sees mysql command, for my root credentials.
Is there a way to avoid that, so that once entered the root password, all
additional mysql commands execute seamlessly?
Try looking into adding password to ~/.my.cnf
[client]
user = root
password = XXXXXXXX
Check out :
How to execute a MySQL command from a shell script?
Specifying the --password argument
mysql -u root --password=my_mysql_pass db_name
Safer using a bash variable
mysql -u root --password=$MYSQL_PASS db_name
So I want to pipe my netstat output into a mysql database
The plan is to make it a continuous crontab event. That way I can store that data and use it elsewhere easily.
I figured before I could figure that part out though I needed to figure out how to run a SQL Command from terminal.
It seems pretty straight forward
sudo mysql -u username -pMYPassword -e "SQL COMMAND"
This however doesn't work...
When I run this it prints the MYSQL Help
When I run this though
sudo mysql -u username -p -e "SQL COMMAND"
everything works perfect, it just prompts me for a password
Now I don't know if it makes a difference or not but my DB password does have exclamation points in it. It is also over 15 chars long
Could either of these be an issue?
mysql -u user -p -e 'SQL Query' database
Where,
-u : Specify mysql database user name
-p : Prompt for password
-e : Execute sql query
database : Specify database name
So the option -p is a PROMPT, the password need not to be linked, but to be separated from it:
sudo mysql -u username -p'PassWord' -e 'SELECT COUNT(*) FROM my_table'
EDIT: If your real password contains special characters, you need to escape them.
Maybe try : sudo mysql -u username -p'MYPassword' -e "SQL COMMAND"
Or : sudo mysql -u username --password=MyPassword -e "SQL COMMAND"
I am trying to run MySQL query on remote machine with this command:
ssh user#192.168.2.26 "mysql -uroot -proot -e \"use test";""
I am not able to use that database.
Please suggest a working command.
Try this:
mysql -h host -u root -proot -e "show databases;";
Try this:
ssh root#host "mysql database -e 'query to run on table_name; more queries to run;'"
Same can be done with user#host if that user has permission to execute SQL queries let alone launch mysql in general. Using -e is the same as --execute, which will run whatever you put within the trailing quotes (single or double) and quit. The standard output format would be the same as you would see using --batch.
MySql seems to have a special command line syntax which includes the database.
mysql -u user -p -e 'SQL Query' database
This documentation is rather old but I got it to work
http://www.cyberciti.biz/faq/run-sql-query-directly-on-the-command-line/
Final working command with ssh:
ssh user#host "mysql -u user -e 'show tables;' databasename"
This ended up working for me in a bash script:
query='USE [database]; SELECT ...'
mysql='mysql -u [username] -p[password] -e '"'""$query""'"
ssh [username]#[server] -t "$mysql"
If you want to make it more safe then add a prompt for the password instead of storing it somewhere potentially unsafe.
This worked for me after a few tests (basically same answer as #King-Wzrd):
ssh -t kom "mysql -uroot -p -e 'show databases;'"
ssh -t kom "mysql -uroot -p < /home/ling/websites/jin_test/.deploy/tmp.sql"
The "trick" was the quotes around the command.
The -t option allows for prompting password interactively via the remote shell.
The kom here is just a ssh config identifier defined in my ~/.ssh/config file (see more here: https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/).
Running this from my Host environment against MySQL within my Homestead VM produced a nice result... although I did have to set the root password from within the VM first in order for it to work.
ssh vagrant#192.168.10.10 mysql -h localhost -u root -p -e "'SELECT * FROM user;' mysql";
Question Rewritten:
HOMEDIR="ftpuser"
REMOTEIP="1.1.1.1"
MYSQLPASS="password"
Q1="DROP USER "$HOMEDIR"_shop;"
Q2="DROP DATABASE "$HOMEDIR"_shop;"
Q3="CREATE DATABASE IF NOT EXISTS "$HOMEDIR"_shop;"
Q4="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q5="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'anotherip' IDENTIFIED BY '$MYSQLPASS';"
# Need to grant permissions from another server as well
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
echo $SQL
echo " "
ssh -p 8899 root#$REMOTEIP "mysql -u root -p "$SQL""
I then run:
/root/testing/migratesite.sh
And get:
bash: DROP: command not found
bash: CREATE: command not found
bash: GRANT: command not found
bash: GRANT: command not found
bash: FLUSH: command not found
What am I missing?
You are missing quotes and a proper mysql client command line:
ssh -p 8899 root#$REMOTEIP "mysql -u root -p -e \"$SQL\""
You need to escape the quotes around the $SQL variable so they get passed to the remote shell, else they get interpreted by the local shell (that's why you get DROP: command not found, the semi colon is interpreted by the shell.) Also, to have the mysql client to execute a command you have to pass the -e command line option.
Did you try this:
ssh -p 8899 root#$REMOTEIP "echo \"$SQL\" | mysql -u root --password=$SQL_PASS"