Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Mysql database dump. I want to install it with Hudson via a Shell Script to an external Linux machine.

How can you take a Mysql script and run it inside a database on other machine? I know I can transfer files with sftp and use ssh when accessing another machines manually, but how to automate this all to a simple Shell Script?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
$ mysql -p$pass -u$user -h$otherhost $db < dumpfile

This assumes dumpfile is text from mysqldump, containing raw SQL statements.

It is important that there be no space between -p and the password. (I assume you want automatic login.) If the password does not immediately follow the -p, mysql interprets the option as meaning it should ask interactively for the password, and treats the separate password argument as the DB name.

For this to work, $user must have permission to log in, drop a table, create a table, and insert rows into that table, all as a remote user. MySQL keeps separate remote and local permissions for users, so this is an easy trap to fall into. Because allowing a remote user this much power is a security risk, you may want to use SSL (--ssl* options) with mysql. Otherwise, I don't know that I would allow a remote user to do such things.

share|improve this answer
    
+1: That serves quite well what I am trying to do. Thanks! –  mico Jul 12 '11 at 10:46
add comment

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.