MySQL MGR(单主模式+多主模式)配置文档

拓扑图介绍:

单主模式

192.168.43.141  MGR01  (读写)

192.168.43.142  MGR02 (读)

192.168.43.143  MGR03 (读)

多主模式

192.168.43.141  MGR01  (读写)

192.168.43.142  MGR02 (读写)

192.168.43.143  MGR03 (读写)

OS         : CentOS release 6.10 (Final) min

MySQL : mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar


一、系统基本设置(所有节点)

1.iptables及selinux设置

[root@mgr01 opt]# /etc/init.d/iptables status

iptables: Firewall is not running.

[root@mgr01 opt]# chkconfig --list | grep iptables

iptables        0:off  1:off  2:off  3:off  4:off  5:off  6:off

[root@mgr01 opt]# getenforce

Disabled

2.ntp设置

[root@mgr01 opt]# chkconfig --list | grep ntpd

ntpd            0:off  1:off  2:on    3:on    4:on    5:on    6:off

ntpdate        0:off  1:off  2:off  3:off  4:off  5:off  6:off

[root@mgr01 opt]#

3./etc/hosts设置

[root@mgr01 opt]# cat /etc/hosts

127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4

::1        localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.43.141  MGR01

192.168.43.142  MGR02

192.168.43.143  MGR03

[root@mgr01 opt]#

4.安装基本工具

[root@mgr01 opt]# yum install ntp vim wget net-tools mlocate openssh-* cc gcc make -y


二、安装MySQL(所有节点)

1.解压并安装

[root@mgr01 opt]# tar -xf mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar

[root@mgr01 opt]# ls

mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar

mysql-community-client-5.7.31-1.el6.x86_64.rpm

mysql-community-common-5.7.31-1.el6.x86_64.rpm

mysql-community-devel-5.7.31-1.el6.x86_64.rpm

mysql-community-embedded-5.7.31-1.el6.x86_64.rpm

mysql-community-embedded-devel-5.7.31-1.el6.x86_64.rpm

mysql-community-libs-5.7.31-1.el6.x86_64.rpm

mysql-community-libs-compat-5.7.31-1.el6.x86_64.rpm

mysql-community-server-5.7.31-1.el6.x86_64.rpm

mysql-community-test-5.7.31-1.el6.x86_64.rpm

[root@mgr01 opt]#

[root@mgr01 opt]# yum install -y mysql-community-*

[root@mgr01 opt]# /etc/init.d/mysqld start

Initializing MySQL database:                              [  OK  ]

Starting mysqld:                                          [  OK  ]

[root@mgr01 opt]#

2.修改root密码

[root@mgr01 opt]# cat /var/log/mysqld.log|grep 'A temporary password'

2020-08-20T15:09:37.350937Z 1 [Note] A temporary password is generated for root@localhost: sl,:WvZDI9Bx

[root@mgr01 opt]# mysql -uroot -p'sl,:WvZDI9Bx'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.31

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by 'Test.123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql>


三、配置MySQL

1.修改配置文件

MGR01上修改:

[root@mgr01 opt]# cat /etc/my.cnf

[mysqld]

datadir = /var/lib/mysql

socket = /var/lib/mysql/mysql.sock

symbolic-links = 0

log-error = /var/log/mysqld.log

pid-file = /var/run/mysqld/mysqld.pid

server_id=1

gtid_mode=ON

enforce_gtid_consistency=ON

binlog_checksum=NONE

log_bin=binlog

log_slave_updates=ON

binlog_format=ROW

master_info_repository=TABLE

relay_log_info_repository=TABLE

transaction_write_set_extraction=XXHASH64

loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"

loose-group_replication_start_on_boot=off

loose-group_replication_local_address="192.168.43.141:24901"

loose-group_replication_group_seeds="192.168.43.141:24901,192.168.43.142:24901,192.168.43.143:24901"

loose-group_replication_bootstrap_group=off

report_host=192.168.43.141

report_port=3306

[root@mgr01 opt]# /etc/init.d/mysqld restart

Stopping mysqld:                                          [  OK  ]

Starting mysqld:                                          [  OK  ]

[root@mgr01 opt]#

MGR02上修改:

[root@mgr02 opt]# cat /etc/my.cnf

