最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python制作mysql數(shù)據(jù)遷移腳本

 更新時間:2019年01月01日 09:37:56   作者:浮生鳳年  
這篇文章主要為大家詳細介紹的是使用python寫的mysql數(shù)據(jù)遷移的腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下

用python寫了個數(shù)據(jù)遷移腳本,主要是利用從庫將大的靜態(tài)表導出表空間,載導入到目標實例中。

#!/usr/bin/env python3
#-*- coding:utf8 -*-
#author:zhanbin.liu
#!!!!!DB必須同版本
#python3環(huán)境  pip3 install pymysql paramiko

import os
#from pathlib import Path
import sys
import pymysql
import paramiko

#每次只能遷移一個DB下的表,到指定DB
#GRANT SELECT, CREATE, RELOAD, ALTER, LOCK TABLES ON *.* TO 'data_migration'@'192.168.%' IDENTIFIED BY 'data_migration@123';
tables='sqlauto_cluster,sqlauto_user'    #以,分割的字符串,如a,b,c
tableList = tables.split(',')
sourceIp = '192.168.1.101'
sourceDataBase = '/data/mysql/3306/data'
sourceDbName = 'inception_web'
sourceDataDir = os.path.join(sourceDataBase,sourceDbName)
desIp = '192.168.1.102'
desDataBase = '/data/mysql/3306/data'
desDbName = 'inception_web'
desDataDir = os.path.join(desDataBase,desDbName)

# for table in tableList:
#   desFile = Path("%s/%s.ibd" %(desDataDir,table))
#   print(desFile)
#   if desFile.is_file():
#     print("ok")
#   else:
#     print("no")

comUser = 'data_migration'
comPwd = 'data_migration@123'
comPort = 3306

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

def table_judge():
  print("table_judge")
  sourceTableExist = pymysql.connect(sourceIp,comUser,comPwd,sourceDbName,comPort,charset='utf8')
  desTableExist = pymysql.connect(desIp,comUser,comPwd,desDbName,comPort,charset='utf8')
  sourceTables = []
  desTables = []
  cursor_source = sourceTableExist.cursor()
  cursor_des = desTableExist.cursor()

  for table in tableList:
    #print(table)
    cursor_source.execute("select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='%s' and TABLE_NAME='%s';" % (sourceDbName,table))
    sourceTable_tmp = cursor_source.fetchall()
    cursor_des.execute("select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='%s' and TABLE_NAME='%s';" % (desDbName,table))
    desTable_tmp = cursor_des.fetchall()
    #print(desTable_tmp)
    if sourceTable_tmp is ():
      sourceTables.append(table)
    if desTable_tmp is not ():
      desTables.append(desTable_tmp[0][0])
  sourceTableExist.close()
  desTableExist.close()

  s=d=0
  if sourceTables != []:
    print('遷移源不存在將要遷移的表:',sourceIp,sourceDbName, sourceTables,' 請檢查')
    s=1
  if desTables != []:
    print('目標庫存在將要遷移的表:',desIp,desDbName,desTables,' 請移除')
    d=1
  if s == 1 or d == 1:
    sys.exit()

def data_sync():
  print('data_sync')
  db_source = pymysql.connect(sourceIp,comUser,comPwd,sourceDbName,comPort,charset='utf8')
  db_des = pymysql.connect(desIp,comUser,comPwd,desDbName,comPort,charset='utf8')
  cursor_db_source = db_source.cursor()
  cursor_db_des = db_des.cursor()

  for table in tableList:
    print("正在同步表:",table)
    cursor_db_source.execute("show create table %s;" % (table))
    createTableSQL = cursor_db_source.fetchall()[0][1]
    print(createTableSQL)
    try:
      cursor_db_des.execute(createTableSQL)
    except Exception as error:
      print(error)
    cursor_db_source.execute("flush table %s with read lock;" % (table))
    cursor_db_des.execute("alter table %s discard tablespace;" % (table))

    client.connect(sourceIp, 22, 'root')
    stdin1, stdout1, stderr1 = client.exec_command("scp %s %s:%s " % (sourceDataDir+"/"+table+".ibd", desIp, desDataDir))
    stdin2, stdout2, stderr2 = client.exec_command("scp %s %s:%s " % (sourceDataDir+"/"+table+".cfg", desIp, desDataDir))
    a_e_1 = stderr1.readlines()
    a_e_2 = stderr2.readlines()
    if a_e_1 != [] or a_e_2 != []:
      print(a_e_1,a_e_2)
      sys.exit()
    client.close()

    client.connect(desIp, 22, 'root')
    stdin3, stdout3, stderr3 = client.exec_command("chown -R mysql.mysql %s*" % (desDataDir+"/"+table))
    a_e_3 = stderr3.readlines()
    if a_e_3 != []:
      print(a_e_1, a_e_2)
      sys.exit()
    client.close()
    #cursor_db_source.execute("select sleep(10);")
    cursor_db_source.execute("unlock tables;")
    cursor_db_des.execute("alter table %s import tablespace;" % (table))
    print("同步完成")

  cursor_db_source.close()
  cursor_db_des.close()

