jamieb
jamieb

Reputation: 10033

How do I perform "mysqldump" from within Python?

I would like to copy the contents of a MySQL database from one server to another using a third server. This could be done from the shell prompt using this:

mysqldump --host=hostname1 --user=username --password="mypwd" acme | mysql --host=hostname2 --user=username --password="mypwd" acme

However, how do I do this from within a Python script without using os.system or any of the other subprocess methods? I've read through the MySQLdb docs, but don't see a way to do a bulk export/import. Thank you!

Upvotes: 0

Views: 980

Answers (1)

Jimmy
Jimmy

Reputation: 1173

If you dont want to use mysqldump from the command line (using the os.system methods) you are kind of tied to get the data straight from MySQL and then put it to the other server. In that respect your question looks very similar to Get Insert Statement for existing row in MySQL

you can use a query to get the schema creation sql

SHOW CREATE TABLE MyTable;

And then you need to implement a script that just querys data and inserts it to the other server.

You could also look into third party applications that allows you to copy data from one database to another.

Upvotes: 2

Related Questions