日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python3 mysql同步_MySQL上云同步脚本-Python3

發布時間:2024/1/23 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 mysql同步_MySQL上云同步脚本-Python3 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

MySQL上云同步腳本-Python3

最近在做本地同步至云的腳本

使用kettle的話,幾百個表的同步要一一設置,實在是蛋疼的緊

還好python可以解決一部分問題,所以寫了個轉換

由于5.6版本的mysql對null的處理不是很好,因此全部轉為varchar型

blob和longblob,text都單獨處理

#-*- coding: UTF-8 -*-

#獲取對比列表

#表結構同步到云上mysql

#實現功能:oracle-mysql列轉換/寫入腳本/傳輸腳本

#待實現功能:執行腳本

#james.peng 20170905

import os

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

import cx_Oracle

import pymysql

import datetime

import time

Start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

date_nyr = time.strftime('%Y%m%d', time.localtime(time.time()))

#存放結果的txt

os_dir=os.chdir('/a/e/p/y')

remote_loc='/a/e/p/y/'+date_nyr+'/'

try:

os.mkdir(date_nyr)

linux_shell='chmod 777 '+date_nyr

os.popen(linux_shell)

except:

print('folder_existed!')

os_dir='/a/e/p/y/'+date_nyr

os.chdir(os_dir)

try:

f=open(os_dir+'/create_y_script.txt','w')

f.truncate()

f.write("use "+Mysql_schema+";\n")

except:

print('exception!')

print(os.getcwd())

#存放結果的list

create_yrs_table_list=[]

#parameters--參數設置

jump_server_host=

jump_server_destination=

Mysql_yrs_ip_port=

Mysql_yrs_username=

Mysql_yrs_passwd=

Mysql_yrs_db=

Mysql_yrs_port=

Mysql_yrs_schema=

Mysql_ip_port=

Mysql_username=

Mysql_passwd=

Mysql_db=

Mysql_port=

Mysql_schema=

#connection strings--連接信息

mysql_yrs_info = pymysql.connect(Mysql_yrs_ip_port,Mysql_yrs_username,Mysql_yrs_passwd,Mysql_yrs_db,Mysql_yrs_port,charset='utf8')

mysql_yrs_cursor = mysql_yrs_info.cursor()

mysql_info = pymysql.connect(Mysql_ip_port,Mysql_username,Mysql_passwd,Mysql_db,Mysql_port)

mysql_cursor = mysql_info.cursor()

#開始連接,

#<<<>>>先獲取要同步的表,按表名順序排列

mysql_yrs_cursor.execute("SET group_concat_max_len=10000;")

Mysql_yrs_table_name_sql="select table_name from information_schema.tables where "

Mysql_yrs_table_name_sql+="TABLE_SCHEMA='"+Mysql_yrs_schema+"' order by table_name asc ;"

mysql_yrs_cursor.execute(Mysql_yrs_table_name_sql)

Mysql_yrs_table_name = mysql_yrs_cursor.fetchall()

#print(Mysql_yrs_table_name)

for i_sync_list in Mysql_yrs_table_name:

i_sync_list=str(i_sync_list).replace('(','').replace(',)','').replace('\'','')

print(i_sync_list)

drop_table_sql="drop table "+Mysql_schema+"."+i_sync_list+';'

"""

construct_table_sql="select concat('create table test320.account_credit(',"

construct_table_sql+=" group_concat(concat(column_name,' varchar(' ,"

construct_table_sql+=" case when data_type in ('int','decimal','bigint','tinyint','double','bit') then numeric_precision*3"

construct_table_sql+=" when data_type in ('varchar','char') then round(character_maximum_length*1.2) "

construct_table_sql+=" when data_type in ('date','datetime','datetime','timestamp') then '50' "

construct_table_sql+=" else data_type end ,')'))) from information_schema.columns "

construct_table_sql+=" where TABLE_SCHEMA='test320'and table_name='account_credit'"

"""

construct_table_sql="select concat('create table "+Mysql_schema+"."+i_sync_list+"(',"

construct_table_sql+=" group_concat(concat(column_name,' varchar(' ,"

construct_table_sql+=" case when data_type in ('int','decimal','bigint','tinyint','double','bit') then round(numeric_precision*1.2)"

construct_table_sql+=" when data_type in ('varchar','char') then round(character_maximum_length*1.2) "

construct_table_sql+=" when data_type in ('date','datetime','datetime','timestamp') then '50' "

construct_table_sql+=" else data_type end ,')'))) from information_schema.columns "

construct_table_sql+=" where TABLE_SCHEMA='"+Mysql_yrs_schema+"'and table_name='"+i_sync_list+"'"

#print("構建")

#print(construct_table_sql)

#print("構建完成")

mysql_yrs_cursor.execute(construct_table_sql)

construct_table_sql_rslt=str(mysql_yrs_cursor.fetchall()).replace("(('","").replace("'))",");")

construct_table_sql_rslt=construct_table_sql_rslt.replace("varchar(longtext)","longtext").replace("varchar(blob)","blob")

construct_table_sql_rslt=construct_table_sql_rslt.replace("varchar(text)","text").replace("varchar(longblob)","longblob")

#print("行:\n")

#print(drop_table_sql)

#print("行:\n")

#print(construct_table_sql_rslt)

f.write(drop_table_sql)

f.write('\n')

f.write(construct_table_sql_rslt)

f.write('\n')

#print(mysql_create_sql_build)

f.close()

linux_shell="/usr/bin/scp -P 20 create_yrs_script.txt rds@aliyunsql:/h/r/a/i/y/"

linux_shell_result=os.popen(linux_shell).read()

print(linux_shell)

print(linux_shell_result)

mysql_yrs_cursor.close()

mysql_cursor.close()

End_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

print('開始時間', Start_time)

print('完成時間', End_time)

總結

以上是生活随笔為你收集整理的python3 mysql同步_MySQL上云同步脚本-Python3的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。