def data_checksum():
  print('data_checksum')
  db_source = pymysql.connect(sourceIp,comUser,comPwd,sourceDbName,comPort,charset='utf8')
  db_des = pymysql.connect(desIp,comUser,comPwd,desDbName,comPort,charset='utf8')
  cursor_db_source = db_source.cursor()
  cursor_db_des = db_des.cursor()

  for table in tableList:
    print("正在校驗表:", table)
    cursor_db_source.execute("checksum table %s;" % (table))
    ck_s = cursor_db_source.fetchall()[0][1]
    cursor_db_des.execute("checksum table %s;" % (table))
    ck_d = cursor_db_des.fetchall()[0][1]
    if ck_s != ck_d:
      print("表不一致:",table)
    else:
      print("表一致:",table)

  cursor_db_source.close()
  cursor_db_des.close()

if __name__ == "__main__":
  table_judge()
  data_sync()
  data_checksum()
  print('haha')

相關文章

  • python實現(xiàn)ftp文件傳輸系統(tǒng)(案例分析)

    python實現(xiàn)ftp文件傳輸系統(tǒng)(案例分析)

    最近做了一個簡單的文件傳輸系統(tǒng),基于ftp協(xié)議,使用python語言開發(fā),雖然python里面已經(jīng)有ftplib模塊,可以很容易的實現(xiàn)ftp服務器,這篇文章主要介紹了python實現(xiàn)ftp文件傳輸系統(tǒng)的案例分析,需要的朋友可以參考下
    2020-03-03
  • python3.0 模擬用戶登錄,三次錯誤鎖定的實例

    python3.0 模擬用戶登錄,三次錯誤鎖定的實例

    下面小編就為大家?guī)硪黄猵ython3.0 模擬用戶登錄,三次錯誤鎖定的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Python中創(chuàng)建對象列表的實現(xiàn)示例

    Python中創(chuàng)建對象列表的實現(xiàn)示例

    本文主要介紹了Python中創(chuàng)建對象列表的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • jupyter使用自動補全和切換默認瀏覽器的方法

    jupyter使用自動補全和切換默認瀏覽器的方法

    這篇文章主要介紹了jupyter使用自動補全和切換默認瀏覽器的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Python實現(xiàn)用戶名和密碼登錄

    Python實現(xiàn)用戶名和密碼登錄

    這篇文章主要為大家詳細介紹了Python實現(xiàn)用戶名和密碼登錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • python實現(xiàn)局域網(wǎng)內(nèi)實時通信代碼

    python實現(xiàn)局域網(wǎng)內(nèi)實時通信代碼

    今天小編就為大家分享一篇python實現(xiàn)局域網(wǎng)內(nèi)實時通信代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 基于PyQt5實現(xiàn)圖轉(zhuǎn)文功能(示例代碼)

    基于PyQt5實現(xiàn)圖轉(zhuǎn)文功能(示例代碼)

    PyQt提供了一個設計良好的窗口控件集合,具有更方便的操作性。學過VB的同學會知道,相比與VB的使用,在界面設計上元素更豐富,這篇文章主要介紹了基于PyQt5完成的圖轉(zhuǎn)文功能,需要的朋友可以參考下
    2022-06-06
  • python 對類的成員函數(shù)開啟線程的方法

    python 對類的成員函數(shù)開啟線程的方法

    今天小編就為大家分享一篇python 對類的成員函數(shù)開啟線程的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python逐行讀取文件內(nèi)容的方法總結

    Python逐行讀取文件內(nèi)容的方法總結

    在本篇文章里小編給大家整理的是關于Python四種逐行讀取文件內(nèi)容的方法,有興趣的朋友們可以學習下。
    2020-02-02
  • 在DigitalOcean的服務器上部署flaskblog應用

    在DigitalOcean的服務器上部署flaskblog應用

    這篇文章主要介紹了在DigitalOcean的服務器上部署flaskblog的方法,flaskblog是用Python的Flask開發(fā)的一個博客程序,而DigitalOcean則是大受歡迎的SSD主機提供商,需要的朋友可以參考下
    2015-12-12

最新評論

岢岚县| 北安市| 治县。| 凌源市| 庐江县| 竹山县| 荔浦县| 聊城市| 南部县| 南部县| 宁安市| 西盟| 日土县| 阿拉尔市| 满洲里市| 怀安县| 崇阳县| 元江| 莲花县| 赤城县| 佛冈县| 阿瓦提县| 肇源县| 洛川县| 北京市| 顺平县| 阳西县| 舟曲县| 威宁| 桑日县| 息烽县| 德阳市| 邵东县| 夏邑县| 陈巴尔虎旗| 二连浩特市| 陆丰市| 黔南| 化州市| 六枝特区| 都匀市|