PHP前端开发

Python导入数据库时,如何使用`mysqldump`命令正确恢复数据库?

百变鹏仔 5天前 #Python
文章标签 数据库

使用python导入数据库时遇到的问题

在代码中,使用mysqldump命令恢复数据库时,符号“

dumpcmd = "mysql -uroot -p123456 test1 < " + todaybackuppath + "/test2.sql"

此外,在恢复数据库之前,需要先关闭数据库,以确保数据一致性。具体修改如下:

import osimport timeBACKUP_PATH = r'C:/Users/Administrator/Desktop/English_learning_assistant/bakcup/'DATETIME = time.strftime('%Y-%m-%d')TODAYBACKUPPATH = BACKUP_PATH + DATETIMEif not os.path.exists(TODAYBACKUPPATH):    os.makedirs(TODAYBACKUPPATH)dumpcmd = "mysqldump -uroot -p123456 test1 > " + TODAYBACKUPPATH + "/test2.sql"# print("数据已备份到" + BACKUP_PATH)os.system(dumpcmd)# 关闭数据库os.system("mysqladmin -uroot -p123456 shutdown")# 导入数据库dumpcmd = "mysql -uroot -p123456 test1 < " + TODAYBACKUPPATH + "/test2.sql"os.system(dumpcmd)print(TODAYBACKUPPATH)print(dumpcmd)print("数据已恢复")