0
## connect to mysql and source backup file ##
USER="root"
PASS=""

mysql -u$USER -p$PASS database_name < backup.sql

Above is my shell script which I used to source database but still its asking for password. I just want to know how can I source database in case of blank password. If root password is not blank than it works fine.

3

If root has no password you could just do this:

mysql -u$USER database_name < backup.sql

If you want to code so that root can be given a password later, do this in the script

USER="root"
PASS=""
if [ "${PASS}" == "" ]
then
    PASSWORD=""
else
    PASSWORD="-p${PASS}"
fi
mysql -u${USER} ${PASSWORD} database_name < backup.sql

Give it a Try !!!

CAVEAT

If is usually not a good idea to have root without a password. Please give it one.

  • I edited the code, please check. – ypercubeᵀᴹ Dec 20 '17 at 10:42
2

Use --password instead of -p:

mysql "--username=$USER" "--password=$PASS" < backup.sql
  • this is actually the best answer. using --password makes it work even with empty passwords, which is the desired behavior. – Vitor M Aug 30 '17 at 14:30
0

The best way to specify server connection information is with your .mylogin.cnf file. Not only is this file encrypted, but any logging of the utility execution does not expose the connection information. Thus, no user names, passwords, ports, etc. are visible in the log. This is the preferred method for using MySQL Utilities to connect to servers.

Taken from https://dev.mysql.com/doc/mysql-utilities/1.5/en/mysql-utils-intro-connspec-mylogin.cnf.html

This removes the need from specifying your password in the command line and secure.

mysql_config_editor set --login-path=instance_13001 --host=localhost --user=root --port=13001 --password
Enter password: <Password is prompted to be inserted in a more secure way>


mysql --login-path=KeyPath....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.