所谓级联复制就是master服务器,只给一台slave服务器同步数据,然后slave服务器在向后端的所有slave服务器同步数据,降低master服务器的写压力,和复制数据的网络IO。
一,配置master服务器
1,修改主配置文件
vim /etc/my.cnf
在[mysql]配置块下添加如下两行配置
[mysql]
log_bin #开启二进制日志功能
server_id=1 #为当前节点设置一个全局惟一的ID号
2,重启mysql服务,使配置生效
systemctl restart mairadb
3,创建有复制权限的用户账号
GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'replpass';
命令解析:
该命令作用就是授权repluser能拷贝数据库的所有内容
二,中继slave服务器配置
1,修改主配置文件
vim /etc/my.cnf
在[mysql]配置块中添加如下两行配置
[mysqld]
log_bin
server_id=2 #为当前节点设置一个全局惟一的ID号
read_only=ON #限制从服务器为只读."注意:此限制对拥有SUPER权限的用户均无效"
log_slave_updates #该项的作用是把master服务器的二进制日志计入到本机,然后再把二进制日志复制给后端的其他slave服务器
2,重启mysql服务,使配置生效
systemctl restart mariadb
3,使用有复制权限的用户账号连接至主服务器,并启动复制线程
CHANGE MASTER TO
MASTER_HOST='host', #指定master主机IP
MASTER_USER='repluser', #指定master被授权的用户名
MASTER_PASSWORD='replpass',#指定被授权的用户密码 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定从master服务器的那个二进制日志开始复制
MASTER_LOG_POS=#; #二进制日志位置,可以在master服务器上执行该命令查看,show master logs;
启动复制线程IO_THREAD和SQL_THREAD
START SLAVE;
4,查看中继slave服务器状态
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.68.7
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 557
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 843
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes "重点关注如果是NO表示线程没起来"
Slave_SQL_Running: Yes "重点关注 如果是NO表示该线程没起来"
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: 557
Relay_Log_Space: 1139
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 "该项表示同步时间 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: 1
三,后端slave配置
1,修改配置文件
vim /etc/my.cnf
在[mysql]配置块中添加如下两行配置
[mysqld]
server_id=3 #为当前节点设置一个全局惟一的ID号
read_only=ON #限制从服务器为只读."注意:此限制对拥有SUPER权限的用户均无效"
2,重启mysql服务,使配置生效
systemctl restart mariadb
3,使用有复制权限的用户账号连接至主服务器,并启动复制线程
CHANGE MASTER TO
MASTER_HOST='中继host', #指定中继slave主机IP
MASTER_USER='repluser', #指定master被授权的用户名
MASTER_PASSWORD='replpass',#指定被授权的用户密码 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定从中继slave服务器的那个二进制日志开始复制
MASTER_LOG_POS=#; #二进制日志位置,可以在slave服务器上执行该命令查看,show master logs;
启动复制线程IO_THREAD和SQL_THREAD
START SLAVE;
4,查看slave服务器状态
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.68.17
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 557
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 843
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes "重点关注如果是NO表示线程没起来"
Slave_SQL_Running: Yes "重点关注 如果是NO表示该线程没起来"
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: 557
Relay_Log_Space: 1139
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 "该项表示同步时间 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: 1
5,最后在master服务器上创建数据库测试即可查看是否同步
级联复制特点
总结
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。