标签:trigger replication filter binlog format sql_mode
需求描述1.内网服务器有俩套主从复制环境,一套是基于传统复制的5.6.26版本,另外一套是基于GTID的5.7.19版本的复制。现在开发的需求是需要将基于传统复制的上面的俩个表同步到基于基于GTID复制上面去,并且要求同步的俩个表中有一个表的一列的值必须是源的10倍。
root@mysqldb 15:51: [remix_test]> show create table sbtest1 \G
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL,
`k` int(11) NOT NULL DEFAULT ‘0‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
表结构如上所示,在原来的基础上column k必须乘以10,即new_k = old_k*10
1.如何将俩个表的数据同步过来。在这里我们采用了使用主从同步的方案,但是主从同步的方案又带来了以下几个问题:
2.如何将同步过来的数据做修改。在这里我们采取的方案是使用触发器,但是触发器本身会消耗资源;因为触发器是基于row行的,所以假如我们一次性修改500条数据花费1s中,触发器触发修改会触发500次,加入每行数据修改花费1s,那么触发器就会花费500s。
Last_SQL_Error: Error ‘Can‘t update table ‘sbtest1‘ in stored function/trigger because it is already used by statement which invoked this stored function/trigger.‘ on query. Default database: ‘remix_test‘. Query: ‘insert into sbtest1(id,k) values(39039,1),(39040,10)‘
1.数据同步:
mysql> set global sql_mode=‘ON_PERMISSIVE‘;
mysql> Change replication filter REPLICATE_DO_DB=(remix_test),REPLICATE_DO_TABLE = (remix_test.sbtest1,remix_test.sbtest2);
mysql> delimiter || ;
mysql> create trigger tr1 after insert on sbtest1
-> for each row
-> begin
-> update sbtest1 set k=new.k*10 where id=new.id;
-> end||
mysql> delimiter ;
shell> mysqldump --set-gtid-purged=OFF --single-transaction --dump-slave=2 -uroot -p -t remix_test sbtest1 > sbtest1_20180402.sql
shell> mysqldump --set-gtid-purged=OFF --single-transaction --dump-slave=2 -uroot -p -t remix_test sbtest2 > sbtest2_20180402.sql
shell> mysql -S /var/lib/mysql/mysql_3306.sock -uroot -p remix_test< sbtest1_20180402.sql
shell> mysql -S /var/lib/mysql/mysql_3306.sock -uroot -p remix_test<sbtest2_20180402.sql
mysql> grant replication slave on ‘slave‘@‘ip_address‘ identified by ‘new_password‘;
mysql> flush privileges;
mysql> set global sql_mode=‘ON_PERMISSIVE‘;
mysql> change master ......
mysql> change replication replication_do_db=(remix_test),replication_do_table=(remix_test.sbtest1,remix_test.sbtest2);
mysql> create table sbtest1_bak like sbtest1;
mysql> insert into sbtest1_bak select id,k*10 from sbtest1;
9.基于GTID复制的MASTER上面创建触发器
root@mysqldb 15:39: [remix_test]> delimiter ||
root@mysqldb 15:41: [remix_test]> create trigger tr2 after update on sbtest1
-> for each row
-> begin
-> update sbtest1_bak set k=new.k*10 where id=new.id;
-> end||
root@mysqldb 15:48: [remix_test]> delimiter ||
root@mysqldb 15:48: [remix_test]> create trigger tr3 after delete on sbtest1
-> for each row
-> begin
-> delete from sbtest1_bak where id=old.id;
-> end||
Query OK, 0 rows affected (0.11 sec)
root@mysqldb 15:49: [remix_test]> delimiter ;
root@mysqldb 15:06: [remix_test]> create trigger tr1 after insert on sbtest1
-> for each row
-> begin
-> insert into sbtest1_bak(id,k) select id,k*10 from sbtest1 where id=new.id;
-> end||
Query OK, 0 rows affected (0.11 sec)
root@mysqldb 15:08: [remix_test]> delimiter ;
【20180402】MySQL关于replication filter和trigger的一些应用
标签:trigger replication filter binlog format sql_mode
原文地址:http://blog.51cto.com/11819159/2094349