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 [root@Rocky8-11 ~] [root@Rocky8-11 ~] [root@Rocky8-11 ~] [mysqld] server-id=11 log-bin=/binlog/mysql-bin [root@Rocky8-11 ~] [root@Rocky8-11 ~] [root@Rocky8-11 ~] [root@Rocky8-11 ~] all.sql [root@Rocky8-11 ~] 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 ~]
从节点 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 [root@Rocky8-12 ~] [root@Rocky8-12 ~] [root@Rocky8-12 ~] [root@Rocky8-12 ~] [mysqld] server-id=12 log-bin=/binlog/mysql-bin read-only [root@Rocky8-12 ~] 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> 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 [root@Rocky8-12 ~] [mysqld] server-id=12 log-bin=/binlog/mysql-bin read-only=off [root@Rocky8-12 ~] mysql> set global read_only=off; mysql> stop slave; mysql> reset slave all; [root@Rocky8-12 ~] [root@Rocky8-12 ~]
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 ~] 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 ~] (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)