2018/05/16 - 14:48
环境:centos7u4
master1 10.0.0.131
master2 10.0.0.130
slave1 10.0.0.140
slave2 10.0.0.141
所有机器都作以下操做:
修改主机名称
分别为:master1和master2 slave1 slave2
域名解析
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.131 master1
10.0.0.130 master2
10.0.0.140 slave1
10.0.0.141 slave2
ssh公钥
关闭selinux和防火墙
[root@master ~]# setenforce 0 && systemctl stop firewalld
确保时间一致
主机之间的连同性
下载并安装Mysql5.7(所有机器都做)
#lftp 10.18.41.251
> cd 1802
> mirror mysql57
# cd mysql57
# rpm -ivh *.rpm
启动mysql:(所有机器都做)
# systemctl start mysqld
注意:
mysql5.7启动之后自带密码,不能直接登陆
获取mysql5.7初始启动密码:
# grep passw /var/log/mysqld.log
2018-05-15T07:12:00.852544Z 1 [Note] A temporary password is generated for root@localhost: N-+e*ZL:_3gh
测试密码是否可用:
# mysql -uroot -p'N-+e*ZL:_3gh'
去掉简单密码限制功能(做实验的时候先不要作这一步):
#vim /etc/my.cnf //追加如下选项
[mysqld]
validate_password=off
重启服务
修改成简单密码:现在密码为123
--------------------
master1:
修改配置文件:
#vim /etc/my.cnf
[mysqld]
log-bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1
授权远程账户:
mysql> grant replication slave,super,reload on *.* to slave@'%' identified by 'Qingyang@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
master2:
修改配置文件
# vim /etc/my.cnf
[mysqld]
log-bin
server-id=2
gtid_mode=ON
enforce_gtid_consistency=1
授权远程账户:
mysql> grant replication slave,super,reload on *.* to slave@'%' identified by 'Qingyang@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
重启master1和master2上的mysqld:
[root@master1 mysql]# systemctl restart mysqld
[root@master2 mysql]# systemctl restart mysqld
master:
# mysql -u root -p'Qingyang@123'
mysql > change master to
master_host='master2',
master_user='slave',
master_password='Qingyang@123',
master_auto_position=1;
mysql > start slave; //启动slave角色
mysql > show slave status\G //查看状态
master2:
# mysql -u root -p'Qingyang@123'
mysql > change master to
master_host='master1',
master_user='slave',
master_password='Qingyang@123',
master_auto_position=1;
mysql > start slave; //启动slave角色
mysql > show slave status\G //查看状态
到此,互为主从配置成功!
接下来配置两台slave:
slave1:
修改配置文件
# vim /etc/my.cnf
[mysqld]
log-bin
server-id=3
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
重启服务:
# systemctl restart mysqld
slave2:
修改配置文件
# vim /etc/my.cnf
[mysqld]
log-bin
server-id=4
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
重启服务:
# systemctl restart mysqld
slave1:
# mysql -u root -p'Qingyang@123'
mysql> change master to master_host='master1',master_user='slave',master_password='Qingyang@123',master_auto_position=1 for channel 'master1';
mysql> change master to master_host='master2',master_user='slave',master_password='Qingyang@123',master_auto_position=1 for channel 'master2';
mysql> start slave;
slave2:
# mysql -u root -p'Qingyang@123'
mysql> change master to master_host='master1',master_user='slave',master_password='Qingyang@123',master_auto_position=1 for channel 'master1';
mysql> change master to master_host='master2',master_user='slave',master_password='Qingyang@123',master_auto_position=1 for channel 'master2';
mysql> start slave;
两个slave分别查看状态:
mysql> show slave status \G
测试:
1.只要slave状态中i/o进程和sql进程为YES,基本可以确定成功
2.主服务器上写入数据,在从服务器上查看,如果能在从上看到数据,则成功!
排错
mysql > stop slave;
mysql > reset master;
mysql > reset slave;