配置 MySQL 多源复制,主要是需要在 MySQL 从服务器的主配置文件 [mysqld] 段中添加以下两行:
$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
master-info-repository = table
relay-log-info-repository = table
MySQL主服务器配置片断
以 dev-master-01 为例,另一台 Master 也是类似的配置方法.
$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
server-id = 1
log-bin = /var/log/mysql/mysql-bin
log_bin_index = /var/log/mysql/mysql-bin.index
expire_logs_days = 30
max_binlog_size ?= 100M
binlog_format = ROW
MySQL从服务器配置片断
$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
server-id = 3
log-slave-updates = true
skip-slave-start = true
expire_logs_days = 30
max_binlog_size ?= 100M
log-bin = /var/log/mysql/mysql-bin
relay-log = /var/log/mysql/relay-log
relay-log-index = /var/log/mysql/relay-log-index
relay-log-info-file = /var/log/mysql/relay-log.info
master-info-repository = table
relay-log-info-repository = table
report-port = 3306
report-host = 192.168.2.212
replicate-do-db = master1
replicate-do-db = master2
replicate_wild_do_table=master1.%
replicate_wild_do_table=master2.%
注:server-id 每台必须配置为不一样,比如 dev-master-01 为1,dev-node-01 为2,dev-node-02 为3.这里没有给出全部配置,其它请根据实际情况自行配置.
$ service mysql restart
在两台 MySQL Master 上创建
mysql> grant replication slave on *.* to 'repl'@'192.168.2.%' identified by '000000';
mysql> flush privileges;
MySQL 5.7 有了通信渠道的概念,每一个通信渠道都是一个从服务器到主服务器获得二进制日志的链接.这意味着每个通信渠道都得有一个 IO_THREAD .对于每一个主服务器,我们需要运行不同的 CHANGE MASTER 命令和FOR CHANNEL 这个参数来分别提供不同通信链接名字.
下面开始设置需要同步的源,同步两个主服务器的数据到从服务器上.
设置同步源到 Master1 (在 MySQL 从服务器上执行)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.210',MASTER_USER='repl',MASTER_PORT=3306,MASTER_PASSWORD='000000',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=1 FOR CHANNEL 'master1';
设置同步源到 Master2 (在 MySQL 从服务器上执行)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.211',MASTER_LOG_POS=1 FOR CHANNEL 'master2';
启动所有SLAVE
mysql> START SLAVE;
也可以单独启动需要同步的通道.
mysql> START SLAVE FOR CHANNEL 'master1';
mysql> START SLAVE FOR CHANNEL 'master2';
停止和 RESET 复制的命令也同 START 类似,可以操作所有的,也可以操作单个通道.
查看SLAVE信息
mysql> SHOW SLAVE STATUS\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
确认 Slave_IO_Running 和 Slave_SQL_Running 两个参数都为 Yes 状态.
如果要查看单一信道的复制的详细状态,可以使用以下命令:
mysql> SHOW SLAVE STATUS FOR CHANNEL 'master1'\G;
测试多源复制
- 在主库(dev-master-01)实例创建一些数据.
mysql> create database master1;
mysql> use master1;
mysql> CREATE TABLE `test1` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);
mysql> insert into test1 values(1,1);
- 在主库(dev-node-01)实例创建一些数据.
mysql> create database master2;
mysql> use master2;
mysql> CREATE TABLE `test2` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);
mysql> insert into test2 values(1,1);
- 在从库(dev-node-02)实例检查数据是否成功复制.
mysql> select * from master1.test1;
+------+-------+
| id ? | count |
+------+-------+
| ? ?1 | ? ? 1 |
+------+-------+
1 row in set (0.00 sec)
mysql> select * from master2.test2;
+------+-------+
| id ? | count |
+------+-------+
| ? ?1 | ? ? 1 |
+------+-------+
1 row in set (0.00 sec)
列出所有的复制信道的复制状态概况:
(编辑:ASP站长网)
|