场景

node1 和 node2 为两台不同业务的MySQL服务器。
业务方有个需求,需要将node1上的 employees库的departments 、dept_manager 这2张表同步到 node2 的 hellodb 库下面。

这里涉及到3个方面问题,

  1. 版本升级,5.5 -> 5.7
  2. 复制过滤,只复制 departments 、dept_manager 2张表
  3. 重写库名,主库名为 employees ,备库名为 hellodb

接下来,我们一步步解决上述的 3 个问题。

架构图1:

架构图2:

架构图3:

一、版本升级

原主库版本是 5.5.57,新库版本是 5.7.21(开启了gtid,master_auto_position=1)。

首先要把新库的复制改为传统复制:

# node2 && node3

stop slave;
CHANGE MASTER TO master_auto_position=0;
start slave;

然后关掉 GTID:

# node2 && node3

# node2: set global gtid_mode=ON_PERMISSIVE;
# node3: set global gtid_mode=ON_PERMISSIVE;
# node2: set global gtid_mode=OFF_PERMISSIVE;
# node3: set global gtid_mode=OFF_PERMISSIVE;
# node2: set global gtid_mode=OFF;
# node3: set global gtid_mode=OFF;
# node2: set global enforce_gtid_consistency=OFF;
# node3: set global enforce_gtid_consistency=OFF; # 注意 GTID 的在线开启和关闭,主从需要依次逐步开启

这样,5.5 到 5.7 的复制就不会有GTID的硬性问题了(如果你碰到其他不兼容,比如datetime默认值问题,谷歌解决即可)

然后把 node1 的employees库的departments 、dept_manager 这2张表同步到 node3 的 hellodb 库下面。

# node3

stop slave;
change master to ...

附:如何从传统复制改为基于GTID的复制?

# 开启 GTID
主从: set global gtid_mode=OFF_PERMISSIVE;
主从: set global gtid_mode=ON_PERMISSIVE;
主从: set global enforce_gtid_consistency=ON;
主从: set global gtid_mode=ON; # 改 auto_increment = 1
stop slave;
CHANGE MASTER TO master_auto_position=1;
start slave;

二、复制过滤及重写库名

接下来设置复制过滤,我采用了笨重的重启方法(5.7 已经支持在线设置复制过滤了),这也是为什么业务迁到 node2,我却用node3 作为 node1的从库,因为需要重启。

修改node3的配置文件:

replicate-rewrite-db = employees -> hellodb
replicate-wild-do-table=hellodb.departments
replicate-wild-do-table=hellodb.dept_manager 注意,如果有 replicate-rewrite-db , replicate-wild-do-table 后面的库名要写 rewrite 之后的库名,而不是原来的库名。

然后重启 node3,现在,node1 employees 库下面的 departments 、dept_manager 表,就会实时同步到 node3和node2 。

node1 -> node3 -> node2

show slave status\G
Replicate_Wild_Do_Table: hellodb.departments,hellodb.dept_manager
Replicate_Rewrite_DB: (employees,hellodb)

附:在线修改复制过滤

Syntax:
CHANGE REPLICATION FILTER filter[, filter][, ...] filter:
REPLICATE_DO_DB = (db_list)
| REPLICATE_IGNORE_DB = (db_list)
| REPLICATE_DO_TABLE = (tbl_list)
| REPLICATE_IGNORE_TABLE = (tbl_list)
| REPLICATE_WILD_DO_TABLE = (wild_tbl_list)
| REPLICATE_WILD_IGNORE_TABLE = (wild_tbl_list)
| REPLICATE_REWRITE_DB = (db_pair_list) db_list:
db_name[, db_name][, ...] tbl_list:
db_name.table_name[, db_table_name][, ...]
wild_tbl_list:
'db_pattern.table_pattern'[, 'db_pattern.table_pattern'][, ...] db_pair_list:
(db_pair)[, (db_pair)][, ...] db_pair:
from_db, to_db
# 设置只同步db1,db2这2个库:(要先停止SQL线程)
使用命令很简单:CHANGE REPLICATION FILTER REPLICATE_DO_DB = (db1, db2); # 要是又要全部的库都要同步该如何操作呢,也很简单。
STOP SLAVE SQL_THREAD;
CHANGE REPLICATION FILTER REPLICATE_DO_DB = ();
start SLAVE SQL_THREAD; # 置空多个过滤设置
CHANGE REPLICATION FILTER REPLICATE_REWRITE_DB=(),REPLICATE_WILD_DO_TABLE=(); # 同样可以可以设置只同步某个库下面的某张表,或者不同步某个库下面的某张表。看命令。
mysql> CHANGE REPLICATION FILTER
-> REPLICATE_WILD_DO_TABLE = ('db1.t1%'),
-> REPLICATE_WILD_IGNORE_TABLE = ('db1.t2%'); # 设置同时同步1个库下面的某些表可以这样写:
mysql> CHANGE REPLICATION FILTER
-> REPLICATE_WILD_DO_TABLE = ('db2.t1%','db2.t2%'); # 设置重写DB:(注意需要加2个括号,否则语法报错,2个括号是因为可以同时设置多对重写规则)
CHANGE REPLICATION FILTER REPLICATE_REWRITE_DB = ((db_activity,db_webadmin),(db_a,db_b)); 

