2

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 (v.new) to bash scripts.

Thanks Kind Regards

Mutahir
  • 2,357
  • 2
  • 32
  • 42

2 Answers2

6

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.
Falcon Momot
  • 25,244
  • 15
  • 63
  • 92
GM_ARG
  • 61
  • 1
  • 1
2

echo Y | mysqladmin ...

Or

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.

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81
  • 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. – Mutahir Nov 03 '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 ? – Mutahir Nov 03 '10 at 11:09
  • 1
    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 03 '10 at 12:11
  • 1
    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 03 '10 at 12:16