How to copy a database with mysqldump and mysql in Python?

PJvG

I am writing a simple Python script to copy a MySQL database. I am attempting to copy the database based on the following SO questions and their answers: "Copy/duplicate database without using mysqldump", "python subprocess and mysqldump" and "Python subprocess, mysqldump and pipes". However, my script does not work for some reason I cannot see as the tables and the data do not appear in my new database.

I can see from my output that the mysqldump works correctly (I see a "Dump completed on..." in my output), so I think that something is wrong with my pipeline.

Here is my script:

#!/usr/bin/env python

import pymysql
from subprocess import Popen, PIPE, STDOUT

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='mydb')
cur = conn.cursor()

print("Attempting to create new database...")
try:
    cur.execute("CREATE DATABASE mydb2")
    print("Creating new database")
except Exception:
    print("Database already exists")
print()

# close connection just to be sure
cur.close()
conn.close()

print("Trying to copy old database to new database...")

args1 = ["mysqldump", "-h", "localhost", "-P", "3306", "-u", "root", "-p", "mydb"]
args2 = ["mysql", "-h", "localhost", "-P", "3306", "-u", "root", "-p", "mydb2"]

p1 = Popen(args1, stdout=PIPE, stderr=STDOUT)
p2 = Popen(args1, stdin=p1.stdout, stdout=PIPE, stderr=STDOUT)
output = p2.communicate()

print("output:")
print(output)
print()

As you can see I took the copy database pipeline from this answer. And at first I had the error mysqldump: Couldn't find table: "|" just as in that other question. So now I use two subprocess.Popen calls as suggested, which solved that error message.

The output variable shows that a mysqldump is performed, but I see nothing being mentioned about the mysql command.

I have tried to use p2.wait() and p1.wait() instead of p2.communicate() as suggested in one answer, but that just makes my Python script become unresponsive.

I have also tried the following:

output1 = p1.communicate()
output2 = p2.communicate()

But then both output1 and output2 show the same mysqldump output. So that was just a silly thing to do I guess..

I have also tried to use subprocess.call instead of subprocess.Popen, but that also makes my script become unresponsive.

Also including shell=True in either Popen or call also results in the script being just unresponsive.

However, it does work to type in the command in the command prompt (I use Windows 8.1) as follows:

mysqldump -h localhost -P 3306 -u root -p mydb | mysql -h localhost -P 3306 -u root -p mydb2

It copies my small test database in less than three seconds.

I wish I could also get it to work in Python.

dreyescat

I don't know the degree of pure Python you want to use for the copy, but you can just delegate the entire pipe operation to the shell.

subprocess.Popen('mysqldump -h localhost -P 3306 -u -root mydb | mysql -h localhost -P 3306 -u root mydb2', shell=True)

This should work the same way it works when you run it on the shell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysqldump from mysql to mariadb - database size diffrence

From Dev

Can I dump database with mysql (not mysqldump)?

From Dev

How to copy MySQL database with java jar file

From Dev

How to make mysqldump work well for a WordPress database?

From Dev

XAMPP MySQL Database Copy

From Dev

Backup mysql from remote database with mysqldump process error

From Dev

How can in compress the mysqldump with Python Popen

From Dev

get the copy of database from mysql

From Dev

Creating an independent copy of a MySQL database

From Dev

How to export WebSQL database to .sql file? (like mysqldump)

From Dev

mysqldump will not export an individual database

From Dev

creating mysqldump to backup database

From Dev

How to use python 3.5.1 with a MySQL database

From Dev

How to insert Records of data into MySQL database with python?

From Dev

how to insert in mysql database using python

From Dev

How to query MySQL database using API in python?

From Dev

Exporting MySQL database from within a command prompt mysql tool. Without mysqldump

From Dev

How can I dump mysql table without using mysqldump?

From Dev

How can I dump mysql table without using mysqldump?

From Dev

How to make a copy of a database package

From Dev

How to make a physical copy of database?

From Dev

How to copy data from Database to another Database?

From Dev

Copy changes from one mysql database to other

From Dev

mysql - copy from one database to another

From Dev

Copy fields from one database to another in MySQL

From Java

mysqldump & gzip commands to properly create a compressed file of a MySQL database using crontab

From Dev

Python 3.4.0 with MySQL database

From Dev

Mock a MySQL database in Python

From Dev

Create a MySQL database in python

Related Related

  1. 1

    Mysqldump from mysql to mariadb - database size diffrence

  2. 2

    Can I dump database with mysql (not mysqldump)?

  3. 3

    How to copy MySQL database with java jar file

  4. 4

    How to make mysqldump work well for a WordPress database?

  5. 5

    XAMPP MySQL Database Copy

  6. 6

    Backup mysql from remote database with mysqldump process error

  7. 7

    How can in compress the mysqldump with Python Popen

  8. 8

    get the copy of database from mysql

  9. 9

    Creating an independent copy of a MySQL database

  10. 10

    How to export WebSQL database to .sql file? (like mysqldump)

  11. 11

    mysqldump will not export an individual database

  12. 12

    creating mysqldump to backup database

  13. 13

    How to use python 3.5.1 with a MySQL database

  14. 14

    How to insert Records of data into MySQL database with python?

  15. 15

    how to insert in mysql database using python

  16. 16

    How to query MySQL database using API in python?

  17. 17

    Exporting MySQL database from within a command prompt mysql tool. Without mysqldump

  18. 18

    How can I dump mysql table without using mysqldump?

  19. 19

    How can I dump mysql table without using mysqldump?

  20. 20

    How to make a copy of a database package

  21. 21

    How to make a physical copy of database?

  22. 22

    How to copy data from Database to another Database?

  23. 23

    Copy changes from one mysql database to other

  24. 24

    mysql - copy from one database to another

  25. 25

    Copy fields from one database to another in MySQL

  26. 26

    mysqldump & gzip commands to properly create a compressed file of a MySQL database using crontab

  27. 27

    Python 3.4.0 with MySQL database

  28. 28

    Mock a MySQL database in Python

  29. 29

    Create a MySQL database in python

HotTag

Archive