MySQL 一主一从 部署

一、 ENV

  • Master: 10.0.0.201
  • Slave: 10.0.0.203
  • mysql-version: 5.7.44

1.1 关闭防火墙和SElinux

systemctl stop firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

1.2 确保两台服务器时间同步

yum install chrony
yum enable chronyd --now

二、 主库配置

2.1 确保以下配置开启

[mysqld]
server-id=1
log-bin=/data/mysql/data/binlog/mysql-bin
gtid-mode=on
enforce-gtid-consistency=ON

2.2 为从库创建用户

CREATE USER 'repl'@'10.0.0.203' IDENTIFIED BY '$PASSWORD';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.203';
FLUSH PRIVILEGES;

2.3 锁定主库并获取日志信息

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

输出示例如下, 记下FilePosition, 配置从库的时候用得到。

mysql> 
mysql> flush tables read lock;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000053 | 2392 | | | 84942b7b-7c8a-11ee-a8db-4609d97455dc:1-26 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
mysql>

2.4 解锁主库之前进行数据备份

这部分参考MySQL数据库备份

三、 从库配置

3.1 确保以下配置开启

注意: 主从库的server-id一定不相同

[mysqld]
server-id=2
relay-log=/data/mysql/relay-log
gtid-mode=on
enforce-gtid-consistency=on

3.2 设置从库

mysql> 
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.201',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='$PASSWORD',
-> MASTER_LOG_FILE='mysql-bin.000053', ## master bin-log filename
-> MASTER_LOG_POS=2392; ## master bin-log position
Query OK, 0 rows affected, 2 warnings (0.69 sec)
mysql>
mysql>
mysql> START SLAVE; ## start replication
Query OK, 0 rows affected (0.06 sec)

3.3 查看从库状态

  • 注意到如下两个参数显示为yes即表示主从复制配置成功
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

      mysql> 
    mysql> show slave status \G
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 10.0.0.201
    Master_User: repl
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000053
    Read_Master_Log_Pos: 2392
    Relay_Log_File: relay-log.000003
    Relay_Log_Pos: 320
    Relay_Master_Log_File: mysql-bin.000053
    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: 2392
    Relay_Log_Space: 521
    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: 84942b7b-7c8a-11ee-a8db-4609d97455dc
    Master_Info_File: /data/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)

3.4 验证主从复制

  • 主库上执行插入语句:

    create database test;
  • 从库查询该纪录

    show databases;