How do I connect to a database with a blank password using a shell script?

Posted on

Question :

## connect to mysql and source backup file ##

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

if [ "${PASS}" == "" ]
mysql -u${USER} ${PASSWORD} database_name < backup.sql

Give it a Try !!!


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

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....

Leave a Reply

Your email address will not be published. Required fields are marked *