Giter Site home page Giter Site logo

mysql-schema-sync's Introduction

mysql-schema-sync

MySQL Schema 自动同步工具

用于将 线上 数据库 Schema 变化同步到 本地测试环境! 只同步 Schema、不同步数据。

支持功能:

  1. 同步新表
  2. 同步字段 变动:新增、修改
  3. 同步索引 变动:新增、修改
  4. 支持预览(只对比不同步变动)
  5. 邮件通知变动结果
  6. 支持屏蔽更新表、字段、索引、外键
  7. 支持本地比线上额外多一些表、字段、索引、外键
  8. 在该项目的基础上修复了比对过程中遇到分区表会终止后续操作的问题,支持分区表,对于分区表,会同步除了分区以外的变更。
  9. 支持每条 ddl 只会执行单个的修改,目的兼容tidb ddl问题 Unsupported multi schema change,通过single_schema_change字段控制,默认关闭。

安装

go install github.com/hidu/mysql-schema-sync@master

配置

参考 默认配置文件 config.json 配置同步源、目的地址。
修改邮件接收人 当运行失败或者有表结构变化的时候你可以收到邮件通知。

默认情况不会对多出的表、字段、索引、外键删除。若需要删除字段、索引、外键 可以使用 -drop 参数。

配置示例(config.json):

cp config.json mydb_conf.json
{
      //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"],
            "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": [],
      //有变动或者失败时,邮件接收人
      "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 : 同步完成后发送邮件通知信息
single_schema_change:是否每个ddl只执行单个修改

运行

直接运行

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

预览并生成变更sql

./mysql-schema-sync -drop -conf mydb_conf.json 2>/dev/null >db_alter.sql

使用shell调度

bash check.sh

每个json文件配置一个目的数据库,check.sh脚本会依次运行每份配置。 log存储在当前的log目录中。

自动定时运行

添加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
        待同步的数据库 eg: test@(10.10.0.1:3306)/test_1
        该项不为空时,忽略读入 -conf参数项
  -drop
        是否对本地多出的字段和索引进行删除 默认否
  -http
        启用web站点显示运行结果报告的地址,如 :8080,默认否
  -source string
        mysql 同步源,eg test@(127.0.0.1:3306)/test_0
  -sync
        是否将修改同步到数据库中去,默认否
  -tables string
        待检查同步的数据库表,为空则是全部
        eg : product_base,order_*
  -single_schema_change
        生成 SQL DDL 语言每条命令是否只会进行单个修改操作,默认否

mysql-schema-sync's People

Contributors

514366607 avatar blylei avatar bryant1410 avatar hidu avatar kevalin avatar leeyisoft avatar ser163 avatar wbflooksky avatar xiaohei16h avatar yinheli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mysql-schema-sync's Issues

阿里云两个RDS实例同步没有信息

两个阿里云RDS同步,生成sql脚本,里面没有任何信息。去掉 2>/dev/null 看到查询都是空。
在同一个实例里两个库同步是正常的。

表同步有问题,

2020/07/21 db.go:71: Error 1146: Table 'clj.test1' doesn't exist
2020/07/21 index.go:113: db_index parse failed,unsupported,line: ) ENGINE=InnoDB DEFAULT CHARSET=utf8
test1 表没有索引

某张表没有索引,报错了

image

show create table xx_activeuser_usage

