网址:http://www.schemasync.org
源码:https://github.com/mmatuson/schemasync
命令格式
schemasync [options]
# source/target 格式: mysql://user:pass@host:port/database
# 输出(sql脚本): [_].yyyymmdd.(patch revert)[_].sql
使用方法
先下载schemasync:
wget http://www.schemasync.org/downloads/schemasync-0.9.2.tar.gz
然后解压:
tar xvzf schemasync-0.9.2.tar.gz
执行安装(需要python的setuptools)
python setup.py install
还需要安装 python-mysqldb
apt-get install python-mysqldb
好了,现在可以用schemasync命令比较任意两个mysql db实例,并自动生成两者之间同步和回滚的sql语句:
schemasync mysql://root:pass@host1:3306/db_a mysql://root:pass@host2:3306/db_b
migration scripts created for mysql://localhost/db_b
patch script: ~/db_b.20140716.patch.sql
revert script: ~/db_b.20140716.revert.sql
可以看到它自动产生了两个sql文件,*patch.sql是更新的sql脚本,*revert.sql则是回滚用的sql脚本。下面列出两个脚本的示例。
输出文件内容
more db_b.20140716.patch.sql
--
-- schema sync 0.9.1 patch script
-- created: wed, jul 16, 2014
-- server version: 5.5.37-0ubuntu0.12.04.1-log
-- apply to: host2/db_b
--
use `db_b`;
alter database `db_b` character set=latin1 collate=latin1_swedish_ci;
create table `contact` ( `id` int(11) default null, `phonenumber` varchar(255) default null) engine=innodb default charset=latin1;
create table `person` ( `id` int(11) default null, `lastname` varchar(255) default null) engine=innodb default charset=latin1;
drop table `cond_instances`;
drop table `events_waits_current`;
drop table `events_waits_history`;
drop table `events_waits_history_long`;
drop table `events_waits_summary_by_instance`;
drop table `events_waits_summary_by_thread_by_event_name`;
drop table `events_waits_summary_global_by_event_name`;
drop table `file_instances`;
drop table `file_summary_by_event_name`;
drop table `file_summary_by_instance`;
drop table `mutex_instances`;
drop table `performance_timers`;
drop table `rwlock_instances`;
drop table `setup_consumers`;
drop table `setup_instruments`;
drop table `setup_timers`;
drop table `threads`;
more db_b.20140716.revert.sql
--
-- schema sync 0.9.1 revert script
-- created: wed, jul 16, 2014
-- server version: 5.5.37-0ubuntu0.12.04.1-log
-- apply to: host2/db_b
--
use `db_b`;
alter database `db_b` character set=utf8 collate=utf8_general_ci;
drop table `contact`;
drop table `person`;
create table `cond_instances` ( `name` varchar(128) not null, `object_instance_begin` bigint(20) not null) engine=db_b default charset=utf8;
create table `events_waits_current` ( `thread_id` int(11) not null, `event_id` bigint(20) unsigned not null, `event_name` varchar(128) not null, `source` varchar(64) default null, `timer_start` bigint(20) unsigned default null, `timer_end` bigint(20) unsigned default null, `timer_wait` bigint(20) unsigned default null, `spins` int(10) unsigned default null, `object_schema` varchar(64) default null, `object_name` varchar(512) default null, `object_type` varchar(64) default null, `object_instance_begin` bigint(20) not null, `nesting_event_id` bigint(20) unsigned default null, `operation` varchar(16) not null, `number_of_bytes` bigint(20) unsigned default null, `flags` int(10) unsigned default null) engine=db_b default charset=utf8;
create table `events_waits_history` ( `thread_id` int(11) not null, `event_id` bigint(20) unsigned not null, `event_name` varchar(128) not null, `source` varchar(64) default null, `timer_start` bigint(20) unsigned default null, `timer_end` bigint(20) unsigned default null, `timer_wait` bigint(20) unsigned default null, `spins` int(10) unsigned default null, `object_schema` varchar(64) default null, `object_name` varchar(512) default null, `object_type` varchar(64) default null, `object_instance_begin` bigint(20) not null, `nesting_event_id` bigint(20) unsigned default null, `operation` varchar(16) not null, `number_of_bytes` bigint(20) unsigned default null, `flags` int(10) unsigned default null) engine=db_b default charset=utf8;
create table `events_waits_history_long` ( `thread_id` int(11) not null, `event_id` bigint(20) unsigned not null, `event_name` varchar(128) not null, `source` varchar(64) default null, `timer_start` bigint(20) unsigned default null, `timer_end` bigint(20) unsigned default null, `timer_wait` bigint(20) unsigned default null, `spins` int(10) unsigned default null, `object_schema` varchar(64) default null, `object_name` varchar(512) default null, `object_type` varchar(64) default null, `object_instance_begin` bigint(20) not null, `nesting_event_id` bigint(20) unsigned default null, `operation` varchar(16) not null, `number_of_bytes` bigint(20) unsigned default null, `flags` int(10) unsigned default null) engine=db_b default charset=utf8;
create table `events_waits_summary_by_instance` ( `event_name` varchar(128) not null, `object_instance_begin` bigint(20) not null, `count_star` bigint(20) unsigned not null, `sum_timer_wait` bigint(20) unsigned not null, `min_timer_wait` bigint(20) unsigned not null, `avg_timer_wait` bigint(20) unsigned not null, `max_timer_wait` bigint(20) unsigned not null) engine=performance_schema default charset=utf8;
create table `events_waits_summary_by_thread_by_event_name` (`thread_id` int(11) not null, `event_name` varchar(128) not null, `count_star` bigint(20) unsigned not null, `sum_timer_wait` bigint(20) unsigned not null, `min_timer_wait` bigint(20) unsigned not null, `avg_timer_wait` bigint(20) unsigned not null, `max_timer_wait` bigint(20) unsigned not null) engine=performance_schema default charset=utf8;
create table `events_waits_summary_global_by_event_name` ( `event_name` varchar(128) not null, `count_star` bigint(20) unsigned not null, `sum_timer_wait` bigint(20) unsigned not null, `min_timer_wait` bigint(20) unsigned not null, `avg_timer_wait` bigint(20) unsigned not null, `max_timer_wait` bigint(20) unsigned not null) engine=db_b default charset=utf8;
create table `file_instances` ( `file_name` varchar(512) not null, `event_name` varchar(128) not null, `open_count` int(10) unsigned not null) engine=db_b default charset=utf8;
create table `file_summary_by_event_name` ( `event_name` varchar(128) notnull, `count_read` bigint(20) unsigned not null, `count_write` bigint(20) unsigned not null, `sum_number_of_bytes_read` bigint(20) unsigned not null, `sum_number_of_bytes_write` bigint(20) unsigned not null) engine=performance_schema default charset=utf8;
create table `file_summary_by_instance` ( `file_name` varchar(512) not null, `event_name` varchar(128) not null, `count_read` bigint(20) unsigned not null, `count_write` bigint(20) unsigned not null, `sum_number_of_bytes_read` bigint(20) unsigned not null, `sum_number_of_bytes_write` bigint(20) unsigned not null) engine=db_b default charset=utf8;
create table `mutex_instances` ( `name` varchar(128) not null, `object_instance_begin` bigint(20) not null, `locked_by_thread_id` int(11) default null) engine=db_b default charset=utf8;
create table `performance_timers` ( `timer_name` enum('cycle','nanosecond','microsecond','millisecond','tick') not null, `timer_frequency` bigint(20) default null, `timer_resolution` bigint(20) default null, `timer_overhead` bigint(20) default null) engine=db_b default charset=utf8;
create table `rwlock_instances` ( `name` varchar(128) not null, `object_instance_begin` bigint(20) not null, `write_locked_by_thread_id` int(11) default null, `read_locked_by_count` int(10) unsigned not null) engine=performance_schema default charset=utf8;create table `setup_consumers` ( `name` varchar(64) not null, `enabled` enum('yes','no') not null) engine=db_b default charset=utf8;
create table `setup_instruments` ( `name` varchar(128) not null, `enabled` enum('yes','no') not null, `timed` enum('yes','no') not null) engine=performance_schema default charset=utf8;
create table `setup_timers` ( `name` varchar(64) not null, `timer_name` enum('cycle','nanosecond','microsecond','millisecond','tick') not null) engine=db_b default charset=utf8;
create table `threads` ( `thread_id` int(11) not null, `processlist_id` in t(11) default null, `name` varchar(128) not null) engine=performance_schema default charset=utf8;
