MySQL5.7多源复制

MySQL5.7开始支持多源复制,也就是多主一从的复制架构:

使用多源复制的考虑:

1、灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了),还有最后一个救命稻草;

2、备份:直接在这个从库中做备份,不影响线上的数据库;

3、减少成本:不需要每个库都做一个实例,也减少了DBA的维护成本;

4、数据统计:后期的一些数据统计,需要将所有的库汇总在一起。

... ...

实验环境:

Master1:10.157.26.132:3307

Master2:10.157.26.133:3307

Slave:10.157.26.134:3307

多源复制是支持GTID和Binlog+Position,我这里是GTID复制。

MySQL5.7的安装这里不再介绍,可以查看http://www.cnblogs.com/lizhi221/p/6813907.html

一、配置文件:

Master1和Master2:

#GTID
gtid-mode = on
binlog_gtid_simple_recovery=1
enforce_gtid_consistency=1
binlog_format = row
skip_slave_start = 1
log-bin = /data/mysql/mysql_3307/logs/binlog/mysql-bin

Slave:

#binlog
binlog_format = row
server-id = 1343307
log-bin = /data/mysql/mysql_3307/logs/binlog/mysql-bin
#GTID
gtid-mode = on
binlog_gtid_simple_recovery=1
enforce_gtid_consistency=1
#修改MySQL存储master-info和relay-info的方式,即从文件存储改为表存储
master_info_repository=TABLE
relay_log_info_repository=TABLE
replicate_ignore_db=mysql  #忽略mysql库的同步
skip_slave_start = 1

二、主库创建复制账号:

Master1:grant replication slave,replication client on *.* to [email protected]‘10.157.26.%‘identified by ‘sampson_132‘;
Master2:grant replication slave,replication client on *.* to [email protected]‘10.157.26.%‘identified by ‘sampson_133‘;

三:从库启动复制:

mysql> change master to master_host=‘10.157.26.132‘,master_port=3307,master_user=‘sampson_132‘,master_password=‘sampson_132‘,master_auto_position=1 for channel ‘Master_132‘;
mysql>change master to master_host=‘10.157.26.133‘,master_port=3307,master_user=‘sampson_133‘,master_password=‘sampson_133‘,master_auto_position=1 for channel ‘Master_133‘;
mysql>start slave;

也可以start slave for channel ‘Master_132 ‘启动单个channel的复制。

四:查看slave状态

【也可以直接show slave status for channel ‘Master_132‘ \G查看各个channel的复制状态】

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.157.26.132
                  Master_User: sampson_132
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay_log-master_132.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 579
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1323307
                  Master_UUID: 8785129a-3ace-11e7-9f13-fa163e48cafd
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master_132
           Master_TLS_Version:
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.157.26.133
                  Master_User: sampson_133
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay_log-master_133.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 579
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1333307
                  Master_UUID: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master_133
           Master_TLS_Version:
2 rows in set (0.01 sec)

通过查看performance_schema相关的表查看同步状态:

mysql> select * from performance_schema.replication_connection_status\G
*************************** 1. row ***************************
             CHANNEL_NAME: master_132
               GROUP_NAME:
              SOURCE_UUID: 8785129a-3ace-11e7-9f13-fa163e48cafd
                THREAD_ID: 89
            SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 1
 LAST_HEARTBEAT_TIMESTAMP: 2017-05-17 16:59:45
 RECEIVED_TRANSACTION_SET:
        LAST_ERROR_NUMBER: 0
       LAST_ERROR_MESSAGE:
     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row ***************************
             CHANNEL_NAME: master_133
               GROUP_NAME:
              SOURCE_UUID: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad
                THREAD_ID: 91
            SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 1
 LAST_HEARTBEAT_TIMESTAMP: 2017-05-17 16:59:45
 RECEIVED_TRANSACTION_SET:
        LAST_ERROR_NUMBER: 0
       LAST_ERROR_MESSAGE:
     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
2 rows in set (0.01 sec)

五、数据同步检查:

Master1建库建表并插入数据:

mysql>CREATE DATABASE /*!32312 IF NOT EXISTS*/ `sampson_132` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
mysql>use sampson_132
mysql> create table t_132(id int primary key auto_increment,name varchar(20) not null);
mysql> insert into t_132(id,name)values(1,‘132‘),(2,‘132‘),(3,‘132‘);
mysql> select * from t_132;
+----+------+
| id | name |
+----+------+
|  1 | 132  |
|  2 | 132  |
|  3 | 132  |
+----+------+
3 rows in set (0.00 sec)

Master2建库建表并插入数据:

mysql> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `sampson_133` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
mysql> use sampson_133
mysql> create table t_133(id int primary key auto_increment,name varchar(20) not null);
mysql> insert into t_133(id,name)values(1,‘133‘),(2,‘133‘),(3,‘133‘);
mysql> select * from sampson_133.t_133;
+----+------+
| id | name |
+----+------+
|  1 | 133  |
|  2 | 133  |
|  3 | 133  |
+----+------+
3 rows in set (0.00 sec)

Slave查看数据是否同步:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sampson_132        |
| sampson_133        |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from sampson_132.t_132;
+----+------+
| id | name |
+----+------+
|  1 | 132  |
|  2 | 132  |
|  3 | 132  |
+----+------+
3 rows in set (0.00 sec)

mysql> select * from sampson_133.t_133;
+----+------+
| id | name |
+----+------+
|  1 | 133  |
|  2 | 133  |
|  3 | 133  |
+----+------+
3 rows in set (0.00 sec)

