1. 程式人生 > >centos安裝MySQL表結構自動同步-工具 mysql-schema-sync

centos安裝MySQL表結構自動同步-工具 mysql-schema-sync

下載即用

mysql-schema-sync 是一款使用 Go 開發跨平臺的 MySQL 表結構自動同步工具。主要用於解決多個環境資料庫表結構不同步問題。

mysql-schema-sync 支援功能:
同步新表
同步欄位 變動:新增、修改
同步索引 變動:新增、修改
支援預覽(只對比不同步變動)
郵件通知變動結果
支援遮蔽更新表、欄位、索引、外來鍵
支援本地比線上額外多一些表、欄位、索引、外來鍵
專案地址:https://github.com/hidu/mysql-schema-sync

安裝環境

#yum -y install go git

下載工具

#go get -u github.com/hidu/mysql-schema-sync

預設路徑為

#pwd

/root/go/src/github.com/hidu/mysql-schema-sync
把執行指令碼拷貝到配置目錄

#cd /root/go/bin/
#cp mysql-schema-sync /root/go/src/github.com/hidu/mysql-schema-sync/

配置
mysql-schema-sync 預設配置檔案是 config.json,預設在/root/go/src/github.com/hidu/mysql-schema-sync/ 目錄裡。
配置示例(config.json):

{
      //source:同步源
      "source":"test:
[email protected]
(127.0.0.1:3306)/test_0", //dest:待同步的資料庫 "dest":"test:[email protected](127.0.0.1:3306)/test_1", #以下可以不要直接同步也可! //alter_ignore: 同步時忽略的欄位和索引 "alter_ignore":{ "tb1*":{ "column":["aaa","a*"], "index":["aa"], "foreign":[] } }, // tables: table to check schema,default is all.eg :["order_*","goods"] "tables":[], //有變動或者失敗時,郵件接收人 "email":{ "send_mail":false, "smtp_host":"smtp.163.com:25", "from":"
[email protected]
", "password":"xxx", "to":"[email protected]" } }

json配置項說明
source: 資料庫同步源
dest: 待同步的資料庫
tables: 陣列,配置需要同步的表,為空則是不限制,eg: [“goods”,“order_*”]
alter_ignore: 忽略修改的配置,表名為tableName,可以配置 column 和 index,支援萬用字元 *
email : 同步完成後傳送郵件通知資訊

直接執行

./mysql-schema-sync -conf config.json -sync

mysql-schema-sync 命令使用語法

#./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_*
mysql schema sync tools 0.3
https://github.com/hidu/mysql-schema-sync/

引數說明

-conf string 指定配置檔名稱。
-dest string 指定同步的目的資料庫,eg:root:[email protected](127.0.0.1:3306)/wordpress。
-source string 指定同步的源資料庫 eg:root:[email protected](127.0.0.1:3306)/wordpress_new。該項不為空時,忽略讀入 -conf 引數項。
-drop 是否對本地多出的欄位和索引進行刪除,預設值為否。
-sync 是否將修改同步到目標資料庫中去,預設值為否。
-tables string 待檢查同步的資料庫表,為空則是全部。eg : product_base,order_*。

同時同步到多個目標庫
官方有提供一個 SHELL 指令碼 check.sh 可以同時在多個庫間同步。只需為要同步的多個庫生成多個以 .json 為字尾的配置檔案,check.sh 指令碼會依次執行每份配置。

指令碼預設在當前目錄呼叫 mysql-schema-sync,這裡需要根據實際情況修改下:

cat check.sh

for f in ls *.json
do
mysql-schema-sync -conf $f -sync
done

執行指令碼,完成後會在當前目錄的 log 目錄下記錄每次執行後的日誌。

30 * * * * cd /your/path/xxx/ && bash check.sh >/dev/null 2>&1