附2:MySQL复制过滤流程

参考官网:https://dev.mysql.com/doc/refman/5.7/en/replication-rules-db-options.html

在复制过滤中,有这样一条规则:

Note:
Only DML statements can be logged using the row format. DDL statements are always logged as statements, even when binlog_format=ROW.
All DDL statements are therefore always filtered according to the rules for statement-based replication.
This means that you must select the default database explicitly with a USE statement in order for a DDL statement to be applied.

意思是说,DDL语句永远是以 statement-based 的格式记录在binlog中的(即使 binlog_format=ROW)。在复制过滤中,要使 DDL 在设置复制过滤的从库生效,必须显示指定数据库(使用 USE db 语句)。

由此,小编做了一个测试,A B C 三个实例,B是A的从库,C是设置了复制过滤的A的从库。在A执行语句:

ALTER TABLE `db1`.`tb1` DROP INDEX `idx_abc`, ADD INDEX `idx_abc` (`a`,`b`,`c`);

A的binglog如下:
use `test`/*!*/;
SET TIMESTAMP=1566374584/*!*/;
SET @@session.sql_mode=0/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=45/*!*/;
ALTER TABLE `db1`.`tb1`
DROP INDEX `idx_abc`,
ADD INDEX `idx_abc` (`a`,`b`,`c`)
/*!*/;

可以看到,在未显示 USE db 的情况下,MySQL使用了默认的test库。结果是:B节点正确复制了该语句,C节点却没有。在C的error log中找到如下错误信息:

2019-08-21T16:03:04.376974+08:00 144172 [Note] Slave SQL for channel '': Worker 1 failed executing transaction 'ANONYMOUS' at master log mysql-bin.616178, end_log_pos 31881792;
Could not execute Query event. Detailed error: Slave SQL thread ignored the query because of replicate-*-table rules; Error log throttle is enabled.
This error will not be displayed for next 60 secs. It will be suppressed, Error_code: 1237

(一)对于 Database-Level Replication,MySQL的处理流程如下:

1. Which logging format is used?

  • STATEMENT. Test the default database.
  • ROW. Test the database affected by the changes.

2. Are there any --replicate-do-db options?

  • Yes. Does the database match any of them?

    •   Yes. Continue to Step 4.
    •   No. Ignore the update and exit.
  • No. Continue to step 3.

3.  Are there any --replicate-ignore-db options?

  • Yes. Does the database match any of them?

    •   Yes. Ignore the update and exit.
    •   No. Continue to step 4.
  • No. Continue to step 4.

4. Proceed to checking the table-level replication options, if there are any. For a description of how these options are checked, see Section 16.2.5.2, “Evaluation of Table-Level Replication Options”.

(二)对于 Table-Level Replication,MySQL的处理流程如下:

1. Are there any table replication options?

  • Yes. Continue to step 2.
  • No. Execute the update and exit.

2. Which logging format is used?

  • STATEMENT. Carry out the remaining steps for each statement that performs an update.
  • ROW. Carry out the remaining steps for each update of a table row.

3. Are there any --replicate-do-table options?

  • Yes. Does the table match any of them?

    •   Yes. Execute the update and exit.
    •   No. Continue to step 4.
  • No. Continue to step 4.

4. Are there any --replicate-ignore-table options?

  • Yes. Does the table match any of them?

    •   Yes. Ignore the update and exit.
    •   No. Continue to step 5.
  • No. Continue to step 5.

5. Are there any --replicate-wild-do-table options?

  • Yes. Does the table match any of them?

    •   Yes. Execute the update and exit.
    •   No. Continue to step 6.
  • No. Continue to step 6.

6. Are there any --replicate-wild-ignore-table options?

  • Yes. Does the table match any of them?

    •   Yes. Ignore the update and exit.
    •   No. Continue to step 7.
  • No. Continue to step 7.

7. Is there another table to be tested?

  • Yes. Go back to step 3.
  • No. Continue to step 8.

