Python實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的腳本分享
一: 需求
線上數(shù)據(jù)全量同步到測試環(huán)境。
二:腳本
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
import datetime
import pymysql
import os
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
# 需要同步的表
table_names = ['table_name_1', 'table_name_2']
source_db_config = {
'db_host': 'XXXXX',
'db_user': 'XXXX',
'db_pwd': 'XXXX',
'db': 'XCXXXCX',
'db_port': 3306,
}
out_db_config = {
'db_host': 'XXXX',
'db_user': 'XXXX',
'db_pwd': 'XXXX',
'db': 'XXXXX',
'db_port': 3306,
}
def get_datas(table_name):
"""源數(shù)據(jù)庫,獲取全量數(shù)據(jù)"""
db = pymysql.connect(source_db_config['db_host'], source_db_config['db_user'], source_db_config['db_pwd'],
source_db_config['db'], source_db_config['dp_port'], charset='utf8')
cursor = db.cursor(pymysql.cursors.DictCursor)
readsql = '''select * from {}'''.format(table_name)
cursor.execute(readsql)
results = cursor.fetchall()
for data in results:
yield data
cursor.close()
db.close()
def format_data(data):
"""數(shù)據(jù)格式化"""
for k, v in data.items():
if type(v) == datetime.datetime:
data[k] = "'{}'".format(v.strftime('%Y-%m-%d %H:%M:%S'))
elif type(v) == type(v) == datetime.date:
data[k] = "'{}'".format(v.strftime('%Y-%m-%d'))
elif type(v) == unicode:
data[k] = "'{}'".format(v.encode('utf-8'))
return data
def out_put_data(table_name):
write = pymysql.connect(out_db_config['db_host'], out_db_config['db_user'], out_db_config['db_pwd'],
out_db_config['db'], out_db_config['dp_port'], charset='utf8')
for data in get_datas(table_name):
write_cursor = write.cursor()
data = format_data(data)
sql = ','.join(['{}=%s'.format(item) for item in data.keys()])
temp = sql % (tuple(data.values()))
write_sql = '''insert into %s set %s on duplicate key update %s''' % (table_name, temp, temp)
try:
write_cursor.execute(write_sql)
write.commit()
except Exception as e:
print("insert error, err_msg is {}".format(e))
write.rollback()
finally:
write_cursor.close()
write.close()
if __name__ == "__main__":
start_time = time.time()
for table_name in table_names:
out_put_data(table_name=table_name)
end_time = time.time()
cost_time = end_time - start_time
print("cost_time is {}".format(cost_time))到此這篇關(guān)于Python實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的腳本分享的文章就介紹到這了,更多相關(guān)Python Mysql數(shù)據(jù)同步內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
selenium+python自動化測試環(huán)境搭建步驟
在本文中小編給大家分享了關(guān)于selenium+python自動化測試環(huán)境搭建的相關(guān)步驟以及知識點(diǎn)內(nèi)容,需要的朋友們參考學(xué)習(xí)下。2019-06-06
分享一下Python數(shù)據(jù)分析常用的8款工具
Python是數(shù)據(jù)處理常用工具,可以處理數(shù)量級從幾K至幾T不等的數(shù)據(jù),具有較高的開發(fā)效率和可維護(hù)性,還具有較強(qiáng)的通用性和跨平臺性,這里就為大家分享幾個不錯的數(shù)據(jù)分析工具,需要的朋友可以參考下2018-04-04
一篇文章帶你了解python標(biāo)準(zhǔn)庫--os模塊
在本篇內(nèi)容里小編給大家整理的是關(guān)于Python中os模塊及用法相關(guān)知識點(diǎn),有興趣的朋友們可以學(xué)習(xí)下,希望能給你帶來幫助2021-08-08
在樹莓派2或樹莓派B+上安裝Python和OpenCV的教程
這篇文章主要介紹了在樹莓派2或樹莓派B+上安裝Python和OpenCV的教程,主要基于GTK庫,并以Python2.7和OpenCV 2.4.X版本的安裝作為示例,需要的朋友可以參考下2015-03-03
Python借助with語句實(shí)現(xiàn)代碼段只執(zhí)行有限次
這篇文章主要介紹了Python借助with語句實(shí)現(xiàn)代碼段只執(zhí)行有限次,首先要定義一個能夠在with語句中使用的類實(shí)現(xiàn)enter和exit,下文詳細(xì)介紹需要的小伙伴可以參考一下2022-03-03
pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式
這篇文章主要介紹了pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

