当前位置:   article > 正文

python如何导出数据库数据,python导入导出mysql数据库数据

python导出mysql

# !/usr/bin/env python3

# -*- coding:utf-8 -*-

import pymysql

import time

from datetime import datetime

import os

import shutil

dbUser='db_order'

dbPasswd='db_order'

dbHost='118.89.139.104'

remoteDBUser='db_order'

remoteDBPasswd='db_order'

remoteDBHost='192.168.16.125'

dbCharset = 'utf8'

backupDir = '/home/developer/mysqlbackup'

backupDate = time.strftime("%Y%m%d")

backupDir = '/home/developer/mysqlbackup/' + backupDate

conn = None

cur = None

try:

if not os.path.exists(backupDir):

os.makedirs(backupDir)

print("backupDir exists:%s"%os.path.exists(backupDir))

conn = pymysql.connect(host=dbHost,port= 3306,user = dbUser,passwd=dbPasswd,db='db_order')

cur = conn.cursor()

cur.execute("show databases like 'db_%'")

databases = cur.fetchall()

print("databases:%s" %databases)

print('The database backup to start! %s' %time.strftime('%Y-%m-%d %H:%M:%S'))

for db in databases:

dbName = db[0]

print('databaseName:%s'%dbName)

fileName = '%s/%s_%s.sql' %(backupDir,dbName,backupDate)

print('fileName:%s'%fileName)

if os.path.exists(fileName):

os.remove(fileName)

os.system("mysqldump -h%s -u%s -p%s %s --default_character-set=%s > %s/%s_%s.sql" %(dbHost,dbUser,dbPasswd,dbName,dbCharset,backupDir,dbName,backupDate))

print('The database backup success! %s' %

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/967435
推荐阅读
相关标签
  

闽ICP备14008679号