MySQL表結構自動同步工具mysql-schema-sync安裝使用
阿新 • • 發佈:2018-12-28
mysql表結構自動同步工具
用於將 線上
資料庫結構變化同步到 本地環境
!
支援功能:
- 同步新表
- 同步欄位 變動:新增、修改
- 同步索引 變動:新增、修改
- 支援預覽(只對比不同步變動)
- 郵件通知變動結果
- 支援遮蔽更新表、欄位、索引、外來鍵
- 支援本地比線上額外多一些表、欄位、索引、外來鍵
首先安裝go 和 git環境
安裝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 安裝成功
安裝git
yum -y install git
安裝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":" [email protected]",
"password":"xxx",
"to":"[email protected]"
}
}
[[email protected] 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 [email protected](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: [email protected](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: [email protected](10.10.0.1:3306)/test_1
該項不為空時,忽略讀入 -conf引數項
-drop
是否對本地多出的欄位和索引進行刪除 預設否
-source string
mysql 同步源,eg [email protected](127.0.0.1:3306)/test_0
-sync
是否將修改同步到資料庫中去,預設否
-tables string
待檢查同步的資料庫表,為空則是全部
eg : product_base,order_*
實際使用
比對錶結構並生成sql檔案,不執行
./mysql-schema-sync -conf config.json > alter.sql
檢視alter.sql 生成了一些新增列或者索引的語句,如下
[[email protected] mysql-schema-sync]# head -50 alter.sql
-- Table : fund_sign_config
-- Type : alter
-- RealtionTables :
-- SQL :
ALTER TABLE `fund_sign_config`
ADD `UPDATE_TIME` datetime NOT NULL,
ADD `DEL_FLAG` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否刪除 0 未刪除 1刪除',
ADD `ID` bigint(20) NOT NULL AUTO_INCREMENT,
ADD `CHANNEL` varchar(20) DEFAULT NULL COMMENT '簽約的通道 比如 BF',
ADD `CREATE_TIME` datetime NOT NULL,
ADD `PRIORITY` int(11) NOT NULL COMMENT '優先順序 越大越優先',
ADD `FUND_CODE` varchar(20) NOT NULL COMMENT '資金方編碼',
ADD `SIGN_TYPE` tinyint(4) DEFAULT NULL COMMENT '簽約型別 1 主訂單 2 白條 3 逾期',
ADD `PARTNER_ID` varchar(10) DEFAULT NULL COMMENT '商戶號',
DROP PRIMARY KEY,
ADD PRIMARY KEY (`ID`);
從源庫應用差異結構到目標庫
./mysql-schema-sync -conf config.json -sync
可以利用check.sh 配置定時任務,因為我的測試環境較多 有32套 我自己改寫了指令碼如下
配置定時任務,每週從線上往測試同步一次
#定時從線上同步表結構到測試環境 mysql-schema-sync
00 01 * * 7 cd /root/go/src/github.com/hidu/mysql-schema-sync && bash check.sh >/dev/null 2>&1