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相关推荐
- mysql数据库搬家,MySQL数据库迁移上云-数据搬家不用愁
MySQL数据库迁移上云 数据搬"家"不用愁 随着数字化时代的发展,越来越多的企业开始数字化转型.云上数据库能提升对数据库的使用及管理效率,给企业带来了很大经济效益.那么如何安全有 ...
- mysql 单向自动同步_mysql单向自动同步
mysql自动同步 以下教程均使用mysql自带的自动同步功能 全库单向自动同步 本例把192.168.3.45上名称为ewater_main的数据库自动同步到192.168.3.68的ewater_ ...
- mysql5.1 主主同步_mysql主主同步指定库的指定表(version 5.1~5.7)
server1: ip:1.1.1.1 server2: ip:1.1.1.2 <1> 修改server1的配置文件vim /etc/my.cnf [mysqld] vim /etc/my ...
- 计算机里面百度云同步盘,百度云同步盘和百度网盘之间的区别是什么
百度的这个"云网盘"虽然容量有 2TB,但是限制了单个文件大小为 4G,文件修改缺少历史版本,没有团队版的功能,从某种意义上看,和真正的百度云同步盘有不少差别.百度网盘和百度云同步 ...
- 计算机云同步,Cupcloud(电脑云同步软件)
Cupcloud电脑操作云同步软件是一款能让两台电脑同步操作的工具,可以将电脑本地的一些操作进行网络存档,当你在另一台电脑登陆时,即可快速恢复之前的操作,没有系统限制,即便是Windows和max之间 ...
- mysql 备库同步_MYSQL主从库同步配置过程
MYSQL主从库同步配置过程 为了实现网站数据库的异地备份,采用了MySQL数据库主从同步配置,需要两台服务器分别作为主从库,当主库发生增删改等操作,会实时反映到从库,我的个人服务器配置如下: 主库为 ...
- mysql负载均衡与同步_MySql数据库从同步负载均衡实时备份
如果你对MySql数据库主从同步负载均衡实时的备份,的实际操作步骤感到很是很郁闷时.你就可以浏览下面的文章了,供大家分享. 最近将四台MySQL数据库服务器配置成主从模式以实现一定的负载均衡,好象还可 ...
- 主mysql删除从服务不同步_MySQL主从不同步解决
由于binlog日志带多删除了几个后发现MySQL主从不同步 1.由于binlog日志带多删除了几个后发现MySQL主从不同步 mysql> show slave status\G; Slave ...
- mariadb mysql同步_MySQL(Mariadb)主从同步基础
一.准备阶段 两台linux虚拟机centos7版本,都安装了mariadb 5.5.6 进行主从同步配置: 首先防火墙要开放3306端口, 主库服务器ip:192.168.211.100 从库服务器 ...
最新文章
- JavaScript数组的API
- 我也来推荐一个强大的flash应用
- SAP CRM Cross Component级别的跳转如果出了问题,该如何调试
- QQProtect.exe(Q盾)
- 没想到,我都来阿里5年了!
- Google Protocol Buffers浅析(四)
- linux adc是什么设备,Linux 驱动--ADC驱动
- 部队计算机考试题,2020军队文职计算机知识:计算机考试练习题(1)
- 三星性能测试软件,三星R25的性能测试
- Intellij Idea下tomcat设置自动编译
- 国内外网站设计与浏览习惯的差异
- 1.ESP32c3 移植lvgl核心组件教程
- Unity 脚本入门教程
- Matlab图例设置
- 分布式服务架构原理、设计
- Selenium + C# 实现模拟百度贴吧签到 1
- 中国石油安全问题及解决对策
- 腾讯服务器鉴黄系统,腾讯云智能鉴黄系统
- cortex系列处理器排行_arm处理器排行_ARM Cortex A系列处理器性能分类比较ARM处理器排名 ZNDS资讯...
- 算法导论第三章思考题