[mysqld]

datadir = /var/lib/mysql

socket = /var/lib/mysql/mysql.sock

symbolic-links = 0

log-error = /var/log/mysqld.log

pid-file = /var/run/mysqld/mysqld.pid

server_id=2

gtid_mode=ON

enforce_gtid_consistency=ON

binlog_checksum=NONE

log_bin=binlog

log_slave_updates=ON

binlog_format=ROW

master_info_repository=TABLE

relay_log_info_repository=TABLE

transaction_write_set_extraction=XXHASH64

loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"

loose-group_replication_start_on_boot=off

loose-group_replication_local_address="192.168.43.142:24901"

loose-group_replication_group_seeds="192.168.43.141:24901,192.168.43.142:24901,192.168.43.143:24901"

loose-group_replication_bootstrap_group=off

report_host=192.168.43.142

report_port=3306

[root@mgr02 opt]# /etc/init.d/mysqld restart

Stopping mysqld:                                          [  OK  ]

Starting mysqld:                                          [  OK  ]

[root@mgr02 opt]#

MGR03上修改:

[root@mgr03 opt]# cat /etc/my.cnf

[mysqld]

datadir = /var/lib/mysql

socket = /var/lib/mysql/mysql.sock

symbolic-links = 0

log-error = /var/log/mysqld.log

pid-file = /var/run/mysqld/mysqld.pid

server_id=3

gtid_mode=ON

enforce_gtid_consistency=ON

binlog_checksum=NONE

log_bin=binlog

log_slave_updates=ON

binlog_format=ROW

master_info_repository=TABLE

relay_log_info_repository=TABLE

transaction_write_set_extraction=XXHASH64

loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"

loose-group_replication_start_on_boot=off

loose-group_replication_local_address="192.168.43.143:24901"

loose-group_replication_group_seeds="192.168.43.141:24901,192.168.43.142:24901,192.168.43.143:24901"

loose-group_replication_bootstrap_group=off

report_host=192.168.43.143

report_port=3306

[root@mgr03 opt]# /etc/init.d/mysqld restart

Stopping mysqld:                                          [  OK  ]

Starting mysqld:                                          [  OK  ]

[root@mgr03 opt]#

2.安装MGR插件,设置复制账号(所有MGR节点都要执行)

[root@mgr01 opt]# mysql -uroot -p'Test.123'

mysql: [Warning] Using a password on the command line interface can be i                                                                            nsecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reser                                      ved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input stat                                                ement.

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.05 sec)

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

mysql>  CREATE USER repl@'%' IDENTIFIED BY 'Test.123';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

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='repl', MASTER_PASSWORD='Test.123' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql>

3.启动MGR单主模式

这里:我们将MGR01设置为主,即读写数据库服务器

以下为:MGR01上的操作

mysql> SET GLOBAL group_replication_bootstrap_group=ON;

Query OK, 0 rows affected (0.01 sec)

mysql>  START GROUP_REPLICATION;

Query OK, 0 rows affected (2.16 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 |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

1 row in set (0.00 sec)

以下为:MGR02和MGR03上的操作

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (5.82 sec)

以下为:MGR01上的操作

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

3 rows in set (0.00 sec)


四、测试功能

1.验证MGR单主模式下节点数据的同步

MGR01进行写入操作:

mysql> show databases;

+--------------------+

| Database          |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)

mysql> CREATE DATABASE kevin CHARACTER SET utf8 COLLATE utf8_general_ci;

Query OK, 1 row affected (0.01 sec)

mysql> use kevin;

Database changed

mysql>  create table if not exists test (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);

Query OK, 0 rows affected (0.03 sec)

mysql> insert into kevin.test values(1,"wangshibo"),(2,"guohuihui"),(3,"yangyang"),(4,"shikui");

Query OK, 4 rows affected (0.09 sec)

Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

+----+-----------+

4 rows in set (0.00 sec)

mysql>

MGR01与MGR02的查询:

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

+----+-----------+

4 rows in set (0.00 sec)

观察可知:已经同步

2.从节点不可写测试

以下为在MGR02和MGR03上的操作:

mysql> delete from kevin.test where id>3;

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement


五、单主模式切换多主模式

(一)停止单主模式,开启多主模式

