server1(master)节点:
1.关闭mysqld
systemctl stop mysqld
2.删除mysql数据 ##注意 :删除数据之前先复制uuid /var/lib/mysql/auto.cnf
rm -fr /var/lib/mysql
3.修改配置文件#官网 https://dev.mysql.com/doc/refman/5.7/en/group-replication-configuring-instances.html
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="c36cc649-3f17-11e9-960e-525400cf2a01" ##可以看/var/lib/mysql/auto.cnf
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.136.1:24901"
loose-group_replication_group_seeds= "172.25.136.1:24901,172.25.136.2:24901,172.25.136.3:24901"
loose-group_replication_bootstrap_group=off ##插件是否自动引导,这个选项一般都要off掉,只需要由发起组复制的节点开启,并只启动一次,如果是on,下次再启动时,会生成一个同名的组,可能会发生脑裂
loose-group_replication_ip_whitelist="127.0.0.1,172.25.136.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF #后两行是开启多主模式的参数
4.启动mysqld
systemctl start mysqld
5.初始化数据库
[root@server1 mysql]# grep password /var/log/mysqld.log
2019-03-14T07:50:42.947487Z 1 [Note] A temporary password is generated for root@localhost: J)hjM=V39sw>
登录数据库,先修改密码
mysql> alter user root@localhost identified by 'Westos+001';
6.配置 #看官网复制 https://dev.mysql.com/doc/refman/5.7/en/group-replication-user-credentials.html
mysql> SET SQL_LOG_BIN=0; #关闭二进制日志,防止传到其他server上
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Wsp+123ld';
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='Wsp+123ld' 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; ##查看server1是否online
###注意:先不要添加数据 最后测试的时候再添加
加入数据
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;
##添加server2到组内
server2上:
[root@server2 mysql]# systemctl stop mysqld
rm -fr /var/lib/mysql/*
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="c36cc649-3f17-11e9-960e-525400cf2a01" ##注意:这里和server1的保持一致!!!!!!
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.136.2:24901"
loose-group_replication_group_seeds= "172.25.136.1:24901,172.25.136.2:24901,172.25.136.3:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.136.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
#启动mysqld
systemctl start mysqld
#初始化
grep password /var/log/mysqld.log
#进入数据库
mysql> alter user root@localhost identified by 'Wsp+123ld'; #修改root用户密码
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Wsp+123ld';
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='Wsp+123ld' 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’
mysql -pWsp+123ld
mysql> STOP GROUP_REPLICATION;
mysql> set global group_replication_allow_local_disjoint_gtids_join=on; ##直接做这一步 不用等报错
mysql> START GROUP_REPLICATION;
恢复正常
#再配置server3,和server2配置相同
配置好后在server1上查看
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 43a9e177-46c2-11e9-9b8a-52540039a8e5 | server3 | 3306 | ONLINE |
| group_replication_applier | 926e543f-4635-11e9-bb87-525400cf2a01 | server2 | 3306 | ONLINE |
| group_replication_applier | de5a155a-462d-11e9-b9df-5254004c61ed | server1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
看到3台都是online,表示正常
这时在任何一个节点都可以看到刚才插入的数据
在任何节点写入数据,其他节点也能看到
如在server3上:
mysql> INSERT INTO t1 VALUES (2, 'wsp');
在server2和server1上查看:
mysql> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | Luis |
| 2 | wsp |
+----+------+
可以看到刚插入的数据