参考文档

修改配置文件

配置介绍

  • Master数据库

编辑配置文件vim /etc/my.cnf,文件内容如下

1
2
3
4
5
6
7
8
9
10
11
[mysqld]
# 必须配置的服务id,一般配置主机的ip
server-id=168
# 必须配置,开启二进制
log-bin=mysql-bin

# 下面都是可选配置
# 要同步的数据库
binlog-do-db=demo
# 同步表
# replicate-do-table=user
  • Slave数据库

修改配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqld]
# 必须配置
server-id=169

# 下面都是可选配置
sync_binlog=0
innodb_flush_log_at_trx_commit=2

slave-skip-errors=all
# 配置忽略的数据库
# binlog-ignore-db=mysql
# 配置同步的数据库
# replicate-do-db=demo
  • 重启修改配置后的数据库
1
systemctl restart mysqld

在Master创建用户用于同步

1
2
3
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
flush privileges;

查看Master二进制文件信息

1
2
3
4
5
6
7
8
9
10
11
# 锁住全部表
FLUSH TABLES WITH READ LOCK;
# 同步完解锁
unlock tables;
# File和Position的参数是我们配置Slave的时候需要的
SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1077 | | | |
+------------------+----------+--------------+------------------+-------------------+

Slave配置连接

登录MySQL命令控制台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# MASTER_HOST 主机地址
# MASTER_USER Master创建好的用户
# MASTER_PASSWORD 密码
# MASTER_PORT 端口,可选
# MASTER_LOG_FILE 二进制文件名
# MASTER_LOG_POS 开始同步位置

CHANGE MASTER TO
MASTER_HOST='192.168.99.168',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_PORT='3306',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=2216;
  • 启动
1
2
# 启动
start slave;
  • 查看Slave

输入show slave status\G,会显示类似如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.99.168
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 700
Relay_Log_File: LENOVO-PC-relay-bin.000002
Relay_Log_Pos: 867
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: demo
Replicate_Ignore_DB: mysql
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: 700
Relay_Log_Space: 1079
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: 168
Master_UUID: bd1531fb-f568-11e9-bc4b-46afd4d32e02
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
dates
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:
1 row in set (0.00 sec)

ERROR:
No query specified

注意显示状态是这样表示成功连到Master:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

不是这个状态会有错误显示

1
2
# 每次重新配置Slave需要停止
stop slave

注意事项

  • 配置前务必将两个数据库手工同步