最近在整集成环境,环境是搭建好了,但是新环境的数据库表结构和测试环境的差异太大,导致服务各种报错,前几天都是哪个报错,就同步那张表,后面发现太多了;今天发现了一个工具mysql-schema-sync,真的太好用了。下面将介绍mysql-schema-sync+python+jenkins结合起来,定时同步表结构,再也不用担心因为表结构的问题了
1.服务器上安装工具mysql-schema-sync
1.安装go语言环境
下载安装包
wget -c https://studygolang.com/dl/golang/go1.10.3.linux-amd64.tar.gz
解压到/usr/local
tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
配置环境变量
vi /etc/profile
在文件末尾添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
生效环境变量
source /etc/profile
验证
go version
go version go1.10.3 linux/amd64
安装成功
2.安装git
yum -y install git
3.安装mysql-schema-sync
go get -u github.com/hidu/mysql-schema-sync
执行命令会在当前目录生成一个go/文件夹
以我的安装目录为例 我下载在了/root 目录下 ,
mysql-schema-sync的命令在/root/go/bin/下
配置文件config.json 和 自动运行脚本check.sh 在目录/root/go/src/github.com/hidu/mysql-schema-sync下
配置文件
一定要注意新添加的参数 -tables_ignore string 如果默认值的话工具会跳过所有表的检测
解决办法可以写一个没有的表名 例如 -tables_ignore "x"
{
"source":"root:密码@(10.238.160.1:3306)/xiaodai",
"dest":"root:密码@(10.238.160.2:3306)/xiaodai",
"alter_ignore":{
"tb1*":{
"column":["aaa","a*"],
"index":["aa"],
"foreign":[]
}
},
// tables: table to check schema,default is all.eg :["order_*","goods"]
"tables":[],
// tables_ignore: table to ignore check schema,default is Null :["order_*","goods"]
"tables_ignore":["x"],
"email":{
"send_mail":false,
"smtp_host":"smtp.163.com:25",
"from":"xxx@163.com",
"password":"xxx",
"to":"xxx@163.com"
}
}
[root@10-238-160-1 bin]# ./mysql-schema-sync --help
Usage of ./mysql-schema-sync:
-conf string
json config file path (default "./config.json")
-dest string
mysql dsn dest,eg test@(127.0.0.1:3306)/imis
-drop
drop fields,index,foreign key
-mail_to string
overwrite config's email.to
-source string
mysql dsn source,eg: test@(10.10.0.1:3306)/test
when it is not empty ignore [-conf] param
-sync
sync shcema change to dest db
-tables string
table names to check
eg : product_base,order_*
-tables_ignore string
table names to ignore check
eg : product_base,order_*
mysql schema sync tools 0.3
https://github.com/hidu/mysql-schema-sync/
参数解释:
# mysql-schema-sync -help
-conf string
配置文件名称
-dest string
待同步的数据库 eg: test@(10.10.0.1:3306)/test_1
该项不为空时,忽略读入 -conf参数项
-drop
是否对本地多出的字段和索引进行删除 默认否
-source string
mysql 同步源,eg test@(127.0.0.1:3306)/test_0
-sync
是否将修改同步到数据库中去,默认否
-tables string
待检查同步的数据库表,为空则是全部
eg : product_base,order_*
实际使用
比对表结构并生成sql文件,不执行
./mysql-schema-sync -conf config.json > alter.sql
从源库应用差异结构到目标库
./mysql-schema-sync -conf config.json -sync
可以利用check.sh 配置定时任务,可以自行了解,我参考的是:MySQL表结构自动同步工具mysql-schema-sync安装使用 - 灰信网(软件开发博客聚合)
2.python脚本编写
这个脚本的功能主要是查询出所有库、通过mysql-schema-sync循环同步库表
#!usr/bin/python
#-*- coding:utf-8 _*-
"""
@author:xiana
@file: SyncDB.py
@time: 2022/4/14 3:34 下午
"""
import json
import MySQLdb
import os
class SyncAllDB:
def __init__(self):
self.db = MySQLdb.connect("192.168.1.1", "账号", "密码", "库名", charset='utf8')
def query(self, sql):
cursor = self.db.cursor()
cursor.execute(sql)
data = cursor.fetchall()
self.db.close()
self.data = data
return self
def listdb(self):
db_list = []
for data in self.data:
db_list.append(data[0])
self.db_list_name = db_list
return self
def syncdb(self, db):
control = {
"source":"root:密码@(10.238.160.1:3306)/xiaodai",
"dest":"root:密码@(10.238.160.2:3306)/xiaodai",
"alter_ignore":{
"tb1*":{
"column":["aaa","a*"],
"index":["aa"],
"foreign":[]
}
},
"tables":[],
"tables_ignore":["x"],
"email":{
"send_mail":false,
"smtp_host":"smtp.163.com:25",
"from":"xxx@163.com",
"password":"xxx",
"to":"xxx@163.com"
}
}
json.dump(control, open('config.json', 'w'), indent=4)
def syncAllDB(self):
for db in self.db_list_name:
self.syncdb(db)
//将当前目录下的config.json文件复制到mysql-schema-sync的bin目录下,然后进入到该bin目录,最后执行同步命令
os.system("cp config.json /home/app/go/bin "
"&& cd /home/app/go/bin"
"&& ./mysql-schema-sync -conf config.json -sync")
if __name__ == '__main__':
s = SyncAllDB()
s.query("show databases").listdb().syncAllDB()
3.在jenkins上集成以上操作
创建一个自由风格的项目,配置执行以上python脚本 ,需要定时执行,在jenkins上配置一下就行