1.1、主从同步的两种角色:
主服务器(master):接受客户端访问连接
从服务器(slave):自动同步主服务器数据
1.2、主从同步实现的原理:
主(master)数据服务器启动binlog日志,当binlog日志在备份的时候。从(slave)数据服务器的的I/O线程会自动复制主服务器的binlog日志里面的sql命令到relay_log文件中,从服务器的sql线程会执行relay_log中的sql语句实现与主服务器中的数据一致
1.3、数据库恢复初始化设置
[root@master ~]# systemctl stop mysqld.service
[root@master ~]# rm -rf /var/lib/mysql/*
[root@master ~]# rm -rf /var/log/mysqld.log
[root@master ~]# systemctl start mysqld
[root@master ~]# grep "password" /var/log/mysqld.log
2022-05-16T16:05:03.715052Z 1 [Note] A temporary password is generated for root@localhost: Hm1=3D&y?0lA
[root@master ~]# mysql -uroot -p"Hm1=3D&y?0lA"
2.1、环境配置
master服务器:192.168.4.10
slave服务器:192.168.4.20
两台服务器都安装mysql数据库,并关闭防火墙和selinux
2.2、配置master服务器
1)启用binlog日志文件
[root@master ~]# vim /etc/my.cnf
[mysqld]
server_id=10
log_bin=master10
[root@master ~]# systemctl restart mysqld
2)用户授权,让用户有复制命令权限
[root@master ~]# mysql -uroot -pJY1987...zy2011
mysql> grant replication slave on *.* to repluser@"%" identified by "JY1987...zy2011";
Query OK, 0 rows affected, 1 warning (0.00 sec)
3)日志查看
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master10.000001 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.2、配置slave服务器
1) 指定server_id,并重启mysqld服务
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server_id=20
2)确保数据一致(如果一致的此步骤可以省略)
3)指定主服务器信息
[root@slave ~]# mysql -uroot -pJY1987...zy2011
mysql> show slave status \G #不是从服务器显示结果
Empty set (0.00 sec)
mysql> change master to master_host="192.168.4.10",
-> master_user="repluser",
-> master_password="JY1987...zy2011",
-> master_log_file="master10.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
4)启动slave线程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
5)查看状态,IO线程和SQL线程必须同时是YES状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.4.10
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master10.000001
Read_Master_Log_Pos: 441
Relay_Log_File: slave-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master10.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.1、环境配置
master服务器:192.168.4.10
slave服务器:192.168.4.20、192.168.4.30(增加一台从服务器)
延续一主一从的架构,新添加一台从服务器,并安装mysql数据库,并关闭防火墙和selinux
3.2、配置新的slave服务器
1)指定slave_id,并重启mysqld服务
[root@slave2 ~]# vim /etc/my.cnf
[mysqld]
server_id=30
[root@slave2 ~]# systemctl restart mysqld
2)确保和主服务器数据一致性,主做完全备份并拷贝给从。
--master-data做完全备份数据时,在备份文件里记录使用的日志名和偏移量
[root@master ~]# mysqldump -uroot -pJY1987...zy2011 --master-data -A > /root/master.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# scp /root/master.sql 192.168.4.30:/root/
3)从服务器使用备份文件恢复数据。
[root@slave2 ~]# mysql -uroot -pJY1987...zy2011 < master.sql
3)指定主服务器信息,在备份文件里查看日志名和偏移量
[root@slave2 ~]# grep master10 /root/master.sql
CHANGE MASTER TO MASTER_LOG_FILE='master10.000001', MASTER_LOG_POS=685;
[root@slave2 ~]# mysql -uroot -pJY1987...zy2011
mysql> change master to master_host="192.168.4.10",
-> master_user="repluser",
-> master_password="JY1987...zy2011",
-> master_log_file="master10.000001",
-> master_log_pos=685;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
4)启动slave进程,并查看状态(IO线程和SQL线程必须同时是YES状态)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.10
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master10.000001
Read_Master_Log_Pos: 685
Relay_Log_File: slave2-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master10.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4.1、环境配置
master服务器:192.168.4.10 #为slave【1】的主
slave【1】服务器:192.168.4.20 #即为master的从也为slave【2】的主
slave【2】服务器:192.168.4.30 #为slave【1】的从
服务器安装mysql数据库,并关闭防火墙和selinux
4.2、主服务器的配置
[root@master ~]# vim /etc/my.cnf
[mysqld]
server_id=10
log_bin=master10
[root@master ~]# systemctl restart mysqld
[root@master ~]# mysql -uroot -pJY1987...zy2011
mysql> grant replication slave on *.* to slave@"%" identified by "JY1987...zy2011";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master10.000002 | 438 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@master ~]# ls /var/lib/mysql/master10.*
/var/lib/mysql/master10.000001 /var/lib/mysql/master10.index
4.3、从服务器【1】的配置:它有两种角色,即为主服务器也为从服务器,因为从服务器也有从服务器所以就要启用级联复制功能。
1)配置主
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server_id=20
log_bin=zucong20
log_slave_updates #允许级联复制,当前将自己主服务器的数据拷贝给从服务器
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# mysql -uroot -pJY1987...zy2011
mysql> grant replication slave on *.* to slave2@"%" identified by "JY1987...zy2011";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| zucong20.000001 | 439 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2)配置从
mysql> change master to master_host="192.168.4.10",
-> master_user="slave",
-> master_password="JY1987...zy2011",
-> master_log_file="master10.000002",
-> master_log_pos=438;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.10
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master10.000002
Read_Master_Log_Pos: 438
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master10.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4.4、从服务器【2】的配置,将这台从服务器配置为服务器【1】的从
1)指定server_id,并重启mysqld服务
[root@slave2 ~]# vim /etc/my.cnf
[mysqld]
server_id=30
[root@slave2 ~]# systemctl restart mysqld
2)指定主服务器的信息
[root@slave2 ~]# mysql -uroot -pJY1987...zy2011
mysql> change master to master_host="192.168.4.20",
-> master_user="slave2",
-> master_password="JY1987...zy2011",
-> master_log_file="zucong20.000001",
-> master_log_pos=439;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.20
Master_User: slave2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zucong20.000001
Read_Master_Log_Pos: 439
Relay_Log_File: slave2-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: zucong20.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.1、环境配置
slave【1】服务器:192.168.4.20
slave【2】服务器:192.168.4.30
服务器安装mysql数据库,并关闭防火墙和selinux
5.2、 slave【1】的配置
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server_id=20
log_bin=slave20
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# mysql -uroot -pJY1987...zy2011
mysql> grant replication slave on *.* to slave20@"%" identified by "JY1987...zy2011";
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| slave20.000001 | 440 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5.3、slave【2】的配置
[root@slave2 ~]# vim /etc/my.cnf
[mysqld]
server_id=30
log_bin=slave30
[root@slave2 ~]# systemctl restart mysqld
[root@slave2 ~]# mysql -uroot -pJY1987...zy2011
mysql> grant replication slave on *.* to slave30@"%" identified by "JY1987...zy2011";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| slave30.000001 | 440 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
1)把自己配置为slave【1】的从服务器,日志名和偏移量要在slave【1】主机查看后填写
mysql> change master to master_host="192.168.4.20",
-> master_user="slave20",
-> master_password="JY1987...zy2011",
-> master_log_file="slave20.000001",
-> master_log_pos=440;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.20
Master_User: slave20
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: slave20.000001
Read_Master_Log_Pos: 440
Relay_Log_File: slave2-relay-bin.000004
Relay_Log_Pos: 318
Relay_Master_Log_File: slave20.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.4、把slave【1】配置为slave【2】的从服务器,日志名和偏移量要在slave【2】主机查看后填写
mysql> change master to master_host="192.168.4.30",
-> master_user="slave30",
-> master_password="JY1987...zy2011",
-> master_log_file="slave30.000001",
-> master_log_pos=440;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.30
Master_User: slave30
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: slave30.000001
Read_Master_Log_Pos: 440
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 318
Relay_Master_Log_File: slave30.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
异步复制:主服务器执行完一次事务后,立即将结果返回给客户端,不关心从服务器是否已经同步数据。
半同步复制:介于异步复制和全同步复制之间,主服务器在执行完一次事务后,等待至少一台从服务器数据完成,才将结果返回给客户端。
6.1、环境配置
master【1】服务器:192.168.4.10
slave【2】服务器:192.168.4.30
服务器安装mysql数据库,并关闭防火墙和selinux
6.2、方法一:命令行设置,不用重启服务马上生效
1)安装模块
mysql> install plugin rpl_semi_sync_master soname "semisync_master.so"; #master模块
Query OK, 0 rows affected (0.00 sec)
mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so"; #slave模块
Query OK, 0 rows affected (0.00 sec)
2)查看模块
mysql> select plugin_name,plugin_status from information_schema.plugins where plugin_name like "%semi%";
+----------------------+---------------+
| plugin_name | plugin_status |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE |
| rpl_semi_sync_slave | ACTIVE |
+----------------------+---------------+
2 rows in set (0.00 sec)
3)启动模块
mysql> set global rpl_semi_sync_master_enabled=1; #主模块
Query OK, 0 rows affected (0.00 sec)
mysql> set global rpl_semi_sync_slave_enabled=1; #从模块
Query OK, 0 rows affected (0.00 sec)
4)查看模块是否启动
mysql> show variables like "rpl_semi_sync_%_enabled";
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_slave_enabled | ON |
+------------------------------+-------+
2 rows in set (0.00 sec)
6.2、永久配置,编辑主配置文件
[root@master ~]# vim /etc/my.cnf
[mysqld]
#安装模块
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
#启用模块
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled=1
[root@master ~]# systemctl restart mysqld
[root@master ~]# mysql -uroot -pJY1987...zy2011
mysql> show variables like "rpl_semi_sync_%_enabled";
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_slave_enabled | ON |
+------------------------------+-------+
2 rows in set (0.00 sec)