准备:
服务器1:192.168.37.130 (主)
服务器2:192.168.37.131 (从)
MySQL 版本:5.7.24
开始:
主服务器设置:
- 修改 my.cnf 配置,完成后重启数据库
[mysqld]
# 主服务器启用二进制日志,之后从服务器将读取该日志,复制到从服务器的 Relay log 中,并定时执行新增的内容
log-bin=/usr/local/mysql/logs/mysql-bin
# 指定数据库
binlog-do-db=xxxx 二进制日志记录的数据库
# 忽略数据库
binlog-ignore-db=xxxx
# 忽略 server-id 配置时,默认其为 0,主服务器将拒绝来自从服务器的任何连接
server-id=1
# 使用带事务的InnoDB进行复制设置时尽可能提高持久性和一致性
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
- 确保 skip_networking 为关闭状态(OFF)
mysql> SHOW VARIABLES LIKE '%skip_networking%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| skip_networking | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
- 创建从服务器用户复制数据的用户并授予权限
mysql> GRANT REPLICATION SLAVE ON *.* to 'slave1'@'192.168.10.131' identified by 'password';
从服务器配置:
- 修改 my.cnf 配置,完成后重启
[mysqld]
server-id=2
# 设定需要复制的数据库
replicate-do-db=xxx
# 设定需要忽略的数据库
replicate-ignore-db=xxx
- 配置连接主服务器的账号信息
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.37.130',
-> MASTER_USER='slave1',
-> MASTER_PASSWORD='password';
Query OK, 0 rows affected, 2 warnings
- 启动复制线程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
- 检查状态 Slave_IO_Running 与 Slave_SQL_Running 为 YES 则表示成功启动
mysql> SHOW slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.130
Master_User: slave1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1464
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 1677
Relay_Master_Log_File: mysql-bin.000002
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: 1464
Relay_Log_Space: 2101
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: c19b2ca3-b2e7-11ea-9aaa-000c29839863
Master_Info_File: /usr/local/mysql/data/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)
ERROR:
No query specified
测试效果
在主服务器上执行创建或插入语句,在从服务器中检查是否成功复制
注意事项:
- 当启用主从复制并主服务器中还有数据时,需要另外进行处理,可以先使用 mysqldump 将之前的数据复制到从数据库
- 添加新的从服务器时,新的服务器的 server-id 不可与之前的服务器重复
常用命令
# 关闭从服务器所有类型线程,也就是停止从主服务器复制数据的操作
mysql> STOP SLAVE;
# 单独关闭某个线程
mysql> STOP SLAVE IO_THREAD;
mysql> STOP SLAVE SQL_THREAD;
# 启动线程
mysql> START SLAVE;
mysql> START SLAVE IO_THREAD;
mysql> START SLAVE SQL_THREAD;
# 从库删除主从关系
mysql> RESET SLAVE;