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)文章
Pytorch實(shí)現(xiàn)將label變成one hot編碼的兩種方式
這篇文章主要介紹了Pytorch實(shí)現(xiàn)將label變成one hot編碼的兩種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python2中文處理紀(jì)要的實(shí)現(xiàn)方法
本篇文章主要介紹了Python2中文處理紀(jì)要的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
解決tensorflow訓(xùn)練時(shí)內(nèi)存持續(xù)增加并占滿的問題
今天小編就為大家分享一篇解決tensorflow訓(xùn)練時(shí)內(nèi)存持續(xù)增加并占滿的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python 圖像處理之顏色遷移(reinhard VS welsh)
這篇文章主要介紹了分別利用reinhard算法和welsh算法實(shí)現(xiàn)圖像的顏色遷移,并對二者算法的效果進(jìn)行了對比,感興趣的小伙伴可以了解一下2021-12-12
Python實(shí)現(xiàn)類似jQuery使用中的鏈?zhǔn)秸{(diào)用的示例
chained calls鏈?zhǔn)秸{(diào)用其實(shí)多是指一種方法鏈的程序?qū)懛?這里我們來看一下Python實(shí)現(xiàn)類似jQuery使用中的鏈?zhǔn)秸{(diào)用的示例,首先說明一下什么是鏈?zhǔn)秸{(diào)用:2016-06-06
Selenium結(jié)合BeautifulSoup4編寫簡單的python爬蟲
這篇文章主要介紹了Selenium結(jié)合BeautifulSoup4編寫簡單的python爬蟲,幫助大家更好的理解和學(xué)習(xí)python 爬蟲的相關(guān)知識(shí),感興趣的朋友可以了解下2020-11-11
django連接mysql數(shù)據(jù)庫及建表操作實(shí)例詳解
這篇文章主要介紹了django連接mysql數(shù)據(jù)庫及建表操作,結(jié)合實(shí)例形式詳細(xì)分析了Django框架連接mysql數(shù)據(jù)庫、創(chuàng)建與查詢數(shù)據(jù)表相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12

