操纵系统任然是rocky9.3,备注:要至少两台服务器可以互相ping同,关闭防火墙或者是方型3306。如果是云防火墙也要放行3306。
查看mysql(master)的状态
[root@Server33 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-06-04 19:38:58 CST; 22min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 2596 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 2619 (mysqld)
Status: "Server is operational"
Tasks: 50 (limit: 48846)
Memory: 456.6M
CPU: 13.726s
CGroup: /system.slice/mysqld.service
└─2619 /usr/sbin/mysqld
Jun 04 19:38:57 Server33 systemd[1]: Starting MySQL Server...
Jun 04 19:38:58 Server33 systemd[1]: Started MySQL Server.查看mysql(slave)的状态
[root@server30 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-06-04 19:51:49 CST; 10min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3167 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3190 (mysqld)
Status: "Server is operational"
Tasks: 44 (limit: 48846)
Memory: 463.6M
CPU: 8.857s
CGroup: /system.slice/mysqld.service
└─3190 /usr/sbin/mysqld
Jun 04 19:51:48 server30 systemd[1]: Starting MySQL Server...
Jun 04 19:51:49 server30 systemd[1]: Started MySQL Server.使用ping一下,关闭或者放行防火墙(master)
[root@Server33 ~]# ping 192.168.122.12 -c 4
PING 192.168.122.12 (192.168.122.12) 56(84) bytes of data.
64 bytes from 192.168.122.12: icmp_seq=1 ttl=64 time=0.561 ms
64 bytes from 192.168.122.12: icmp_seq=2 ttl=64 time=0.558 ms
64 bytes from 192.168.122.12: icmp_seq=3 ttl=64 time=0.770 ms
64 bytes from 192.168.122.12: icmp_seq=4 ttl=64 time=0.899 ms
--- 192.168.122.12 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3054ms
rtt min/avg/max/mdev = 0.558/0.697/0.899/0.144 ms
[root@Server33 ~]# systemctl is-active firewalld.service
inactive
[root@Server33 ~]# 使用ping一下,关闭或者放行防火墙(slave)
[root@server30 ~]# ping 192.168.122.25 -c 4
PING 192.168.122.25 (192.168.122.25) 56(84) bytes of data.
64 bytes from 192.168.122.25: icmp_seq=1 ttl=64 time=0.474 ms
64 bytes from 192.168.122.25: icmp_seq=2 ttl=64 time=0.713 ms
64 bytes from 192.168.122.25: icmp_seq=3 ttl=64 time=1.29 ms
64 bytes from 192.168.122.25: icmp_seq=4 ttl=64 time=0.930 ms
--- 192.168.122.25 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3049ms
rtt min/avg/max/mdev = 0.474/0.851/1.288/0.299 ms
[root@server30 ~]# systemctl is-active firewalld.service
inactive
[root@server30 ~]# master的工作创建和授权
创建从用户myslave
设置密码
刷新权限
查看状态
修改配置文件/etc/my.cnf
修改mysql的配置文件一定要重启mysql的服务。
#直接复制
[root@Server33 ~]# tail /etc/my.cnf
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#id一定不能重复
server-id = 11
log-bin = master-bin
log-slave-updates=true
[root@Server33 ~]# #创建从用户myslave,允许192.168.122所有的主机进行访问
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.122.%';
Query OK, 0 rows affected (0.01 sec)
#刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
#查看状态记住file值和Position
mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 887 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)slave的工作创建和连接master
修改配置文件
/etc/my.cnf
连接master
查看状态
[root@server30 ~]# tail -n 11 /etc/my.cnf
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id = 12
log-bin = master-bin
log-slave-updates=true
#设置只读
read_only = 1
[root@server30 ~]# 修改mysql的配置文件一定要重启mysql的服务。
如果之前开启过主从一定要先暂停主从
mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.01 sec)
#连接master服务器
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.122.25',
-> MASTER_USER='myslave',
-> MASTER_PASSWORD='Root.000',
-> MASTER_LOG_FILE='master-bin.000001',
-> MASTER_LOG_POS=887,
-> GET_MASTER_PUBLIC_KEY = 1;
Query OK, 0 rows affected, 9 warnings (0.06 sec)
#开启主从服务
mysql> START SLAVE;
Query OK, 0 rows affected, 1 warning (0.05 sec)看到这两个参数为 Yes 就代表成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes检查从库状态(验证是否成功)
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.122.25
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 887
Relay_Log_File: server30-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: master-bin.000001
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: 887
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: 11
Master_UUID: 6756f46c-5cda-11f1-9a98-52540030f9c6
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: 1
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
评论区