python dump mysql_如何在Python中使用mysqldump和mysql复制数据库?

本文介绍了一位开发者尝试用Python脚本来复制MySQL数据库的问题。他们使用了mysqldump创建数据库备份,并尝试通过管道将备份导入新数据库,但遇到问题。尽管mysqldump部分工作正常,新数据库中并未出现数据。经过尝试不同方法,如使用Popen和 communicate(),或者使用 subprocess.call,问题仍未解决。最后,开发者发现直接在命令行中运行命令可以成功复制数据库,但在Python脚本中使用shell=True参数运行相同命令解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bd96500e110b49cbb3cd949968f18be7.png

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值