Question :
## 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.
Answer :
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.
Use --password
instead of -p
:
mysql "--username=$USER" "--password=$PASS" < backup.sql
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....