MySQL系列详解十:MySQL多源复制演示-技术流ken

前言

多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面。至于为什么要做多源复制下面的总结很到位。

1、灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了),还有最后一个救命稻草;
2、备份:直接在这个从库中做备份,不影响线上的数据库;
3、减少成本:不需要每个库都做一个实例,也减少了DBA的维护成本;
4、数据统计:后期的一些数据统计,需要将所有的库汇总在一起。
 

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 =
expire_logs_days = 10
...

检查主2服务服务器端id

[root@ken ~]# vim /etc/my.cnf
...
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id =
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
...

检查从服务服务器端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 | |
| sync_relay_log_info | |
+--------------------------------+----------------+
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
*************************** . row ***************************
File: mysql-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
row in set (0.00 sec)

主2 服务器

MySQL [(none)]> show master status\G
*************************** . row ***************************
File: mysql-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
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
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.137 <<主1连接成功
Master_User: ken
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin-m1.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 987ac782-d7b8-11e8-a462-000c292218ec
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
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:-,
987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name: m1
Master_TLS_Version:
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.138 <<主2连接成功
Master_User: ken
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin-m2.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: c01b1811-d7b3-11e8--000c29492f7b
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
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:-,
987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name: m2
Master_TLS_Version:
rows in set (0.00 sec)

测试

下面我们就分别在主1以及主2上面建立库和表,检查是否可以都同步到从节点上

主1服务器

在主1服务器上面建立一个ken的数据库,并在里面创建一张ken1的表,并插入一些数据等待验证

mysql> create database ken;
Query OK, row affected (0.00 sec) mysql> use ken;
Database changed
mysql> create table ken1(id int);
Query OK, rows affected (0.02 sec) mysql> insert into ken1 values();
Query OK, row affected (0.05 sec) mysql> select * from ken1;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)

主2服务器

在主2服务器上面建立一个ken6的数据库,并在里面创建一张ken的表,并插入一些数据等待验证

MySQL [(none)]>  create database ken6;
Query OK, row affected (0.00 sec) MySQL [(none)]> use ken5;
Database changed
MySQL [ken6]> create table ken(id int);
Query OK, rows affected (0.01 sec) MySQL [ken6]> insert into ken values();
Query OK, row affected (0.05 sec) MySQL [ken6]> select * from ken;
+------+
| id |
+------+
| |
+------+
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 |
+--------------------+
rows in set (0.00 sec)

查看主1同步过来数据

MySQL [(none)]> select * from ken.ken1;
+------+
| id |
+------+
| |
+------+

查看主2同步过来的数据

MySQL [ken5]> select * from ken5.ken;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)

至此,多源复制的架构就完成了。

如果要想清除slave status可以先停掉同步,再执行reset slave all;即可

MySQL [ken5]> stop slave;
Query OK, rows affected (0.01 sec) MySQL [ken5]> reset slave all;
Query OK, rows affected (0.00 sec) MySQL [ken5]> show slave status\G
Empty set (0.01 sec)
上一篇:StackExchange.Redis通用封装类分享


下一篇:aes加密在linux下会生成随机key的解决办法