主机环境:rhel7.3 selinux and firewalld disabled
主机名 | ip | 服务 |
---|---|---|
server3 | 172.25.254.3 | mysql |
server4 | 172.25.254.4 | mysql |
server1 | 172.25.254.1 | mysql |
一、基础概念
MySQL组复制是一个MySQL插件,它基于常规的MySQL复制,利用了基于行格式的二进制日志和GTID等特性。
MySQL组复制是MySQL 5.7.17开始引入的新功能,为主从复制实现高可用功能。它支持单主模型和多主模型两种工作方式(默认是单主模型)。
-
单主模型:从复制组中众多个MySQL节点中自动选举一个master节点,只有master节点可以写,其他节点自动设置为read
only。当master节点故障时,会自动选举一个新的master节点,选举成功后,它将设置为可写,其他slave将指向这个新的master。 -
多主模型:复制组中的任何一个节点都可以写,因此没有master和slave的概念,只要突然故障的节点数量不太多,这个多主模型就能继续可用。
参考:https://blog.51cto.com/ylw6006/1971139
搭建步骤:
1.关闭mysql,删除信息(看uuid),修改配置文件
[root@server3 ~]# systemctl stop mysqld
[root@server3 ~]# cat /var/lib/mysql/auto.cnf
[auto]
server-uuid=cc954647-67a4-11e9-95a0-525400c7087d
[root@server3 ~]# rm -rf /var/lib/mysql/*
##参考官网 https://dev.mysql.com/doc/refman/5.7/en/group-replication-configuring-instances.html
[root@server3 ~]# vim /etc/my.cnf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE #关闭binlog校验
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW #组复制依赖基于行的复制格式
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="cc954647-67a4-11e9-95a0-525400c7087d" ##可以看/var/lib/mysql/auto.cnf
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.254.3:24901"
loose-group_replication_group_seeds= "172.25.254.3:24901,172.25.254.4:24901,172.25.254.1:24901"
loose-group_replication_bootstrap_group=off ##插件是否自动引导,这个选项一般都要off掉,只需要由发起组复制的节点开启,并只启动一次,如果是on,下次再启动时,会生成一个同名的组,可能会发生脑裂
loose-group_replication_ip_whitelist="127.0.0.1,172.25.254.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF #后两行是开启多主模式的参数
2.启动mysqld,修改安全设定
[root@server3 ~]# systemctl start mysqld
[root@server3 ~]# cat /var/log/mysqld.log | grep password
[root@server3 ~]# mysql_secure_installation
3.登陆mysql,进行配置设定(#看官网复制 https://dev.mysql.com/doc/refman/5.7/en/group-replication-user-credentials.html)
[root@server3 ~]# mysql -uroot -pWestos+001
mysql> SET SQL_LOG_BIN=0; #关闭二进制日志,防止传到其他server上
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1; ##开启日志
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+001' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> SHOW PLUGINS; ##查看插件
| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
mysql> SET GLOBAL group_replication_bootstrap_group=ON; ##组复制发起节点开启这个参数
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
mysql> SELECT * FROM performance_schema.replication_group_members; ##查看server3是否online
4.添加server4到组内
server4上:
将sercer3上的配置文件发送过来修改
01.关闭mysql,删除信息,修改配置文件
[root@server4 ~]# systemctl stop mysqld
[root@server4 ~]# rm -rf /var/lib/mysql/*
[root@server4 ~]# vim /etc/my.cnf
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="cc954647-67a4-11e9-95a0-525400c7087d" ##注意:这里和server3的保持一致!!!!!!
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.254.4:24901"
loose-group_replication_group_seeds= "172.25.254.3:24901,172.25.254.4:24901,172.25.254.1:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.254.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
02.启动mysqld,修改安全设定
[root@server4 ~]# systemctl start mysqld
[root@server4 ~]# cat /var/log/mysqld.log | grep password
[root@server4 ~]# mysql_secure_installation
03.登陆mysql,进行配置设定
[root@server4 ~]# mysql -uroot -pWestos+001
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+001' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
注意: #这里不需要做server1上做的SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
'这里开启组复制可能会报错,查看mysql日志'
'cat /var/log/mysqld.log ,找到解决办法'
2019-03-14T08:51:52.838185Z 0 [Note] Plugin group_replication reported: 'To force this member into the group you can use the group_replication_allow_local_disjoint_gtids_join option'
[root@server4 ~]# mysql -uroot -pWestos+001
mysql> STOP GROUP_REPLICATION;
mysql> set global group_replication_allow_local_disjoint_gtids_join=on; ##直接做这一步 不用等报错
mysql> START GROUP_REPLICATION;
注意:此时恢复正常
在server3上查看是否online
5.添加server1到组内
此处server1是全新的所以需要先安装mysql,这里我们采取了准备好的rpm包安装,不需要进行源码编译
01.获得资源,解压,安装
[root@server1 mysql]# tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@server1 mysql]# yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm
02.修改配置文件,启动mysqld,进行安全设定(将server4的配置文件发过来一份)
[root@server1 ~]# vim /etc/my.cnf
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="b62276d1-671e-11e9-b032-52540077fc54" ##注意:这里和server3的保持一致!!!!!!
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.254.1:24901"
loose-group_replication_group_seeds= "172.25.254.3:24901,172.25.254.4:24901,172.25.254.1:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.254.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
[root@server1 mysql]# systemctl start mysqld
[root@server1 mysql]# cat /var/log/mysqld.log | grep password
[root@server1 mysql]# mysql_secure_installation
03.登陆mysql,进行配置设定
[root@server1 ~]# mysql -uroot -pWestos+001
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+001' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
注意: #这里不需要做server1上做的SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
'这里开启组复制可能会报错,查看mysql日志'
'cat /var/log/mysqld.log ,找到解决办法'
2019-03-14T08:51:52.838185Z 0 [Note] Plugin group_replication reported: 'To force this member into the group you can use the group_replication_allow_local_disjoint_gtids_join option'
[root@server1 ~]# mysql -uroot -pWestos+001
mysql> STOP GROUP_REPLICATION;
mysql> set global group_replication_allow_local_disjoint_gtids_join=on; ##直接做这一步 不用等报错
mysql> START GROUP_REPLICATION;
注意:此时恢复正常
看到3台都是online,表示正常
这时,在任何节点写入数据,其他节点都能看到
测试:
在server3加入数据
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
mysql> INSERT INTO t1 VALUES (1, ‘Luis’);
mysql> SELECT * FROM t1;
此时在哪一台上都可以看到加入的信息
又如在server4上插入:
mysql> INSERT INTO t1 VALUES (2, ‘wsp’);
在server3和server1上查看:
mysql> select * from t1;