概述
1、master开启二进制日志记录
2、slave开启IO进程,从master中读取二进制日志并写入slave的中继日志
3、slave开启SQL进程,从中继日志中读取二进制日志并进行重放
4、最终,达到slave与master中数据一致的状态,我们称作为主从复制的过程。
基础环境设置
防火墙和上下文
#主从
[root@slave ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# getenforce
Enforcing
[root@slave ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@slave ~]# setenforce 0
网络对时
#主从
[root@localhost ~]# cat /etc/chrony.conf | grep -Ev '^$|#'
server ntp.aliyun.com iburst ###添加或修改
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
leapsectz right/UTC
logdir /var/log/chrony
[root@master ~]# timedatectl set-timezone Asia/Shanghai
[root@master ~]# date
2025年 07月 05日 星期六 17:18:20 CST
主从配置
主配置
#创建一个从可以登录的账户,并赋予权限
mysql> create user 'slave'@'192.168.157.%' identified by '1230';
Query OK, 0 rows affected (0.01 sec)
#因为要同步所有数据,所以给全部权限
mysql> grant all on *.* to 'slave'@'192.168.157.%';
Query OK, 0 rows affected (0.00 sec)
#主服务文件配置
#屏蔽原有日志文件,新建日志文件,设置id值
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
log-bin=mysql-bin
binlog_format="statement"
server-id=101 #主从必须不一样
#重启MySQL服务
[root@master ~]# systemctl restart mysqld
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#注意:查看位置完毕后,不要对master做insert、update、delete、create、drop等操作!!!
从配置
#修改配置文件
vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
relay-log-index=slave-bin.index 1
server-id=202 #与主配置要不一样 1
#进入MySQL 指向主服务器
[root@slave mysql]# mysql
mysql> change master to master_host='192.168.157.150',master_user='slave',master_password='1230',master_log_file='mysqlbin.000003',master_log_pos=157;
#验证
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 source to send event
Master_Host: 192.168.157.150
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 157
Relay_Log_File: slave-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: 536
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: 101
Master_UUID: f2a6f311-51cb-11f0-bd56-000c299bfda5
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:
测试
#从
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
#主
mysql> create database slave;
Query OK, 1 row affected (0.01 sec)
#从
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slave |
| sys |
+--------------------+
5 rows in set (0.01 sec)
重复
reset replica;
##用于重置SQL线程对relay log的重放记录!!