环境介绍:
角色 | IP地址 | 主机名 | server_id | MySQL版本 | 操作系统 |
---|---|---|---|---|---|
master | 192.168.149.149 | yulong-master | 149 | MySQL 8.0.18 | CentOS7.7 |
slave | 192.168.149.153 | yulong-slave01 | 153 | MySQL 8.0.18 | CentOS7.7 |
slave | 192.168.149.154 | yulong-slave02 | 154 | MySQL 8.0.18 | CentOS7.7 |
###查看mysql的版本
[root@yulong-master ~]# mysql -V
mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
###将虚拟机的ip改为静态ip(三个虚拟机)
[root@yulong-master ~]# nmcli connection modify ens33 ipv4.addresses 192.168.149.149/24 ipv4.gateway 192.168.149.2 ipv4.dns 192.168.149.2 ipv4.method manual
[root@yulong-master ~]# nmcli connection up ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)
[root@yulong-master ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=76.2 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=128 time=93.6 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=128 time=75.2 ms
^C
--- 8.8.8.8 ping statistics ---
6 packets transmitted, 3 received, 50% packet loss, time 5003ms
##关掉selinux
[root@yulong-master ~]# setenforce 0
[root@yulong-master ~]# getenforce
Permissive
##修改hosts文件(三个虚拟机)
[root@yulong-master ~]# vim /etc/hosts
192.168.149.149 yulong-master
192.168.149.153 yulong-slave01
192.168.149.154 yulong-slave02
##配置ssh免秘钥登录(三个虚拟机)
[root@yulong-master ~]# ssh-keygen
[root@yulong-master ~]# vim ssh-copy.sh
#!/usr/bin/bash
for i in 149 153 154
do
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected].$i
done
[root@yulong-master ~]# chmod 777 ssh-copy.sh
[root@yulong-master ~]# ./ssh-copy.sh
[root@yulong-master ~]# vim check-ssh.sh
#!/usr/bin/bash
for i in 149 153 154
do
ssh 192.168.149.$i hostname
done
[root@yulong-master ~]# chmod 777 check-ssh.sh
[root@yulong-master ~]# ./check-ssh.sh
yulong-master
yulong-slave01
yulong-slave02
####修改master的配置文件
##随机生成一个uuid
[root@yulong-master ~]# mysql -uroot -p123456.Com -e 'select uuid()'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------------------------+
| uuid() |
+--------------------------------------+
| d5e927ac-5f5f-11ea-bb5e-000c294276a2 |
+--------------------------------------+
(注意:要确保master配置文件中的组复制的名字,master虚拟机,slave01,slave02的uuid都不相同)
[root@yulong-master ~]# vim /etc/my.cnf
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=149
log_bin=/var/lib/mysql/binlogs/master
log_bin_index=/var/lib/mysql/binlogs/master.index
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
plugin_load_add='group_replication.so'
group_replication_group_name="d5e927ac-5f5f-11ea-bb5e-000c294276a2"
group_replication_start_on_boot=off
group_replication_local_address= "192.168.149.149:33061"
group_replication_group_seeds="192.168.149.149:33061,192.168.149.153:33061,192.168.149.154:33061"
group_replication_bootstrap_group=off
[root@yulong-master ~]# mkdir -p /var/lib/mysql/binlogs/master
[root@yulong-master ~]# chmod 777 /var/lib/mysql/binlogs
##防火墙放行33061端口,mysql服务
[root@yulong-master ~]# firewall-cmd --add-port=33061/tcp --permanent
success
[root@yulong-master ~]# firewall-cmd --add-service=mysql --permanent
success
[root@yulong-master ~]# firewall-cmd --reload
success
[root@yulong-master ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client mysql ntp ssh
ports: 80/tcp 8000/tcp 3306/tcp 33061/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
##重启服务
[root@yulong-master ~]# systemctl restart mysqld
####创建用于分布式恢复的复制用户
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user rpl_user@'%' identified with mysql_native_password by 'Com.123456';
Query OK, 0 rows affected (0.01 sec)
##授权
mysql> grant replication slave on *.* to 'rpl_user'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> grant backup_admin on *.* to 'rpl_user'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
##创建用户后
mysql> change master to master_user='rpl_user',master_password='Com.123456' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.03 sec)
##查看用户
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| rpl_user | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
####自举启动组
mysql> set global group_replication_bootstrap_group=on;
Query OK, 0 rows affected (0.00 sec)
mysql> start group_replication;
Query OK, 0 rows affected (3.49 sec)
mysql> set global group_replication_bootstrap_group=off;
Query OK, 0 rows affected (0.00 sec)
##检查组信息及组成员
mysql> mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
| group_replication_applier | cd16d921-52ee-11ea-a55e-000c29040228 | yulong-master | 3306 | ONLINE | PRIMARY | 8.0.19 |
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
1 row in set (0.00 sec)
###测试,创建数据库和表并插入数据
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table t1 (c1 int primary key, c2 text not null);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values(1,'lisi');
Query OK, 1 row affected (0.12 sec)
mysql> insert into t1 values(2,'zhangsan');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(3,'wangwu');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+----------+
| c1 | c2 |
+----+----------+
| 1 | lisi |
| 2 | zhangsan |
| 3 | wangwu |
+----+----------+
3 rows in set (0.00 sec)
##查看二进制日志
mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+------------------------------------------+
| master.000001 | 1689 | | | d5e927ac-5f5f-11ea-bb5e-000c294276a2:1-6 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'master.000001' limit 10;
+---------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------+
| master.000001 | 4 | Format_desc | 149 | 124 | Server ver: 8.0.19, Binlog ver: 4 |
| master.000001 | 124 | Previous_gtids | 149 | 151 | |
| master.000001 | 151 | Gtid | 149 | 233 | SET @@SESSION.GTID_NEXT= 'd5e927ac-5f5f-11ea-bb5e-000c294276a2:1' |
| master.000001 | 233 | Query | 149 | 295 | BEGIN |
| master.000001 | 295 | View_change | 149 | 394 | view_id=15834743172730389:1 |
| master.000001 | 394 | Query | 149 | 462 | COMMIT |
| master.000001 | 462 | Gtid | 149 | 542 | SET @@SESSION.GTID_NEXT= 'd5e927ac-5f5f-11ea-bb5e-000c294276a2:2' |
| master.000001 | 542 | Query | 149 | 646 | create database test /* xid=32 */ |
| master.000001 | 646 | Gtid | 149 | 726 | SET @@SESSION.GTID_NEXT= 'd5e927ac-5f5f-11ea-bb5e-000c294276a2:3' |
| master.000001 | 726 | Query | 149 | 864 | use `test`; create table t1 (c1 int primary key, c2 text not null) /* xid=37 */ |
+---------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
#####添加第二台服务器
##修改slave01的配置文件
(注意:这里slave01里边的UUID要和master上的UUID一样)
[root@yulong-slave01 ~]# vim /etc/my.cnf
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=153
log_bin=/var/lib/mysql/binlogs/slave01
log_bin_index=/var/lib/mysql/binlogs/slave01.index
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="d5e927ac-5f5f-11ea-bb5e-000c294276a2"
group_replication_start_on_boot=off
group_replication_local_address= "192.168.149.153:33061"
group_replication_group_seeds="192.168.149.149:33061,192.168.149.153:33061,192.168.149.154:33061"
group_replication_bootstrap_group=off
[root@yulong-slave01 ~]# mkdir -p /var/lib/mysql/binlogs/slave01
[root@yulong-slave01 ~]# chmod 777 /var/lib/mysql/binlogs/
[root@yulong-slave01 ~]# firewall-cmd --add-port=33061/tcp --permanent
success
[root@yulong-slave01 ~]# firewall-cmd --add-service=mysql --permanent
success
[root@yulong-slave01 ~]# firewall-cmd --reload
success
###重启服务
[root@yulong-slave01 ~]# systemctl restart mysqld
##创建复制用户
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user rpl_user@'%' identified with mysql_native_password by 'Com.123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user',MASTER_PASSWORD='Com.123456' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
##启动复制组,将slave01加入组
mysql> start group_replication;
Query OK, 0 rows affected (4.39 sec)
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 7e81a64b-5f59-11ea-a95c-000c29c95b40 | yulong-slave01 | 3306 | ONLINE | SECONDARY | 8.0.19 |
| group_replication_applier | cd16d921-52ee-11ea-a55e-000c29040228 | yulong-master | 3306 | ONLINE | PRIMARY | 8.0.19 |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
2 rows in set (0.00 sec)
####查看数据同步结果
mysql> show databases like 'test';
+-----------------+
| Database (test) |
+-----------------+
| test |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from test.t1;
+----+----------+
| c1 | c2 |
+----+----------+
| 1 | lisi |
| 2 | zhangsan |
| 3 | wangwu |
+----+----------+
3 rows in set (0.00 sec)
######添加第三台服务器
##修改slave02的配置文件
(这里的uuid也要和master保持一致)
[root@yulong-slave02 ~]# vim /etc/my.cnf
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=154
log_bin=/var/lib/mysql/binlogs/slave02
log_bin_index=/var/lib/mysql/binlogs/slave02.index
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="d5e927ac-5f5f-11ea-bb5e-000c294276a2"
group_replication_start_on_boot=off
group_replication_local_address= "192.168.149.154:33061"
group_replication_group_seeds="192.168.149.149:33061,192.168.149.153:33061,192.168.149.154:33061"
group_replication_bootstrap_group=off
[root@yulong-slave02 ~]# mkdir -p /var/lib/mysql/binlogs/slave02
[root@yulong-slave02 ~]# chmod 777 /var/lib/mysql/binlogs
[root@yulong-slave02 ~]# firewall-cmd --add-service=mysql --permanent
success
[root@yulong-slave02 ~]# firewall-cmd --add-port=33061/tcp --permanent
success
[root@yulong-slave02 ~]# firewall-cmd --reload
success
[root@yulong-slave02 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client mysql ssh
ports: 33061/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@yulong-slave02 ~]# systemctl restart mysqld
####创建复制用户
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user 'rpl_user'@'%' identified with mysql_native_password by'Com.123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'rpl_user'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> grant backup_admin on *.* to 'rpl_user'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_user='rpl_user',master_password='Com.123456' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
###启动复制组,将slave02加入组
mysql> start group_replication;
Query OK, 0 rows affected (4.09 sec)
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 7116a128-5f60-11ea-9dbd-000c29643b80 | yulong-slave02 | 3306 | ONLINE | SECONDARY | 8.0.19 |
| group_replication_applier | 7e81a64b-5f59-11ea-a95c-000c29c95b40 | yulong-slave01 | 3306 | ONLINE | SECONDARY | 8.0.19 |
| group_replication_applier | cd16d921-52ee-11ea-a55e-000c29040228 | yulong-master | 3306 | ONLINE | PRIMARY | 8.0.19 |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
###查看同步结果
mysql> show databases like 'test';
+-----------------+
| Database (test) |
+-----------------+
| test |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from test.t1;
+----+----------+
| c1 | c2 |
+----+----------+
| 1 | lisi |
| 2 | zhangsan |
| 3 | wangwu |
+----+----------+
3 rows in set (0.00 sec)
######## 测试
##停掉master的服务
[root@yulong-master ~]# systemctl stop mysqld
##在slave01上查看组的情况
mysql> mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 7116a128-5f60-11ea-9dbd-000c29643b80 | yulong-slave02 | 3306 | ONLINE | PRIMARY | 8.0.19 |
| group_replication_applier | 7e81a64b-5f59-11ea-a95c-000c29c95b40 | yulong-slave01 | 3306 | ONLINE | SECONDARY | 8.0.19 |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
2 rows in set (0.00 sec)
##修改三个虚拟机的配置文件
[root@yulong-master ~]# vim /etc/my.cnf
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
##重启服务
[root@yulong-master ~]# systemctl restart mysqld
##启动复制组(任意选择一个即可)
mysql> set global group_replication_bootstrap_group=on;
Query OK, 0 rows affected (0.00 sec)
mysql> start group_replication;
Query OK, 0 rows affected (3.21 sec)
mysql> set global group_replication_bootstrap_group=off;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
| group_replication_applier | cd16d921-52ee-11ea-a55e-000c29040228 | yulong-master | 3306 | ONLINE | PRIMARY | 8.0.19 |
+---------------------------+--------------------------------------+-----------------+-------------+--------------+-------------+----------------+
1 row in set (0.00 sec)
###将剩下的节点加入复制组
mysql> start group_replication;
Query OK, 0 rows affected (4.88 sec)
##查看复制组情况
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 7116a128-5f60-11ea-9dbd-000c29643b80 | yulong-slave02 | 3306 | ONLINE | PRIMARY | 8.0.19 |
| group_replication_applier | 7e81a64b-5f59-11ea-a95c-000c29c95b40 | yulong-slave01 | 3306 | ONLINE | PRIMARY | 8.0.19 |
| group_replication_applier | cd16d921-52ee-11ea-a55e-000c29040228 | yulong-master | 3306 | ONLINE | PRIMARY | 8.0.19 |
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)