前言
多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面。至于为什么要做多源复制下面的总结很到位。
MySQL多源复制演示
准备
主1服务器端:10.220.5.137
主2服务器端:10.220.5.138
从服务器端:10.220.5.139
配置两个主服务端
首先要保证三个节点中的server_id不一致,检查各个服务器端server_id
检查主1服务端id
[root@ken ~]# vim /etc/my.cnf ... #binlog#binlog_format = STATEMENTbinlog_format = rowserver-id = 1003307 log-bin = /data/mysql/mysql3306/logs/mysql-binbinlog_cache_size = 4Mmax_binlog_size = 256Mmax_binlog_cache_size = 1Msync_binlog = 0expire_logs_days = 10 ...
检查主2服务服务器端id
[root@ken ~]# vim /etc/my.cnf...#binlog#binlog_format = STATEMENTbinlog_format = rowserver-id = 1003308 log-bin = /data/mysql/mysql3306/logs/mysql-binbinlog_cache_size = 4Mmax_binlog_size = 256Mmax_binlog_cache_size = 1Msync_binlog = 0expire_logs_days = 10...
检查从服务服务器端id
[root@ken ~]# vim /etc/my.cnf...#binlog#binlog_format = STATEMENTbinlog_format = rowserver-id = 1003309 log-bin = /data/mysql/mysql3306/logs/mysql-binbinlog_cache_size = 4M max_binlog_size = 256M max_binlog_cache_size = 1M sync_binlog = 0 expire_logs_days = 10 ...
在两个主服务器端建立用于复制的用户
mysql> grant replication slave on *.* to 'ken'@'%' identified by 'xx';
这样两个主服务器端就已经配置完成了,现在来配置从服务器端
配置从服务器端
检查从段的master_info_repository以及relay_log_info_repository
MySQL [(none)]> show global variables like '%info%';+--------------------------------+----------------+| Variable_name | Value |+--------------------------------+----------------+| master_info_repository | FILE || relay_log_info_file | relay-log.info || relay_log_info_repository | FILE || session_track_transaction_info | OFF || sync_master_info | 10000 || sync_relay_log_info | 10000 |+--------------------------------+----------------+6 rows in set (0.01 sec)
修改从段的master_info_repository以及relay_log_info_repository 为table
MySQL [(none)]> set global master_info_repository = 'table';MySQL [(none)]> set global relay_log_info_repository = 'table';
从端与主1建立连接
首先需要查看主1以及主2服务器的二进制日志位置以便开始复制
主1服务器
mysql> show master status\G*************************** 1. row *************************** File: mysql-bin.000014 Position: 234 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-16,c01b1811-d7b3-11e8-8698-000c29492f7b:3-71 row in set (0.00 sec)
主2 服务器
MySQL [(none)]> show master status\G*************************** 1. row *************************** File: mysql-bin.000009 Position: 234 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-16,c01b1811-d7b3-11e8-8698-000c29492f7b:1-71 row in set (0.00 sec)
与主1建立连接
###注意:
在master_log_pos=234,后面这个起始数字不能加单引号或者双引号,否则会报错
change master to master_host='10.220.5.137',master_user='ken',master_password='xx',master_log_file='mysql-bin.000014',master_log_pos=234 for channel 'm1';
与主2建立连接
change master to master_host='10.220.5.138',master_user='ken',master_password='xx',master_log_file='mysql-bin.000009',master_log_pos=234 for channel 'm2';
启动slave
MySQL [(none)]> start slave;
查看连接状态
MySQL [(none)]> show slave status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.220.5.137 <
测试
下面我们就分别在主1以及主2上面建立库和表,检查是否可以都同步到从节点上
主1服务器
在主1服务器上面建立一个ken的数据库,并在里面创建一张ken1的表,并插入一些数据等待验证
mysql> create database ken;Query OK, 1 row affected (0.00 sec)mysql> use ken;Database changedmysql> create table ken1(id int);Query OK, 0 rows affected (0.02 sec)mysql> insert into ken1 values(1);Query OK, 1 row affected (0.05 sec)mysql> select * from ken1;+------+| id |+------+| 1 |+------+1 row in set (0.00 sec)
主2服务器
在主2服务器上面建立一个ken6的数据库,并在里面创建一张ken的表,并插入一些数据等待验证
MySQL [(none)]> create database ken6;Query OK, 1 row affected (0.00 sec)MySQL [(none)]> use ken5;Database changedMySQL [ken6]> create table ken(id int);Query OK, 0 rows affected (0.01 sec)MySQL [ken6]> insert into ken values(1);Query OK, 1 row affected (0.05 sec)MySQL [ken6]> select * from ken;+------+| id |+------+| 1 |+------+1 row in set (0.00 sec)
从服务器端
查看是否有主1以及主2新建的库表即数据
可以看到下面已经同步过来了主1上面的ken数据库以及主2上面的ken5
MySQL [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || ken || ken5 || mysql || performance_schema || sys || test |+--------------------+7 rows in set (0.00 sec)
查看主1同步过来数据
MySQL [(none)]> select * from ken.ken1;+------+| id |+------+| 1 |+------+
查看主2同步过来的数据
MySQL [ken5]> select * from ken5.ken;+------+| id |+------+| 1 |+------+1 row in set (0.00 sec)
至此,多源复制的架构就完成了。
如果要想清除slave status可以先停掉同步,再执行reset slave all;即可
MySQL [ken5]> stop slave;Query OK, 0 rows affected (0.01 sec)MySQL [ken5]> reset slave all;Query OK, 0 rows affected (0.00 sec)MySQL [ken5]> show slave status\GEmpty set (0.01 sec)