Thu 14 Sep 2006
Export and Import MySQL via the command line
Posted by SteamSHIFT under Developing , Geek , Web DevelopmentIf you use MySQL on a remote server for web sites, you may want a nice way of backing them up or moving them to another server (perhaps keeping 2 servers in sync). To get you started try this:
Use an SSH client, like putty (if your database is on a remote server), to connect to your server and export the databse you want simply with:
mysqldump --user=username --password=1234 --databases your_database --opt --quote-names --allow-keywords --complete-insert | bzip2 -c > your_database.sql.bz2download the .bz2 file created, unzip it using 7-zip (if you dont have any other uncompress utility), and backup your database in mysql using:
mysql --user=username --password your_database < your_database.sql
Via Sephiroth.it
Also note, that if you have mysql installed locally, by including a host / port in the mysqldump arguements, you don’t need to run the command remotely (although you would probably want to get mysql to do the compression rather than piping it through the bzip command).
I’ll upload a full mysql mirror script in a few days. SteamSHIFT out.
Technorati Tags:
mysql, database, ssh
September 17th, 2006 at 9:52 am
I’m not exactly sure what you’re trying to do here. Are you dumping the database, trying to get a compressed copy to your local machine, and then importing it into a local installation of mysql? If so, cool. If you can get a bash shell (though I realize you’re in windows), you can do it all in one shot:
# ssh remote-host “mysqldump … | bzip2 -c” | mysql … your_database
If you really want to have a mysql mirror, why not do replication? Or is this a master database that you have next-to-no control over?
September 17th, 2006 at 9:55 am
Oops. That command line should be:
# ssh remote-host “mysqldump … | bzip2 -c” | bzip2 -c | mysql … your_database
I’m sure you’re aware, but gzip, though not quite as good at compressing text, is much faster and less processor-intensive.
If you need to make sure your backup is transaction-safe, you should also run –single-transaction, though if your tables are myisam, that’ll probably just lock up the entire database for the whole dump.
September 17th, 2006 at 9:56 am
Crap. One more time:
# ssh remote-host “mysqldump … | bzip2 -c” | bunzip2 -c | mysql … your_database
September 17th, 2006 at 10:00 am
thanks for your input Dean. I saw the quoted bits on Sephiroth.it and posted them here as much for my records as anything else.
Actally I use OS X (and Server) so use bash shell. But good call on the transaction safe stuff.
Replication sounds interesting - any pointers where to start looking?
October 27th, 2007 at 8:15 pm
im trying to copy my HUGE sql Db’s from one server to another, what can i do?
i have ssh on both servers, and there has to be a way, its the same version of all software on all machines, but whats happening is it comes back with ‘no sql query’ when i do the import
the file is huge and is full of slq queries
please help, im going ape over here
October 29th, 2007 at 5:37 pm
@steveometer: I’d love to help, but I really don’t consider myself anything of an expert. I have found that *NIX based machines seem to handle big DB inserts better than windows based machines. And that the command line is your friend in those situations. It may be that splitting the files into a few logical chunks is sufficient to make a difference.
February 4th, 2008 at 4:04 pm
Quite nice, needed this information to insert more than 500.000 instructions into a database using the commandline!
Thx!