mysql-schema-sync 是一款使用go開發的、跨平台的、綠色無依賴的 MySQL 表結構自動同步工具。
用於將線上(其他環境)數據庫結構變化同步到測試(本地)環境!
可以解決多人開發,每人都使用獨立數據庫導致結構不一樣的問題!
支持功能:
1. 同步新表
2. 同步字段 變動:新增、修改
3. 同步索引 變動:新增、修改
4. 同步字段、索引 屏蔽變動更新
5. 支持預覽(只對比不同步變動)
6. 郵件通知變動結果
配置
參考 默認配置文件 config.json 配置同步源、目的地址。
修改郵件接收人 當運行失敗或者有表結構變化的時候你可以收到郵件通知。
默認情況不會對多出的表、字段、索引刪除。若需要刪除字段、索引 可以使用-drop參數。
配置示例(config.json):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
//source:同步源
"source"
:
"test:test@(127.0.0.1:3306)/test_0"
,
//dest:待同步的數據庫
"dest"
:
"test:test@(127.0.0.1:3306)/test_1"
,
//alter_ignore: 同步時忽略的字段和索引
"alter_ignore"
:{
"tb1*"
:{
"column"
:[
"aaa"
,
"a*"
],
"index"
:[
"aa"
]
}
},
// tables:
table
to
check
schema
,
default
is
all
.eg :[
"order_*"
,
"goods"
]
"tables"
:[],
//有變動或者失敗時,郵件接收人
"email"
:{
"send_mail"
:
false
,
"smtp_host"
:
"smtp.163.com:25"
,
"from"
:
"xxx@163.com"
,
"password"
:
"xxx"
,
"to"
:
"xxx@163.com"
}
}
json配置項說明
source: 數據庫同步源
dest: 待同步的數據庫
tables: 數組,配置需要同步的表,為空則是不限制,eg: ["goods","order_*"]
alter_ignore: 忽略修改的配置,表名為tableName,可以配置 column 和 index,支持通配符 *
email : 同步完成后發送郵件通知信息
運行
1
2
3
4
5
#運行同步
mysql-schema-
sync
-conf mydb_conf.json -
sync
#預覽並生成變更sql
mysql-schema-
sync
-conf mydb_conf.json 2>
/dev/null
>db_alter.sql
自動定時運行
添加crontab 任務
30 * * * * cd /your/path/xxx/ && bash check.sh >/dev/null 2>&1
參數說明
mysql-schema-sync [-conf] [-dest] [-source] [-sync] [-drop]
說明:
#mysql-schema-sync -help -conf string 配置文件名稱 -dest string mysql 同步源,eg test@(127.0.0.1:3306)/test_0 -drop 是否對本地多出的字段和索引進行刪除 默認否 -source string 待同步的數據庫 eg: test@(10.10.0.1:3306)/test_1 該項不為空時,忽略讀入 -conf參數項 -sync 是否將修改同步到數據庫中去,默認否 -tables string 待檢查同步的數據庫表,為空則是全部 eg : product_base,order_*
來源:http://git.oschina.net/hidu/mysql-schema-sync