0

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!

jamieb
  • 9,847
  • 14
  • 48
  • 63
  • That seems like an arbitrary restriction. What's the logic behind it? – robert Jul 28 '12 at 00:29
  • Behind not being able to shell out? Because I can't guarantee that the MySQL client tools will be installed on the machine that is used to do the transfer. – jamieb Jul 28 '12 at 00:55
  • But you can guarantee that mysql-python will be installed? That will at least require the client libs. – jordanm Jul 28 '12 at 02:11

1 Answers1

2

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.

Community
  • 1
  • 1
Jimmy
  • 1,115
  • 1
  • 9
  • 21