以下是 MySQL 主从复制(Master-Slave Replication)的配置步骤,分为 主库配置 和 从库配置 两部分:
MySQL主从复制
主库配置
1、修改配置文件
在该MySQL配置文件中( /etc/my.cnf
)添加以下内容:
[mysqld]
log_bin=mysql-bin # 启用二进制日志
server-id=1 # 设置主库唯一ID(必须与从库不同)
binlog_format=ROW # 推荐使用 ROW 格式(可选)
2、重启
sudo systemctl restart mysqld
3、创建复制专用用户
-- 登录MySQL,执行:
CREATE USER 'repl_user'@'slave_ip' IDENTIFIED BY 'your_password';
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'slave_ip';
FLUSH PRIVILEGES;
-- 将 slave_ip 替换为从库 IP,your_password 替换为强密码
4、获取主库二进制日志状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
-- 记录输出结果中的 File(当前日志文件名)和 Position(日志位置),后续配置从库时需要用到
从库配置
1、在该MySQL配置文件中( /etc/my.cnf
)添加以下内容:
[mysqld]
server-id=2 # 设置从库唯一ID(必须与主库不同)
read_only=ON # 设置从库为只读(可选,但推荐)
2、重启
sudo systemctl restart mysqld
3、配置主库连接信息
-- 登录从库 MySQL,执行:
CHANGE MASTER TO
MASTER_HOST='master_ip', -- 主库IP
MASTER_USER='repl_user', -- 复制用户名
MASTER_PASSWORD='your_password', -- 复制用户密码
MASTER_LOG_FILE='mysql-bin.000001', -- 主库日志文件名(从SHOW MASTER STATUS获取)
MASTER_LOG_POS=154; -- 主库日志位置(从SHOW MASTER STATUS获取)
4、启动复制进程
START SLAVE;
5、验证复制状态
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master_ip
Master_User: hostname
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-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: 154
Relay_Log_Space: 531
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: 1
Master_UUID: 34a7fa39-1f7b-11f0-94fc-000c29ac8626
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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:
1 row in set (0.00 sec)
-- 检查以下关键字段:
-- Slave_IO_Running: Yes
-- Slave_SQL_Running: Yes
-- Last_IO_Error: (应为空)
-- Last_SQL_Error: (应为空)
验证
主库
-- 创建新数据库
CREATE DATABASE test_db;
-- 切换到新数据库
USE test_db;
-- 创建表并插入数据
CREATE TABLE test_table (id INT);
INSERT INTO test_table VALUES (1);
从库
-- 查看数据库是否同步
SHOW DATABASES;
-- 查看表是否同步
SHOW TABLES FROM test_db;
-- 查看数据是否同步
SELECT * FROM test_db.test_table;
常见问题排查
- 连接失败
- 检查主从库网络连通性:
ping master_ip
- 确认防火墙开放端口:默认 3306
- 检查主从库网络连通性:
- 复制延迟
- 检查主库负载:
SHOW PROCESSLIST;
- 优化从库硬件或调整
sync_binlog
参数。
- 检查主库负载:
- 数据不一致
- 使用
pt-table-checksum
工具校验数据一致性。 - 重新同步数据(需停止复制,全量导出导入,再重新配置)。
- 使用
拓展
- 一主多从:
多个从库独立配置,指向同一个主库。 - 链式复制:
A → B → C,B 既是 A 的从库,也是 C 的主库。 - 半同步复制:
通过插件实现,确保至少一个从库接收数据后才提交事务。