前提
假设我们以将安装好了mysql
(这里我们使用的版本是5.7.25),系统环境为centos7
,master
和slave
机器IP
如下:
master 192.168.10.101
slave 192.168.10.61
1、配置master
1.1 编辑/etc/my.cnf文件
在文件增加如下配置
[mysqld]
server-id=101 # 通常为本机ip后几位
log-bin=master-bin # 开启master的binlog
log-bin-index=master-bin.index
重启mysql
systemctl restart mysqld
1.2 查看master的binlog
下面的master-bin.000005将会在slave配置中使用
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000005 | 20034 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
1.3 创建用于主从同步的用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.10.61' IDENTIFIED BY 'mysql';
mysql> flush privileges;
表示允许从库从192.168.10.61使用repl用户访问主库。
2、配置slave
2.1 编辑/etc/my.cnf文件
在文件增加如下配置
[mysqld]
server-id=61 # 通常为机器ip后几位
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin # slave日志中继
重启mysql
systemctl restart mysqld
2.2 配置master信息
mysql> change master to master_host='192.168.10.101',master_port=3306,master_user='repl',master_password='mysql',master_log_file='master-bin.000005',master_log_pos=0;
mysql> start slave;
这里的master-bin-000005
文件是在master
数据库中使用show master status
获取到的。
3、验证主从同步
在slave
机器上查看slave
状态:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos: 20034
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 20249
Relay_Master_Log_File: master-bin.000005
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: 20034
Relay_Log_Space: 20456
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: 101
Master_UUID: e4d0a3a6-4947-11e9-9ddf-52540049495f
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.01 sec)
ERROR:
No query specified
如果看到slave
的Slave_IO_Running: Yes 和Slave_SQL_Running: Yes
都是开启的且没有错误提示就说明主从配置已经成功。