Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.
## 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.

share|improve this question
1  
An empty database password is not acceptable, ever, and and empty root password is completely unthinkable. –  Michael - sqlbot Apr 28 '13 at 14:02
    
It is also not a good idea (especially on a shared system) to have the user+password on the command line: anyone logged into the machine can potentially read them from the task list. See dev.mysql.com/doc/refman/5.6/en/password-security-user.html and dev.mysql.com/doc/refman/5.6/en/option-files.html for the preferred alternative. –  David Spillett Oct 21 '13 at 14:40

2 Answers 2

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}"
if
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.

share|improve this answer

Use --password instead of -p:

mysql "--username=$USER" "--password=$PASS" < backup.sql
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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