虚拟机:
192.168.1.195
192.168.1.196
192.168.1.197

环境:
centos7+mysql5.7
mysql
  帐号: root
  密码:GAOmeng@123

要求:
192.168.1.195 主库
192.168.1.196 从库
192.168.1.197 从库

master配置

[root@localhost etc]# cat /etc/my.cnf
log-bin = mysql-bin
server-id = 195
port = 3306

设置数据库远程访问的用户,并且赋予权限。

[root@localhost etc]# mysql -uroot -pGAOmeng@123
GRANT FILE ON *.* TO 'rep'@'192.168.1.%' IDENTIFIED BY 'Gm@123456';
GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.1.%' IDENTIFIED BY 'Gm@123456';
select host,user from mysql.user;
FLUSH PRIVILEGES;


设置完成,在重新执行show master status;

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set

显示上面的结果,说明我们的主库已经配置完成。

Slave配置

[root@localhost etc]# cat /etc/my.cnf
log-bin = mysql-bin
server-id = 196
port = 3306

#关闭Slave

[root@localhost etc]# mysql -uroot -pGAOmeng@123
mysql> stop slave;

 

#设置连接主库信息 master_log_file 和 master_log_pos  在show master status中可以查询。

mysql> change master to master_host='192.168.1.195',master_user='rep',master_password='Gm@123456',master_log_file='mysql-bin.000001', master_log_pos=154;

#开启Slave

mysql> start slave;

# 查询从库的配置是否正常。

mysql> show slave status \G;

#如果下面的两项参数都是YES,说明主从配置已经完成。

Slave_IO_Running: Yes 
lave_SQL_Running: Yes

# 其他

mysql>stop slave;  //停止
mysql>reset slave;  //清空
mysql>start slave;  //开启


注意问题:

当你发现按照要求,配置都是一模一样的时候,执行show master status; 得到Empty set 如下:

mysql> show master status;
Empty set

那么,很可能是你的主从库时间不一致导致的,可以如下设置。

第一种:

date -R
ntpdate time.windows.com && hwclock -w

第二种:

如果是VMware Workstation装的虚拟机可以直接,选中虚拟机-右键设置-选项-VMwareTools-点击勾选右侧的【将客户机于主机同步】选项。设置完成,重启下客户机。


相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!