MySQL系列详解十:MySQL多源复制演示-技术流ken
前言
多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面。至于为什么要做多源复制下面的总结很到位。
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 = STATEMENT binlog_format = row server-id = 1003307 log-bin = /data/mysql/mysql3306/logs/mysql-bin binlog_cache_size = 4M max_binlog_size = 256M max_binlog_cache_size = 1M sync_binlog = 0 expire_logs_days = 10
...
检查主2服务服务器端id
[root@ken ~]# vim /etc/my.cnf ... #binlog #binlog_format = STATEMENT binlog_format = row server-id = 1003308 log-bin = /data/mysql/mysql3306/logs/mysql-bin binlog_cache_size = 4M max_binlog_size = 256M max_binlog_cache_size = 1M sync_binlog = 0 expire_logs_days = 10 ...
检查从服务服务器端id
[root@ken ~]# vim /etc/my.cnf
...
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 1003309
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_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-7 1 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-7 1 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连接成功 Master_User: ken Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 234 Relay_Log_File: relay-bin-m1.000003 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 234 Relay_Log_Space: 690 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: 1003306 Master_UUID: 987ac782-d7b8-11e8-a462-000c292218ec 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: 19e7075d-d7d2-11e8-8b2d-000c29629b02:1-889, 987ac782-d7b8-11e8-a462-000c292218ec:1-16, c01b1811-d7b3-11e8-8698-000c29492f7b:1-7 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: m1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.220.5.138 <<主2连接成功 Master_User: ken Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000009 Read_Master_Log_Pos: 234 Relay_Log_File: relay-bin-m2.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000009 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 234 Relay_Log_Space: 524 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: 1003307 Master_UUID: c01b1811-d7b3-11e8-8698-000c29492f7b 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: 19e7075d-d7d2-11e8-8b2d-000c29629b02:1-889, 987ac782-d7b8-11e8-a462-000c292218ec:1-16, c01b1811-d7b3-11e8-8698-000c29492f7b:1-7 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: m2 Master_TLS_Version: 2 rows in set (0.00 sec)
测试
下面我们就分别在主1以及主2上面建立库和表,检查是否可以都同步到从节点上
主1服务器
在主1服务器上面建立一个ken的数据库,并在里面创建一张ken1的表,并插入一些数据等待验证
mysql> create database ken; Query OK, 1 row affected (0.00 sec) mysql> use ken; Database changed mysql> 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 changed MySQL [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\G Empty set (0.01 sec)