再次查看从库状态:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.157.26.132
                  Master_User: sampson_132
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 930
               Relay_Log_File: relay_log-master_132.000002
                Relay_Log_Pos: 1143
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 930
              Relay_Log_Space: 1355
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1323307
                  Master_UUID: 8785129a-3ace-11e7-9f13-fa163e48cafd
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 8785129a-3ace-11e7-9f13-fa163e48cafd:1-3
            Executed_Gtid_Set: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad:1-3,
8785129a-3ace-11e7-9f13-fa163e48cafd:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master_132
           Master_TLS_Version:
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.157.26.133
                  Master_User: sampson_133
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 930
               Relay_Log_File: relay_log-master_133.000002
                Relay_Log_Pos: 1143
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 930
              Relay_Log_Space: 1355
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1333307
                  Master_UUID: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad:1-3
            Executed_Gtid_Set: 3d5ae510-3acf-11e7-82a4-fa163e8e91ad:1-3,
8785129a-3ace-11e7-9f13-fa163e48cafd:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master_133
           Master_TLS_Version:
2 rows in set (0.00 sec)
时间: 2024-12-28 17:55:23

MySQL5.7多源复制的相关文章

MySQL5.7多源复制的实验

MySQL5.7多源复制的实验 node1: 192.168.2.171     master1 node2: 192.168.2.172     slave node3: 192.168.2.170     master2 node2上执行: change master to master_host='192.168.2.171', master_user='rpl', master_password='[email protected]', master_port=3306, master_

MySQL5.7多源复制实践

MySQL5.7开始新增多源复制功能,即允许一个salve同时复制多个主库的事务,slave会创建通往每个主库的管道.多源复制在应用来自多个源的事务的时候不会对有冲突的事务进行检测. 配置实现 主库支持基于GTID和binlog position的配置:从库存储master信息的仓库需要为table: #在配置文件中加入master-info-repository=TABLE relay-log-info-repository=TABLE#在线更改 STOP SLAVE; SET GLOBAL

Mysql5.7多源复制,过滤复制一段时间后增加复制一个库的实现方法

多源复制如果是整个实例级别的复制,那不存在下面描述的情况. 如果是对其中一个或多个主实例都是过滤复制,并且运行一段时间后,想在这个源上再增加一个库怎么实现? 主1:192.168.1.10 3306     db1     db2 主2:192.168.1.20 3306     db3 从:  192.168.1.30 3306 假如之前实现的是过滤复制,复制的是主1的db1 和 主2的db3,已经运行一段时间了.现在想增加主1上的db2,该如何实现? 具体实现方法如下: 1. 使用mysql

MySQL多源复制(八)

一.什么是多源复制 MySQL 5.7发布后,在复制方面有了很大的改进和提升.比如开始支持多源复制(multi-source)以及真正的支持多线程复制了.多源复制可以使用基于二进制日子的复制或者基于事务的复制.下面我们说一说如何配置基于二进制日志的多源复制. 首先,我们要清楚几种常见的复制模式: 一主一从 一主多从 级联复制 multi-master MySQL 5.7 之前只能支持一主一从,一主多从或者多主多从的复制.如果想实现多主一从的复制 只能使用mariadb,但是mariadb又与官方

烂泥:mysql5.0数据库源码编译安装

本文首发于烂泥行天下. 本次实验的mysql.OS相关信息如下: Mysql:5.0.96 OS:centos 32 bit 最近公司要上新的业务,指明数据库使用的是mysql5.0版本.如果是使用rpm包安装的话,那就很简单了.直接使用yum安装即可,命令如下: yum –y install mysql mysql-server Yum方式安装完毕后,直接启动mysql数据库服务即可.如下图: 这样基本上就可以了. 但是这样安装mysql数据库,没有进行定制.比如mysql数据库的数据文件存储

mysql 多源复制

mysql5.7新特性多源复制很实用,方便对分库环境进行汇总,集中备份和数据统计分析. 我的实验环境3台机器,mysql版本:5.7.16 10.10.203.102 从库 10.10.203.93 主库 10.10.203.94 主库 操作步骤: (1)当一个从库是多源复制结构,那么master_info和relay_log_info不能用file方式来存储,必须使用table,它不支持file. 10.10.203.102 从库上操作 在配置文件中新增: [mysqld] master_in

MySQL多源复制搭建

1.1     实验概要 1.1.1  实验假设 本实验假设已经完成操作系统和MySQL安装部署. 1.1.2  实验目的 MySQL5.7的多源复制技术搭建部署,然后简单测试. 1.1.3  环境信息 操作系统 MySQL版本 服务器地址 服务器角色 Centos7 5.7.18 192.168.102.23 source 1 Centos7 5.7.18 192.168.102.24 source 2 Centos7 5.7.18 192.168.102.25 target 1.1.4  实

mysql-5.5.28源码安装过程中错误总结

介绍一下关于mysql-5.5.28源码安装过程中几大错误总结,希望此文章对各位同学有所帮助.系统centOS 6.3 mini (没有任何编译环境)预编译环境首先装了众所周知的 cmake(yum install cmake -y) 复制代码 代码如下: ../bootstrap Error when bootstrapping CMake: Cannot find appropriate C compiler on this system. Please specify one using

mariadb-10GTID复制及多源复制

---本文大纲 一.什么是GTID 二.应用场景 三.多线程复制说明 四.实现过程 五.多源复制原理 六.实现过程 ---------------------------------- 一.什么是GITD 自MySQL 5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置.监控及管理变得更加易于实现,且更加健壮.官方文档在这篇文档里,我们可以知道全局事务 ID 的官方定义是:GTID = source_id:transaction_id gtid 是一个 un