8. Are there any --replicate-do-table or --replicate-wild-do-table options?

  • Yes. Ignore the update and exit.
  • No. Execute the update and exit.

注意:如果一张表同时被设置了  --replicate-do-table and --replicate-wild-do-table option, 或 --replicate-ignore-table and --replicate-wild-ignore-table ,那么只有 binlog_format=ROW 的 DML 语句可以被复制到从库。

MySQL 过滤复制+复制映射 配置方法的更多相关文章

  1. mysql 5.7.14 安装配置方法图文教程(转)

    http://www.jb51.net/article/90259.htm ******************************** 因笔者个人需要需要在本机安装Mysql,先将安装过程记录如 ...

  2. mysql 5.7.13 安装配置方法图文教程(win10) (转)

    http://www.jb51.net/article/87152.htm ***************************** MySQL是一款关系型数据库管理系统,是由Oracle旗下公司M ...

  3. mysql 5.7.15 安装配置方法图文教程(转)

    http://www.jb51.net/article/92521.htm ******************************* MySQL数据库作为关系型数据库中的佼佼者,因其体积小,速度 ...

  4. mysql 5.7.13 安装配置方法(linux)-后期部分运维

    mysql 5.7.13 安装配置方法图文教程(linux) 学习了:https://www.cnblogs.com/zhao1949/p/5947938.html /usr/local/mysql是 ...

  5. [转]IIS6 伪静态 IIS文件类型映射配置方法 【图解】

    1.右键点击 要设置网站的网站 2.属性 -->主目录 -->配置--> 3.如右侧窗口,找到 .aspx 扩展名-->编辑-->复制 可执行文件的路径-->关闭 ...

  6. IIS6 伪静态 IIS文件类型映射配置方法 【图解】

    1.右键点击 要设置网站的网站 2.属性 -->主目录 -->配置--> 3.如右侧窗口,找到 .aspx 扩展名-->编辑-->复制 可执行文件的路径-->关闭 ...

  7. Docker Mysql数据库双主同步配置方法

    一.背景 可先查看第一篇<Docker Mysql数据库主从同步配置方法>介绍 二.具体操作 1.创建目录(~/test/mysql_test1): --mysql --mone --da ...

  8. MySql互为主从配置文件及配置方法

    # Example MySQL config file for medium systems. # # This is for a system with little memory (32M - 6 ...

  9. MySQL主从复制的原理及配置方法(比较详细)

    MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题 一.复制的原理 MySQL 复制基于主服务 ...

随机推荐

  1. declare/typeset

    用来生命变量的,作用完全一样. 不像C语言那样严谨的语法,变量在使用前必须声明. 但是在shell中对变量的声明要求并不高,因为shell弱化了变量的类概念,所以shell被称为弱类型语言, 声明变量 ...

  2. noi.ac #38 线段树+时间复杂度分析

    \(des\) 存在参数数组 \(a\),\(a\) 升序排列 \[a_1 < a_2 < \cdots < a_m, m <= 10\] 存在长度为 \(n\) 价值数组 \ ...

  3. (4)Angular的开发

    angular框架,库,是一款非常优秀的前端高级JS框架,有了这个框架就可以轻松构建SPA应用程序,通过指令宽展了HTML,通过表达式绑定数据到HTML. 轻松构建SPA应用程序,单一页面应用程序 h ...

  4. std_msgs/String.msg

    from std_msgs.msg import String http://docs.ros.org/api/std_msgs/html/msg/String.html

  5. 性能测试学习第八天-----linux环境整合篇

  6. mysql集群高可用架构

    前言 高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用.对于一个系统而言,可能包含很多模块,比如前端应用,缓存,数据库,搜索,消息队列等,每个模块都需要做到高可用,才能 ...

  7. python定制后处理云图

    用后处理软件处理的云图会出现这样或那样的不满意,其实我们可以将求解数据导出以后,借助python定制云图. 我们以fluent为例 求解完成之后,我们将我们需要做云图的物理量以ASCII导出 如下的p ...

  8. 关于Vertical Align的理解

    1:vertical-align 翻译就是垂直-对齐... 2:关于line-height的点 2.1:如果一个标签没有定义height属性,那么其最终表现的高度一定是由line-height起作用. ...

  9. 简要介绍 X Window System (又称为X11 or X)

    X11是一个应用程序,而不是一个操作系统,主要功能是提供图形界面,实现架构和网络架构相同,有X Client和X Server组件,另外还有Window Manager和Display Manager ...

  10. load、loads和 dump、dumps的区别

    相同点 load 和loads 都是实现“反序列化” 区别 1.loadsloads针对内存对象loads: 将 字符串 转换为 字典 # 这是一个字符串'{"b": 2, &qu ...