MySQL主从复制

MySQL 主从复制原理

主从复制(也称 AB 复制)是指一台服务器充当主数据库服务器,另一台或多台服务器充当从数据库服务器,主服务器中的数据自动复制到从服务器之中。对于多级复制,数据库服务器既可充当主机,也可充当从机。MySQL默认采用异步复制方式。

主从复制的过程及原理可以总结如下:
  • master服务器将数据的改变记录二进制binlog日志,当master上的数据发生改变时,则将其改变写入二进制日志中。
  • slave服务器会在一定时间间隔内对master二进制日志进行探测其是否发生改变,如果发生改变,则开始一个I/OThread请求master二进制事件。
  • 同时主节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从节点本地的中继日志中,从节点将启动SQL线程从中继日志中读取二进制日志,在本地重放,使得其数据和主节点的保持一致。
主从复制所需要的条件:
  • 开启二进制日志
  • 设置server-id
  • 创建同步账号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CHANGE MASTER TO 
MASTER_HOST='10.0.0.11',
MASTER_USER='repluser',
MASTER_PASSWORD='replpass',
MASTER_LOG_FILE='mariadb-bin.xxxxXX,
MASTER_LOG_POS=1567,
MASTER_DELAY=interva1; #可指定延迟复制实现访问误操作,单位秒

START SLAVE [IO THREAD SQL THREAD]; #
STOP SLAVE;
RESET SLAVE ALL;

SHOW SLAVE STATUS;

#查看 relaylog 事件,mysql8.0已取消中继日志
SHOW RELAYLOG EVENTS in 'relay-bin.00000x';


例一:将已有的mysql8.0单机变成主从复制架构

主节点 Rocky8-11 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 配置主节点配置文件,启用二进制日志,配置server-id
[root@Rocky8-11 ~]# mkdir /binlog/
[root@Rocky8-11 ~]# chown mysql. /binlog/
[root@Rocky8-11 ~]# vim /etc/my.cnf
[mysqld]
server-id=11
log-bin=/binlog/mysql-bin
[root@Rocky8-11 ~]# systemctl restart mysqld

## 完全备份
[root@Rocky8-11 ~]# mkdir /data
[root@Rocky8-11 ~]# mysqldump -A -F --master-data=1 --single-transaction > /data/all.sql
[root@Rocky8-11 ~]# ls /data/
all.sql

## 创建复制用户并授权
[root@Rocky8-11 ~]# mysql
01:28:55(root@localhost) [(none)] create user repluser@'10.0.0.%' identified by "123456";
01:30:11(root@localhost) [(none)] grant replication slave on *.* to repluser@'10.0.0.%';

## 将备份复制到从节点
[root@Rocky8-11 ~]# scp /data/all.sql 10.0.0.12:/data/
从节点 Rocky8-12 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## 安装数据库、启用二进制日志,修改日志路径、设置server-id、启用数据库只读
[root@Rocky8-12 ~]# yum install mysql-server
[root@Rocky8-12 ~]# mkdir /binlog
[root@Rocky8-12 ~]# chown mysql. /binlog/
[root@Rocky8-12 ~]# vim /etc/my.cnf
[mysqld]
server-id=12
log-bin=/binlog/mysql-bin
read-only

## 修改备份文件
[root@Rocky8-12 ~]# vim /data/all.sql
CHANGE MASTER TO
MASTER_HOST='10.0.0.11',
MASTER_PORT=3306,
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=157;

## 还原数据库
[root@Rocky8-12 ~]# mysql
mysql> set sql_log_bin=0;
mysql> source /data/all.sql

mysql> set sql_log_bin=1;
mysql> start slave;
查看主从服务状态

主要观察以下两项,确保状态为 Yes:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.11
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 573
Relay_Log_File: Rocky8-12-relay-bin.000004
Relay_Log_Pos: 789
Relay_Master_Log_File: mysql-bin.000003
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: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 573
Relay_Log_Space: 1050
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
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: 11
Master_UUID: f41fb64d-fb87-11ed-afcd-000c29bb093a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)

例二:当Master节点故障,提升一个Slave做为新的Master

新master(10.0.0.12):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## 找到一个数据最新的节点,关闭readonly
[root@Rocky8-12 ~]# vim /etc/my.cnf
[mysqld]
server-id=12
log-bin=/binlog/mysql-bin
read-only=off

## 清除旧的master复制信息
[root@Rocky8-12 ~]# mysql
mysql> set global read_only=off;
mysql> stop slave;
mysql> reset slave all;

## 完全备份
[root@Rocky8-12 ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/backup.sql

## 分析旧master上的二进制日志,将未同步的二进制导出来,尽可能的恢复数据到新的master;

## 将备份传到其它slave
[root@Rocky8-12 ~]# scp /data/backup.sql 10.0.0.11:/data/

slave:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@Rocky8-11 ~]# vim /data/backup.sql 
CHANGE MASTER TO
MASTER_HOST='10.0.0.12',
MASTER_PORT=3306,
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=157;

[root@Rocky8-11 ~]# mysql
(root@localhost) [(none)] stop slave;
(root@localhost) [(none)] reset slave all;
(root@localhost) [(none)] set sql_log_bin=off;
(root@localhost) [(none)] source /data/backup.sql;
(root@localhost) [(none)] set sql_log_bin=on;
(root@localhost) [(none)] show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.12
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 157
Relay_Log_File: Rocky8-11-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000003
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: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 157
Relay_Log_Space: 540
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
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: 12
Master_UUID: e0a8221e-fde2-11ed-a771-000c29df68f9
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)


MySQL主从复制
https://www.xcjyc.top/2023/05/25/MySQL主从复制/
作者
XCJYC
发布于
2023年5月25日
许可协议