HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
scriptconnectwithblankpassworddatabaseshellusinghow

Problem

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

Solution

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.

Code Snippets

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

Context

StackExchange Database Administrators Q#41070, answer score: 6

Revisions (0)

No revisions yet.