python實現(xiàn)數(shù)據(jù)庫跨服務(wù)器遷移
更新時間:2018年04月12日 17:03:35 作者:Stupid_Sparrow
這篇文章主要為大家詳細介紹了Python實現(xiàn)數(shù)據(jù)庫之間的數(shù)據(jù)遷移,具有一定的參考價值,感興趣的小伙伴們可以參考一下
基于Python2.7的版本環(huán)境,Python實現(xiàn)的數(shù)據(jù)庫跨服務(wù)器(跨庫)遷移, 每以5000條一查詢一提交,代碼中可以自行更改每次查詢提交數(shù)目.
# -*- coding: utf-8 -*- import MySQLdb import time import warnings warnings.filterwarnings("ignore") class ConnectMysql(object): def __init__(self): # 這里設(shè)置分頁查詢, 每頁查詢多少數(shù)據(jù) self.page_size = 5000 def getTable(self): conn = MySQLdb.connect( host="***.***.**.**", user="****", passwd="*************", db='****', charset='utf8' ) conn_local = MySQLdb.connect( host="********************************", user="**********", passwd="********", db='*******', charset='utf8' ) cur = conn.cursor() cur_local = conn_local.cursor() cur.execute('show tables') tables = cur.fetchall() for table in tables: print str(table[0]).lower() # 需要遷移的數(shù)據(jù)庫查詢表的列數(shù) cur.execute("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE table_schema='china' AND table_name='" + table[0] + "'") table_col_count = cur.fetchone() # print table_col_count[0] # 需要遷移的數(shù)據(jù)庫查詢表的結(jié)構(gòu) cur.execute('show create table ' + table[0]) result = cur.fetchall() create_sql = result[0][1] # 查詢需要遷移的數(shù)據(jù)庫表的數(shù)據(jù)條數(shù) cur.execute('select count(*) from ' + table[0]) total = cur.fetchone() page = total[0] / self.page_size page1 = total[0] % self.page_size if page1 != 0: page = page + 1 # 阿里云數(shù)據(jù)庫創(chuàng)建表 cur_local.execute("SELECT table_name FROM information_schema.`TABLES` WHERE table_schema='user' AND table_name='" + str(table[0]).lower() + "'") table_name = cur_local.fetchone() if table_name is None: cur_local.execute(create_sql) for p in range(0, page): while True: try: print '開始', table[0], '的第', p + 1, '頁查詢' if p == 0: limit_param = ' limit ' + str(p * self.page_size) + ',' + str(self.page_size) else: limit_param = ' limit ' + str(p * self.page_size + 1) + ',' + str(self.page_size) cur.execute('select * from ' + table[0] + limit_param) inserts = cur.fetchall() print '查詢成功' param = '' for i in range(0, table_col_count[0]): param = param + '%s,' print '開始插入' cur_local.executemany('replace into ' + table[0] + ' values (' + param[0:-1] + ')', inserts) print table[0], '的第', p + 1, '頁, 插入完成, 還有', page - p - 1, '頁, 任重而道遠' conn_local.commit() break except Exception as e: print e time.sleep(60) cur = conn.cursor() cur_local = conn_local.cursor() print table[0], ' 插入完成' print '\n \n ======================================================================== \n\n' cur_local.close() conn_local.close() cur.close() conn.close() if __name__ == '__main__': conn_mysql = ConnectMysql() conn_mysql.getTable()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 使用python和Django完成博客數(shù)據(jù)庫的遷移方法
- Python依賴包遷移到斷網(wǎng)環(huán)境操作
- 如何把外網(wǎng)python虛擬環(huán)境遷移到內(nèi)網(wǎng)
- 如何將你的應(yīng)用遷移到Python3的三個步驟
- 詳解Python3遷移接口變化采坑記
- python 動態(tài)遷移solr數(shù)據(jù)過程解析
- python django生成遷移文件的實例
- Python依賴包整體遷移方法詳解
- pycharm使用正則表達式批量添加print括號完美從python2遷移到python3
- python虛擬環(huán)境遷移方法
- 用python寫個博客遷移工具
相關(guān)文章
python中使用百度音樂搜索的api下載指定歌曲的lrc歌詞
這篇文章主要介紹了python中使用百度音樂搜索的api下載指定歌曲的lrc歌詞,同時也分析出了歌曲的下載地址,需要的朋友可以參考下2014-07-07postman發(fā)送文件請求并以python服務(wù)接收方式
這篇文章主要介紹了postman發(fā)送文件請求并以python服務(wù)接收方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07python之no module named xxxx以及虛擬環(huán)境配置過程
在Python開發(fā)過程中,經(jīng)常會遇到環(huán)境配置和包管理的問題,主要原因包括未安裝所需包或使用虛擬環(huán)境導(dǎo)致的,通過pip install命令安裝缺失的包是解決問題的一種方式,此外,使用虛擬環(huán)境,例如PyCharm支持的Virtualenv,可以為每個項目創(chuàng)建獨立的運行環(huán)境2024-10-10python使用smtplib模塊通過gmail實現(xiàn)郵件發(fā)送的方法
這篇文章主要介紹了python使用smtplib模塊通過gmail實現(xiàn)郵件發(fā)送的方法,涉及Python使用smtplib模塊發(fā)送郵件的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-05-05