4.1 环境介绍
4.1.1 Master环境介绍
1)操作系统:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.61
4.1.2 Slave环境介绍:
1)操作系统:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.62
4.2 配置
4.2.1 Master配置
1)my.cnf配置
#vim /etc/my.cnf
[root@ok_1 ~]# vim /etc/my.cnf
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin #[必须]启用二进制日志
server-id=61 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
2)重启mysql
[root@ok_1 ~]# systemctl restart mysqld
3)在主服务器上建立帐户并授权slave
[root@ok_1 ~]# mysql -uroot -p123456
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
这里要写这两句画的原因是mysql5.7不允许写简单密码,所以这里要通过命令更改mysql配置文件使其支持简单密码。
mysql> GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//这里
//这里也不用写Slave端的用户名,因为这里的mysql用户名是专门用来做复制的!
//一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.245.139,加强安全。
//"mysqlsync"也不用写Slave端的用户名,因为这里是mysql专门用来做复制的!
4) 登录mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 434 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化。
4.2.2 Slave配置
1)my.cnf配置
[root@0k_2 ~]# vim /etc/my.cnf
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin #[必须]启用二进制日志
server-id=62 #[必须]服务器唯一ID,默认是1,一般取IP最后一段
2)重启mysql
[root@0k_2 ~]# systemctl restart mysqld
3)配置从服务器Slave:
[root@0k_2 ~]# mysql -uroot -p123456
mysql> change master to master_host='10.0.0.61',master_user='mysqlsync',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=434;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
//注意不要断开,“434”无单引号,master_host的地址一定不能写错!
mysql> start slave; #启动从服务器复制功能
Query OK, 0 rows affected (0.00 sec)
- 检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.61
Master_User: mysqlsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 434
Relay_Log_File: 0k_2-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: 434
Relay_Log_Space: 526
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: 61
Master_UUID: 91df0025-134c-11e9-8264-000c2921ff55
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及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态。
5)开始验证
(1)初始状态
(2)新建test表
(3)验证结果
成功!!