MySQL级联复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL级联复制相关的知识,希望对你有一定的参考价值。
mysql级联复制在生产换进中有一种主从复制的方法主节点先将数据同步到一个中间的从节点,然后由从节点给后续的其他从节点来复制数据,这种复制方式称为级联复制。
级联复制的好处是可以极大的减轻主节点的压力
级联复制在配置时需要在中间节点上启用log_slave_updates的选项。
级联复制的配置方法
准备主机4台,1台主节点(Master),1台中间从节点(Slave),1台从节点(Slave1)
主机 | 系统 | ip |
---|---|---|
Master | CentOS7 | 192.168.73.110 |
Slave | CentOS7 | 192.168.73.111 |
Slave1 | CentOS7 | 192.168.73.112 |
主节点配置(Master)
1.修改配置文件
[mysqld]
log-bin=/data/bin/mysql-bin #启动二进制日志
binlog-format=row #修改日志格式
server-id=1
2.创建二级制日志目录
[[email protected] ~]# mkdir /data/bin
[[email protected] ~]# chown -R mysql.mysql /data/bin
3.启动服务,查看当前二进制日志所在的位置
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql -e "SHOW MASTER LOGS;"
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 26753 |
| mysql-bin.000002 | 921736 |
| mysql-bin.000003 | 245 | #记录当前二进制的位置
+------------------+-----------+
4.创建一个用来复制数据的账户
[[email protected] ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘192.168.73.%‘ IDENTIFIED BY ‘centos‘;"
中间从节点配置
1.修改配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
read-only
log_slave_updates
server-id=2
2.创建二进制日志目录
[[email protected] ~]# mkdir /data/bin
[[email protected] ~]# chown -R mysql.mysql /data/bin
3.启动MySQL服务
[[email protected] ~]# systemctl start mariadb
4.写入CHANGE MASTER TO信息
CHANGE MASTER TO
MASTER_HOST=‘192.168.73.110‘,
MASTER_USER=‘repluser‘,
MASTER_PASSWORD=‘centos‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘mysql-bin.000003‘,
MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
5.查看下从节点的配置状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.73.110
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No #线程尚未开启
Slave_SQL_Running: No #线程尚未开启
6.启动线程
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
7.再次查看从节点状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.73.110
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 402 #已经有小部分数据被复制过来
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 686
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes #线程已经启动
Slave_SQL_Running: Yes #线程已经启动
8.测试
8.1在主节点导入数据库
[[email protected] ~]# mysql < hellodb_innodb.sql
[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
8.2从节点查看是否已经同步
[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
配置Slave1
由于此时各节点上已经有数据,作为后来追加的从服务器,首先需要将之前的所有数据使用备份恢复一次然后再进行主从复制进行同步
1.在中间节点上将数据库备份出来,并将数据传送到后续的Slave1主机上
[[email protected] ~]# mysqldump -A --single-transaction -F --master-data=1 > /data/all.sql
[[email protected] ~]# scp /data/all.sql 192.168.73.112:/data
2.在Slave1主机上修改配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
read-only
server-id=3
3.启动MySQL服务
[[email protected] ~]# systemctl start mariadb
4.对备份文件做修改
找到CHANGE MASTER TO行对信息加以修改
[[email protected] ~]# vim /data/all.sql
CHANGE MASTER TO MASTER_HOST=‘192.168.73.111‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘centos‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-bin.000004‘, MASTER_LOG_POS=245;
5.将修改后的备份文件导入数据库
[[email protected] ~]# mysql < /data/all.sql
6.查看下slave status
[[email protected] ~]# mysql -e "SHOW SLAVE STATUS\G;"
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.73.111
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
7.启动线程
[[email protected] ~]# mysql -e "START SLAVE;"
8.再次查看slave status
[[email protected] ~]# mysql -e "show slave status\G;"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.73.111
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes #线程已经全部启动
Slave_SQL_Running: Yes
测试
在主节点上删除hellodb库中的teachers表
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #先查看下库是否有teachers表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
[[email protected] ~]# mysql -e "DROP TABLE hellodb.teachers;" #删表
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #再次确认表是否删除
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| toc |
+-------------------+
在slave1节点上查看是否同步
[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb;" #查看从节点是否还存在teachers表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| toc |
+-------------------+
级联复制配置成功
以上是关于MySQL级联复制的主要内容,如果未能解决你的问题,请参考以下文章