mysql集群-MMSS集群
测试环境:centos7u6
master 192.168.190.155
master2 192.168.190.156
slave1 172.16.0.10
slave2 172.16.0.11
所有机器都作以下操做:
修改主机名称
vim /etc/hostname
分别为:master和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
master 192.168.190.155
master2 192.168.190.156
slave1 172.16.0.10
slave2 172.16.0.11
关闭selinux和防火墙
systemctl disable firewalld
systemctl stop firewalld
vim /etc/sysconfig/selinux
将该文件中的SELINUX=enforcing改成SELINUX=disabled
PS:在同步之前要确保这些机子的时间一致
请使用data检查机子时间
给所有机子安装mysql
1、配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vi mysql.repo //原文件为空
[mysql]
name=mysql
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.ustc.edu.cn/mysql-repo/RPM-GPG-KEY-mysql
2、开始安装
[root@master yum.repos.d]# yum clean all
[root@master yum.repos.d]# yum repolist
[root@master yum.repos.d]# yum -y install mysql-community-server
3、启动mysql服务
[root@master yum.repos.d]# systemctl start mysqld
注意:
如遇到启动之后自带密码,不能直接登陆
获取mysql5.7初始启动密码:
# grep 'temporary password' /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
重启服务
systemctl restart mysqld
使用原始密码登陆后修改密码
alter user root@'localhost' identified by '123'
master:
修改配置文件:
#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 '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 '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
重启master和master2上的mysqld:
[root@master mysql]# systemctl restart mysqld
[root@master2 mysql]# systemctl restart mysqld
master:
# mysql -u root -p'123'
mysql > change master to
master_host='master2',
master_user='slave',
master_password='123',
master_auto_position=1;
mysql > start slave; //启动slave角色
mysql > show slave status\G //查看状态
master2:
# mysql -u root -p'123'
mysql > change master to
master_host='master',
master_user='slave',
master_password='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'123'
mysql> change master to master_host='master',master_user='slave',master_password='123',
master_auto_position=1 for channel 'master';
mysql> change master to master_host='master2',master_user='slave',master_password='123',master_auto_position=1 for channel 'master2';
mysql> start slave;
slave2:
# mysql -u root -p'123'
mysql> change master to master_host='master',master_user='slave',master_password='123',
master_auto_position=1 for channel 'master';
mysql> change master to master_host='master2',master_user='slave',master_password='123',master_auto_position=1 for channel 'master2';
mysql> start slave;
两个slave分别查看状态:
mysql> show slave status \G
测试:
1.只要slave状态中i/o进程和sql进程为YES,基本可以确定成功
2.主服务器上写入数据,在从服务器上查看,如果能在从上看到数据,则成功!
如果出现1236错误,请将mysqld关闭后删除所有配置文件(包括库),并修改/etc/my.cnf文件改回初始配置后(把之前新加的代码注释掉即可),重启mysqld服务,
找到新的初始密码使用新的初始密码登录,从新配一次,但这一次我们先不急配两台master,先配置一台master和三台slave,然后在master2上再加上远程授权,
再让第一台master以slace的身份与master2同步,两外两台slave配置master2的主从配置,这样即可成功