Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash script which downloads a sql.gz file, extracts it and then import it, however before importing, I drop the existing DB, then import the new DB onto the server.

All is well and the script does what it is supposed to, only one thing :
I have this statement in my bash script :
mysqladmin -uroot -ppassword drop dbname
which then waits for user input to press y or n to continue.

Is there a way I can input Y so it always says yes and automatically completes the script. I am new ( to bash scripts.

Thanks Kind Regards

share|improve this question
up vote 2 down vote accepted

echo Y | mysqladmin ...


mysql -u root -e 'your sql commands' dbname

Please note that from security point of view you should not provide user credentials on command line. Instead you should create a preference file ~/.myrc or so and fill the necessary information there. That way your user info won't be revealed through process list.

share|improve this answer
Hi Janne, Thanks for the prompt reply, so basically I will put the following line in my bash script : mysql -u root -ppassword -e drop articlesdb Also, How can I put the username and passwords in the file, can you provide a step by step ? wasn't aware off this. – rihatum Nov 3 '10 at 11:09
Another One : I am using SCP to download the DB off a server (Linux based), I generated ssh-keys via the root user login and saved the rsa_pub key into the db host server's root user directory...this way SCP doesn't need a password to logon and download off the DB server. Now, if anyone on the client can logon as root via ssh onto the DB server ? is there a way that I can set SCP to login and download without a password but no one can logon to the DB host via SSH, telnet as root without a password ? – rihatum Nov 3 '10 at 11:09
Create a file called ~/.my.cnf and first put there line [mysql], then line user=yourdatabaseuser and then yet another line password=yourpassword. The MySQL client parses that file every time it starts up and you can provide there all the parameters listed in man mysql help pages. – Janne Pikkarainen Nov 3 '10 at 12:11
For scp you have several options. I would recommend you to create some other account than root, and then put some restricted shell as its shell: shells like scponly, lshell or rssh can restrict that user to scp/sftp only. Another option is to restrict the commands you can run with that specific ssh-key. That can be done by putting command="/path/to/thescriptyouwanttorun" to the beginning of your ssh key line in ~/.ssh/authorized_keys . – Janne Pikkarainen Nov 3 '10 at 12:16

You should use mysqladmin drop blah -f -uroot -pbleble. Look at the docs:

   --force, -f

       Do not ask for confirmation for the drop db_name command. With multiple commands, continue even if an error occurs.
share|improve this answer

Your Answer


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.