1.停止组复制(在所有MGR节点上执行):

mysql> stop group_replication;

Query OK, 0 rows affected (9.08 sec)

mysql> set global group_replication_single_primary_mode=OFF;

Query OK, 0 rows affected (0.00 sec)

mysql> set global group_replication_enforce_update_everywhere_checks=ON;

Query OK, 0 rows affected (0.00 sec)

2.随便选择某个MGR节点执行 (比如这里选择在MGR01节点):

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 (2.20 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

3.然后在其他的MGR节点执行 (这里指MGR02和MGR03节点上执行):

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected, 1 warning (5.89 sec)

4.查看MGR组信息 (在任意一个MGR节点上都可以查看)

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

3 rows in set (0.00 sec)

(二)验证MGR多主模式的节点数据同步

MGR01读写:

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

+----+-----------+

4 rows in set (0.00 sec)

mysql> insert into kevin.test values(5,"pony"),(6,"jack"),(7,"bob"),(8,"allen");

Query OK, 4 rows affected (0.02 sec)

Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  5 | pony      |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

+----+-----------+

8 rows in set (0.00 sec)

MGR02读写:

mysql> insert into kevin.test values(9,"ponyMa"),(10,"jackMa"),(11,"Frank");

Query OK, 3 rows affected (0.02 sec)

Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  5 | pony      |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

|  9 | ponyMa    |

| 10 | jackMa    |

| 11 | Frank    |

+----+-----------+

11 rows in set (0.00 sec)

mysql> delete from kevin.test where id > 8;

Query OK, 3 rows affected (0.03 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  5 | pony      |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

+----+-----------+

8 rows in set (0.00 sec)

mysql>

MGR03读写:

mysql> insert into kevin.test values(12,"Bill"),(13,"Smarker");

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  5 | pony      |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

+----+-----------+

10 rows in set (0.00 sec)

mysql> delete from kevin.test where name='pony';

Query OK, 1 row affected (0.00 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

+----+-----------+

9 rows in set (0.00 sec)

mysql>


六、多主模式切换单主模式

(一)切换为单主模式

1.停止组复制(在所有MGR节点上执行):

mysql> stop group_replication;

Query OK, 0 rows affected (9.29 sec)

mysql> set global group_replication_enforce_update_everywhere_checks=OFF;

Query OK, 0 rows affected (0.00 sec)

mysql> set global group_replication_single_primary_mode=ON;

Query OK, 0 rows affected (0.00 sec)

2.选择一个节点作为主节点, 在主节点上执行 (这里选择MGR-node1节点作为主节点)

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 (2.12 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

3.在其他剩余的节点, 也就是从库节点上执行 (这里从库节点指的就是MGR-node2和MGR-node3):

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected, 1 warning (6.16 sec)

4.查看MGR组信息 (在任意一个MGR节点上都可以查看)

mysql>  SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

3 rows in set (0.00 sec)

(二)测试单主模式功能

1.MGR01读写正常:

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

+----+-----------+

9 rows in set (0.00 sec)

mysql> insert into kevin.test values(14,"pony");

Query OK, 1 row affected (0.04 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

+----+-----------+

10 rows in set (0.00 sec)

2.MGR01与MGR02无法写入

mysql> insert into kevin.test values(15,"Jacker");

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

+----+-----------+

10 rows in set (0.00 sec)


七、故障模拟及过程分析

(一)模拟故障

我们这里模拟MGR01的MySQL服务挂掉以后,单主模式下集群的情况。

1.关闭MGR01上的MySQL服务

[root@mgr01 opt]# /etc/init.d/mysqld stop

Stopping mysqld:                                          [  OK  ]

[root@mgr01 opt]# /etc/init.d/mysqld status

mysqld is stopped

[root@mgr01 opt]#

2.这里,我们在MGR02进行读写操作,是正常的。

说明此时已经选举MGR-node2节点为新的主节点了

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

2 rows in set (0.00 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

+----+-----------+

10 rows in set (0.01 sec)

mysql> insert into kevin.test values(15,"Jacker");

Query OK, 1 row affected (0.01 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

| 15 | Jacker    |

+----+-----------+

11 rows in set (0.00 sec)

mysql>

3.MGR03节点还是从节点, 只能读不能写

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

| 15 | Jacker    |

+----+-----------+

11 rows in set (0.00 sec)

mysql> insert into kevin.test values(120,"Bill"),(130,"Smarker");

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

mysql>

(二)故障恢复后如何处理?

1.恢复MGR01的MySQL服务,手动激活下该节点的组复制功能

[root@mgr01 opt]# /etc/init.d/mysqld start

Starting mysqld:                                          [  OK  ]

[root@mgr01 opt]# /etc/init.d/mysqld status

mysqld (pid  3087) is running...

[root@mgr01 opt]#

[root@mgr01 opt]# mysql -uroot -p'Test.123'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (2.79 sec)

mysql>  SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

3 rows in set (0.00 sec)

2.观察MGR01的读写情况

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

|  4 | shikui    |

|  6 | jack      |

|  7 | bob      |

|  8 | allen    |

| 12 | Bill      |

| 13 | Smarker  |

| 14 | pony      |

| 15 | Jacker    |

+----+-----------+

11 rows in set (0.00 sec)

mysql> insert into kevin.test values(140,"Mapony");

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

mysql>

我们发现:MGR01节点恢复后, 则变为了从库节点, 只能读不能写.

如果从节点挂了, 恢复后, 只需要手动激活下该节点的组复制功能("START GROUP_REPLICATION;"),

即可正常加入到MGR组复制集群内并自动同步其他节点数据.

(三)多主模式模式下的故障模拟与测试

1.按照第五节的内容进行单主转多主模式(略)

2.任意挂掉一台服务器的MySQL服务,我们选MGR03进行测试

[root@mgr03 opt]# /etc/init.d/mysqld stop

Stopping mysqld:                                          [  OK  ]

[root@mgr03 opt]#

3.测试发现其他2节点读写正常

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

2 rows in set (0.00 sec)

mysql> select * from kevin.test;

+------+-----------+

| id  | name      |

+------+-----------+

|    1 | wangshibo |

|    2 | guohuihui |

|    3 | yangyang  |

|    4 | shikui    |

|    6 | jack      |

|    7 | bob      |

|    8 | allen    |

|  12 | Bill      |

|  13 | Smarker  |

|  14 | pony      |

|  15 | Jacker    |

| 1200 | Bill      |

| 1300 | Smarker  |

| 1400 | Mapony    |

| 1500 | Jacker    |

+------+-----------+

15 rows in set (0.00 sec)

mysql> insert into kevin.test values(15000,"Jacker");

Query OK, 1 row affected (0.01 sec)

mysql> select * from kevin.test;

+-------+-----------+

| id    | name      |

+-------+-----------+

|    1 | wangshibo |

|    2 | guohuihui |

|    3 | yangyang  |

|    4 | shikui    |

|    6 | jack      |

|    7 | bob      |

|    8 | allen    |

|    12 | Bill      |

|    13 | Smarker  |

|    14 | pony      |

|    15 | Jacker    |

|  1200 | Bill      |

|  1300 | Smarker  |

|  1400 | Mapony    |

|  1500 | Jacker    |

| 15000 | Jacker    |

+-------+-----------+

16 rows in set (0.00 sec)

mysql> delete from kevin.test where name='Jacker';

Query OK, 3 rows affected (0.01 sec)

mysql> select * from kevin.test;

+------+-----------+

| id  | name      |

+------+-----------+

|    1 | wangshibo |

|    2 | guohuihui |

|    3 | yangyang  |

|    4 | shikui    |

|    6 | jack      |

|    7 | bob      |

|    8 | allen    |

|  12 | Bill      |

|  13 | Smarker  |

|  14 | pony      |

| 1200 | Bill      |

| 1300 | Smarker  |

| 1400 | Mapony    |

+------+-----------+

13 rows in set (0.00 sec)

mysql>

4.故障恢复处理(恢复MGR03的MySQL服务)

[root@mgr03 opt]# /etc/init.d/mysqld start

Starting mysqld:                                          [  OK  ]

[root@mgr03 opt]# mysql -uroot -p'Test.123'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  SELECT * FROM performance_schema.replication_group_members;

+---------------------------+-----------+-------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+-----------+-------------+-------------+--------------+

| group_replication_applier |          |            |        NULL | OFFLINE      |

+---------------------------+-----------+-------------+-------------+--------------+

1 row in set (0.00 sec)

mysql>  START GROUP_REPLICATION;

ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.

这里我们跟进日志信息进行故障排查:

[root@mgr03 ~]# tail -f /var/log/mysqld.log

2020-08-20T16:47:16.413305Z 0 [ERROR] Plugin group_replication reported: 'The member configuration is not

compatible with the group configuration. Variables such as single_primary_mode or enforce_update_everywhere_checks

must have the same value on every server in the group. (member configuration option: [group_replication_single_primary_mode],

group configuration option: [group_replication_enforce_update_everywhere_checks]).'

mysql> show variables like '%single_primary_mode%';

+---------------------------------------+-------+

| Variable_name                        | Value |

+---------------------------------------+-------+

| group_replication_single_primary_mode | ON    |

+---------------------------------------+-------+

1 row in set (0.02 sec)

mysql> show variables like '%enforce_update_everywhere_checks%';

+----------------------------------------------------+-------+

| Variable_name                                      | Value |

+----------------------------------------------------+-------+

| group_replication_enforce_update_everywhere_checks | OFF  |

+----------------------------------------------------+-------+

1 row in set (0.01 sec)

mysql> set group_replication_enforce_update_everywhere_checks=ON;

ERROR 1229 (HY000): Variable 'group_replication_enforce_update_everywhere_checks' is a GLOBAL variable and should be set with SET GLOBAL

mysql> set group_replication_single_primary_mode=OFF;

ERROR 1229 (HY000): Variable 'group_replication_single_primary_mode' is a GLOBAL variable and should be set with SET GLOBAL

mysql> SET GLOBAL group_replication_enforce_update_everywhere_checks=ON;

ERROR 1231 (42000): Cannot enable enforce_update_everywhere_checks while single_primary_mode is enabled.

mysql> SET GLOBAL group_replication_single_primary_mode=OFF;

Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%enforce_update_everywhere_checks%';

+----------------------------------------------------+-------+

| Variable_name                                      | Value |

+----------------------------------------------------+-------+

| group_replication_enforce_update_everywhere_checks | OFF  |

+----------------------------------------------------+-------+

1 row in set (0.00 sec)

mysql> SET GLOBAL group_replication_enforce_update_everywhere_checks=ON;

Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL group_replication_single_primary_mode=OFF;

Query OK, 0 rows affected (0.00 sec)

mysql>  START GROUP_REPLICATION;

Query OK, 0 rows affected (2.94 sec)

mysql>  SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST    | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

| group_replication_applier | 28c43e2e-e2f7-11ea-97a5-000c29b26880 | 192.168.43.141 |        3306 | ONLINE      |

| group_replication_applier | 28e2ba30-e2f7-11ea-9446-000c29fb7918 | 192.168.43.142 |        3306 | ONLINE      |

| group_replication_applier | 29647021-e2f7-11ea-955d-000c29a92338 | 192.168.43.143 |        3306 | ONLINE      |

+---------------------------+--------------------------------------+----------------+-------------+--------------+

3 rows in set (0.00 sec)

mysql>

mysql> insert into kevin.test values(200,"Test"),(300,"MGr");

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from kevin.test;

+------+-----------+

| id  | name      |

+------+-----------+

|    1 | wangshibo |

|    2 | guohuihui |

|    3 | yangyang  |

|    4 | shikui    |

|    6 | jack      |

|    7 | bob      |

|    8 | allen    |

|  12 | Bill      |

|  13 | Smarker  |

|  14 | pony      |

|  200 | Test      |

|  300 | MGr      |

| 1200 | Bill      |

| 1300 | Smarker  |

+------+-----------+

14 rows in set (0.00 sec)

mysql> delete from kevin.test where id > 3;

Query OK, 11 rows affected (0.01 sec)

mysql> select * from kevin.test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | wangshibo |

|  2 | guohuihui |

|  3 | yangyang  |

+----+-----------+

3 rows in set (0.00 sec)

观察可知:恢复正常。


参阅:

https://cloud.tencent.com/developer/article/1432589

©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,189评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,577评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,857评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,703评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,705评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,620评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,995评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,656评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,898评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,639评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,720评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,395评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,982评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,953评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,195评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,907评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,472评论 2 342