CREATE TABLE xx_activeuser_usage (
account_id varchar(36) NOT NULL,
host_id varchar(36) NOT NULL,
start_date date NOT NULL,
PRIMARY KEY (account_id,start_date,host_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (TO_DAYS (start_date))
(PARTITION p201510 VALUES LESS THAN (736268) ENGINE = InnoDB,
PARTITION p201511 VALUES LESS THAN (736298) ENGINE = InnoDB,
PARTITION p201512 VALUES LESS THAN (736329) ENGINE = InnoDB,
PARTITION p201601 VALUES LESS THAN (736360) ENGINE = InnoDB,
PARTITION p201602 VALUES LESS THAN (736389) ENGINE = InnoDB,
PARTITION p201603 VALUES LESS THAN (736420) ENGINE = InnoDB,
PARTITION p201604 VALUES LESS THAN (736450) ENGINE = InnoDB,
PARTITION p201605 VALUES LESS THAN (736481) ENGINE = InnoDB,
PARTITION p201606 VALUES LESS THAN (736511) ENGINE = InnoDB,
PARTITION p201812 VALUES LESS THAN (737425) ENGINE = InnoDB) */

能不能讲下原理

我是java开发,不会go,想用这个工具,但是想知道下原理,一般是通过binlog同步的,不过这个工具是通过查询还是什么方式 不清楚

无法通过go install 安装

因为我的网络原因 无法通过go install 进行安装
我是否能将master.zip 下载下来进行编译呢?

对比结果不成功

root@ZabbixServer:~/downloads/go/src/github.com/hidu/mysql-schema-sync# ./mysql-schema-sync -conf config.json -sync
2019/05/29 db.go:85: [SQL] [source] show table status []
2019/05/29 schemaSync.go:265: source db table total: 85
2019/05/29 schemaSync.go:270: Index : 0 Table : ti_account
2019/05/29 schemaSync.go:277: Table: ti_account skip
2019/05/29 schemaSync.go:270: Index : 1 Table : ti_account_activity_collect
2019/05/29 schemaSync.go:277: Table: ti_account_activity_collect skip
2019/05/29 schemaSync.go:270: Index : 2 Table : ti_account_attention_act
2019/05/29 schemaSync.go:277: Table: ti_account_attention_act skip
2019/05/29 schemaSync.go:270: Index : 3 Table : ti_account_auth
2019/05/29 schemaSync.go:277: Table: ti_account_auth skip
2019/05/29 schemaSync.go:270: Index : 4 Table : ti_account_auth_backup
2019/05/29 schemaSync.go:277: Table: ti_account_auth_backup skip
2019/05/29 schemaSync.go:270: Index : 5 Table : ti_account_blacklist
2019/05/29 schemaSync.go:277: Table: ti_account_blacklist skip
2019/05/29 schemaSync.go:270: Index : 6 Table : ti_account_cover
2019/05/29 schemaSync.go:277: Table: ti_account_cover skip
2019/05/29 schemaSync.go:270: Index : 7 Table : ti_account_cover_edit_verify
2019/05/29 schemaSync.go:277: Table: ti_account_cover_edit_verify skip
2019/05/29 schemaSync.go:270: Index : 8 Table : ti_account_destroy
2019/05/29 schemaSync.go:277: Table: ti_account_destroy skip
2019/05/29 schemaSync.go:270: Index : 9 Table : ti_account_edit_record
2019/05/29 schemaSync.go:277: Table: ti_account_edit_record skip
2019/05/29 schemaSync.go:270: Index : 10 Table : ti_account_edit_verify
2019/05/29 schemaSync.go:277: Table: ti_account_edit_verify skip
2019/05/29 schemaSync.go:270: Index : 11 Table : ti_account_intent
2019/05/29 schemaSync.go:277: Table: ti_account_intent skip
2019/05/29 schemaSync.go:270: Index : 12 Table : ti_account_invite
2019/05/29 schemaSync.go:277: Table: ti_account_invite skip
2019/05/29 schemaSync.go:270: Index : 13 Table : ti_account_last_edit_log
2019/05/29 schemaSync.go:277: Table: ti_account_last_edit_log skip

同步bug

source ---> dest
从邮件来看:
1.source有表,dest没有
2.after没了,alter after也没有
触发的操作:drop table table_name

然后在同步一 遍

表同步出现错误,想问一下最低支持的mysql版本

EXEC_SQL_FAIELD Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
ALTER TABLE n_system_apply
ADD CONSTRAINT FK_10egw3bdhwrghm091ueehk8te FOR' at line 3
c9ebe276095a131821bdb5c44a534ec9

不能用,skip

2019/06/13 db.go:85: [SQL] [source] show table status []
2019/06/13 schemaSync.go:265: source db table total: 1
2019/06/13 schemaSync.go:270: Index : 0 Table : test
2019/06/13 schemaSync.go:277: Table: test skip
2019/06/13 schemaSync.go:303: trace changedTables: map[]
2019/06/13 schemaSync.go:352: execute_all_sql_done,success_total: 0 failed_total: 0
2019/06/13 statics.go:137: no table change,skip send mail

{
"source":"root:root@(192.168.1.189:3306)/test",
"dest":"root:root@(192.168.1.189:3306)/test2",
"tables":[],
"email":{
"send_mail":true,
"smtp_host":"",
"from":"
",
"password":"",
"to":"
"
}
}

问题

是否考虑出Windows版本

同步数据问题

该方案是只支持将数据结构从一个库同步到另一个库,还是同样也支持同一个库里主表和分表的结构同步

Couldn't send email and got errors

after I ran the script,I got the message below:
email.go:76: send mail failed,err: x509: certificate is valid for *.263.net, 263.net, not smtp.xxxx.com
what's wrong with it?

如何能不删除目的数据库中多出来的表

如何能不删除目的数据库中多出来的表
比如:目的DB 比 源DB 多出来一个表t1
在执行:mysql-schema-sync -conf mydb_conf.json 2>/dev/null >db_alter.sql
文件 db_alter.sql 中会有一个 drop table t1 的语句
咱们项目的 readme 中提到 “默认情况不会对多出的表、字段、索引、外键删除”
但还是会执行 drop 操作,这里是不是存在BUG ?

db_index parse failed,unsupported,line:

2019/12/30 schemaSync.go:270: Index : 73 Table : sheet_histories
2019/12/30 db.go:86: [SQL] [source] show create table sheet_histories []
2019/12/30 db.go:86: [SQL] [dest] show create table sheet_histories []
2019/12/30 index.go:113: db_index parse failed,unsupported,line: ) ENGINE=InnoDB AUTO_INCREMENT=7814 DEFAULT CHARSET=utf8

what does this mean

当有表分区时无法同步

CREATE TABLE `tab_test` (
  `test_id` bigint(20) unsigned NOT NULL COMMENT 'id',
  `test_str` varchar(128) DEFAULT NULL,
  `test_time` int(11) unsigned NOT NULL COMMENT '时间戳',
 KEY `test_id` (`test_id`) USING BTREE,
 KEY `login_time` (`test_time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test'
/*!50100 PARTITION BY HASH (test_id)
PARTITIONS 10 */;

index.go:111: db_index parse failed,unsupport,line: ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test'

CheckMatchIgnoreTables Bug?

func (cfg *Config) CheckMatchIgnoreTables(name string) bool {
	if len(cfg.TablesIGNORE) == 0 {
		return true  // ****should return false**** 
	}
	for _, tableName := range cfg.TablesIGNORE {
		if simpleMatch(tableName, name, "CheckMatchTables") {
			return true
		}
	}
	return false
}

支持SSH隧道

数据库在防火墙后面,需要通过SSH隧道进行连接,希望能添加此功能

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.