mysql5.7主从(Master/Slave)同步配置

环境:

mysql版本都是5.7(以前的版本配置可能不一样)

主(Master) windows:192.168.0.68

从(Slave) centos7:192.168.0.4

 

基本环境配置:

要保证防火墙3306端口开放,如果只是为了学习,可以直接关闭防火墙。

centos关闭防火墙方法:service iptables stop或者systemctl stop firewalld

 

Master的配置

修改/etc/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=test

这里的server-id用于标识唯一的数据库,在从库必须设置为不同的值。

binlog-ignore-db:表示同步的时候忽略的数据库

binlog-do-db:指定需要同步的数据库

 

1、修改完配置,重启mysql

systemctl restart mysql

2、进入mysql,mysql -uroot -p,回车,输入mysql密码进入。

3、赋予从库权限账号,允许用户在主库上读取日志,赋予192.168.0.4也就是Slave机器有File权限,

只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。

grant FILE on *.* to 'root'@'192.168.0.4' identified by 'root';
grant replication slave on *.* to 'root'@'192.168.0.4' identified by 'root';
flush privileges;

这里的用户是同步的时候从库使用的用户。

4、重启mysql,登录mysql,查看主库信息

show master status;

如果该命令没数据,说明上面配置有误。

File是同步会使用到的binlog文件,Position是同步的时候也要用到的。

 

Slave的配置

1、修改/etc/my.cnf

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60 

2、在这里可以看到,在mysql5.6之后的版本中没有指定:而且在5.6之后的版本设置下面的内容的话会报错

master-host=192.168.1.1 #Master的主机IP
master-user=root
master-password=mysql password #Master的MySQL密码 

3、修改完/etc/my.cnf后,重启一下mysql(systemctl restart mysql)

进入Slave的mysql控制台,执行下面操作:

stop slave;
change master to master_host='192.168.0.68',master_user='root',master_password='root',master_log_file='mysql-bin.000004', master_log_pos=28125;
start slave;

注意:上面的master_log_file是在Master中show master status显示的File,

而master_log_pos是在Master中show master status显示的Position。

然后可以通过show slave status查看配置信息。

如果没有同步成功,请查看show slave status中的position和file是否和master中的对应了。

 

posted @ 2017-08-12 13:54  佚名000  阅读(14318)  评论(1编辑  收藏  举报