【MySQL高可用】MySQL高可用之MGR部署
参考:https://www.xmmup.com/dbbao45mysqlgaokeyongzhimgrconsuljiagoubushu.html
架构介绍
采用MGR集群、和consul服务注册发现实现应用端通过动态dns访问MGR集群,实现数据库高可用,自动化切换的方案。
本次环境一共包含7台机器。其中3台做MySQL的MGR数据节点(也需要安装Consul,作为Consul client)。单主模式,还有3台做Consul server集群。实现Consul的高可用,剩下一台做DNS服务器,用来解析MGR节点域名。
IP | 数据库版本 | 主机名 | 容器名 | 容器映射端口 | Server ID | 实例端口 | MGR通讯端口 | 备注 |
---|---|---|---|---|---|---|---|---|
192.168.68.60 | 8.0.22 | mgr60 | mysql8022mgr33060 | 33060 | 802233060 | 3306 | 33061 | MGR主节点 |
192.168.68.61 | 8.0.22 | mgr61 | mysql8022mgr33061 | 33061 | 802233061 | 3306 | 33061 | MGR节点 |
192.168.68.62 | 8.0.22 | mgr62 | mysql8022mgr33062 | 33062 | 802233062 | 3306 | 33061 | MGR节点 |
搭建MGR
申请3台MGR机器
[root@mysql conf]# docker pull lhrbest/lhrcentos76:8.0
-- 创建主从网络环境
[root@mysql ~]# docker network create --subnet=192.168.68.0/16 mhaclone
8b8e5ae9dd1efbb247323978a3dbcf025415b861b76bd828859ac934aefa77d1
[root@mysql ~]# docker network inspect mhaclone
[
{
"Name": "mhaclone",
"Id": "8b8e5ae9dd1efbb247323978a3dbcf025415b861b76bd828859ac934aefa77d1",
"Created": "2022-10-14T20:55:25.87905019+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.68.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
-- 生成3台CentOS的环境
[root@mysql conf]# docker rm -f mysql8022mgr33060 mysql8022mgr33061 mysql8022mgr33062
[root@mysql conf]# docker run -d --name mysql8022mgr33060 -h mgr60 --network mhaclone --ip 192.168.68.60 --privileged=true -p 33060:3306 -v /sys/fs/cgroup:/sys/fs/cgroup lhrbest/lhrcentos76:8.0 init
575e7d8692eeb9bd702c190d4faf281696eb913611b96cc3eb14f2fd81022b74
[root@mysql conf]# docker run -d --name mysql8022mgr33061 -h mgr61 --network mhaclone --ip 192.168.68.61 --privileged=true -p 33061:3306 -v /sys/fs/cgroup:/sys/fs/cgroup lhrbest/lhrcentos76:8.0 init
73d80f98b3ddd54a5b9c6a4a7c91ae1bf54980e1d7f7aafca571827aff328ae5
[root@mysql conf]# docker run -d --name mysql8022mgr33062 -h mgr62 --network mhaclone --ip 192.168.68.62 --privileged=true -p 33062:3306 -v /sys/fs/cgroup:/sys/fs/cgroup lhrbest/lhrcentos76:8.0 init
3705d9583d6d8f8574a839ddb4e6a46b2055efc86d6e7f0b5b26a074483f6ff6
mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar下载地址:
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
-- 上传mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar到docker
[root@mysql ~]# pwd
/root
[root@mysql ~]# ls
anaconda-ks.cfg centos:7.6.1810 mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar -p --privileged=true
[root@mysql ~]# docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33060:/
[root@mysql ~]# docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33061:/
[root@mysql ~]# docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33062:/
[root@mysql ~]# docker network connect bridge mysql8022mgr33060
[root@mysql ~]# docker network connect bridge mysql8022mgr33061
[root@mysql ~]# docker network connect bridge mysql8022mgr33062
[root@mysql ~]# docker restart mysql8022mgr33060 mysql8022mgr33061 mysql8022mgr33062
3台主机安装MySQL环境
mysql8022mgr33060
[root@mysql ~]# docker exec -it mysql8022mgr33060 bash
[root@mgr60 /]# tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
[root@mgr60 /]# yum localinstall -y mysql-community-*.rpm
-- 取消密码认证
[root@mgr60 /]# mv /usr/lib64/mysql/plugin/validate_password.so /usr/lib64/mysql/plugin/validate_password.so_bk
[root@mgr60 /]# mv /usr/lib64/mysql/plugin/component_validate_password.so /usr/lib64/mysql/plugin/component_validate_password.so_bk
[root@mgr60 /]# systemctl start mysqld
[root@mgr60 /]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 11:43:50 CST; 975ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1140 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 1211 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/docker-575e7d8692eeb9bd702c190d4faf281696eb913611b96cc3eb14f2fd81022b74.scope/system.slice/mysqld.service
└─1211 /usr/sbin/mysqld
Oct 15 11:43:46 mgr60 systemd[1]: Starting MySQL Server...
Oct 15 11:43:50 mgr60 systemd[1]: Started MySQL Server.
[root@mgr60 /]# grep 'temporary password' /var/log/mysqld.log
2022-10-15T03:43:47.341706Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: HPe-pnyYh3us
[root@mgr60 /]# mysql -uroot -pHPe-pnyYh3us
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 8
Server version: 8.0.22
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 with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> create user root@'%' identified with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| user | host | grant_priv | super_priv | authentication_string | password_last_changed |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| root | % | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:45:00 |
| mysql.infoschema | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:43:47 |
| mysql.session | localhost | N | Y | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:43:47 |
| mysql.sys | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:43:47 |
| root | localhost | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:44:49 |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)
mysql8022mgr33061
[root@mysql ~]# docker exec -it mysql8022mgr33061 bash
[root@mgr61 /]# tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
[root@mgr61 /]# yum localinstall -y mysql-community-*.rpm
[root@mgr61 /]# mv /usr/lib64/mysql/plugin/validate_password.so /usr/lib64/mysql/plugin/validate_password.so_bk
[root@mgr61 /]# mv /usr/lib64/mysql/plugin/component_validate_password.so /usr/lib64/mysql/plugin/component_validate_password.so_bk
[root@mgr61 /]# systemctl start mysqld
[root@mgr61 /]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 11:47:14 CST; 608ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1262 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 1333 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/docker-73d80f98b3ddd54a5b9c6a4a7c91ae1bf54980e1d7f7aafca571827aff328ae5.scope/system.slice/mysqld.service
└─1333 /usr/sbin/mysqld
Oct 15 11:47:10 mgr61 systemd[1]: Starting MySQL Server...
Oct 15 11:47:14 mgr61 systemd[1]: Started MySQL Server.
[root@mgr61 /]# grep 'temporary password' /var/log/mysqld.log
2022-10-15T03:47:11.109466Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <A!;j)I4Tptt
[root@mgr61 /]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22
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 with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@'localhost' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> create user root@'%' identified with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| user | host | grant_priv | super_priv | authentication_string | password_last_changed |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| root | % | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:48:35 |
| mysql.infoschema | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:47:11 |
| mysql.session | localhost | N | Y | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:47:11 |
| mysql.sys | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:47:11 |
| root | localhost | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:48:27 |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)
mysql8022mgr33062
[root@mysql ~]# docker exec -it mysql8022mgr33062 bash
[root@mgr62 /]# tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.22-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm
mysql-community-common-8.0.22-1.el7.x86_64.rpm
mysql-community-devel-8.0.22-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-server-8.0.22-1.el7.x86_64.rpm
mysql-community-test-8.0.22-1.el7.x86_64.rpm
[root@mgr62 /]# yum localinstall -y mysql-community-*.rpm
Loaded plugins: fastestmirror, ovl
Examining mysql-community-client-8.0.22-1.el7.x86_64.rpm: mysql-community-client-8.0.22-1.el7.x86_64
Marking mysql-community-client-8.0.22-1.el7.x86_64.rpm as an update to mysql-community-client-5.7.32-1.el7.x86_64
Examining mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm: mysql-community-client-plugins-8.0.22-1.el7.x86_64
Marking mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm to be installed
Examining mysql-community-common-8.0.22-1.el7.x86_64.rpm: mysql-community-common-8.0.22-1.el7.x86_64
Marking mysql-community-common-8.0.22-1.el7.x86_64.rpm as an update to mysql-community-common-5.7.32-1.el7.x86_64
Examining mysql-community-devel-8.0.22-1.el7.x86_64.rpm: mysql-community-devel-8.0.22-1.el7.x86_64
Marking mysql-community-devel-8.0.22-1.el7.x86_64.rpm to be installed
Examining mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm: mysql-community-embedded-compat-8.0.22-1.el7.x86_64
Marking mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm to be installed
Examining mysql-community-libs-8.0.22-1.el7.x86_64.rpm: mysql-community-libs-8.0.22-1.el7.x86_64
Marking mysql-community-libs-8.0.22-1.el7.x86_64.rpm as an update to mysql-community-libs-5.7.32-1.el7.x86_64
Examining mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm: mysql-community-libs-compat-8.0.22-1.el7.x86_64
Marking mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm as an update to mysql-community-libs-compat-5.7.32-1.el7.x86_64
Examining mysql-community-server-8.0.22-1.el7.x86_64.rpm: mysql-community-server-8.0.22-1.el7.x86_64
Marking mysql-community-server-8.0.22-1.el7.x86_64.rpm to be installed
Examining mysql-community-test-8.0.22-1.el7.x86_64.rpm: mysql-community-test-8.0.22-1.el7.x86_64
Marking mysql-community-test-8.0.22-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.32-1.el7 will be updated
---> Package mysql-community-client.x86_64 0:8.0.22-1.el7 will be an update
---> Package mysql-community-client-plugins.x86_64 0:8.0.22-1.el7 will be installed
---> Package mysql-community-common.x86_64 0:5.7.32-1.el7 will be updated
---> Package mysql-community-common.x86_64 0:8.0.22-1.el7 will be an update
---> Package mysql-community-devel.x86_64 0:8.0.22-1.el7 will be installed
---> Package mysql-community-embedded-compat.x86_64 0:8.0.22-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:5.7.32-1.el7 will be updated
---> Package mysql-community-libs.x86_64 0:8.0.22-1.el7 will be an update
---> Package mysql-community-libs-compat.x86_64 0:5.7.32-1.el7 will be updated
---> Package mysql-community-libs-compat.x86_64 0:8.0.22-1.el7 will be an update
---> Package mysql-community-server.x86_64 0:8.0.22-1.el7 will be installed
---> Package mysql-community-test.x86_64 0:8.0.22-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===========================================================================================================================================
Package Arch Version Repository Size
===========================================================================================================================================
Installing:
mysql-community-client-plugins x86_64 8.0.22-1.el7 /mysql-community-client-plugins-8.0.22-1.el7.x86_64 1.0 M
mysql-community-devel x86_64 8.0.22-1.el7 /mysql-community-devel-8.0.22-1.el7.x86_64 58 M
mysql-community-embedded-compat x86_64 8.0.22-1.el7 /mysql-community-embedded-compat-8.0.22-1.el7.x86_64 90 M
mysql-community-server x86_64 8.0.22-1.el7 /mysql-community-server-8.0.22-1.el7.x86_64 2.3 G
mysql-community-test x86_64 8.0.22-1.el7 /mysql-community-test-8.0.22-1.el7.x86_64 678 M
Updating:
mysql-community-client x86_64 8.0.22-1.el7 /mysql-community-client-8.0.22-1.el7.x86_64 230 M
mysql-community-common x86_64 8.0.22-1.el7 /mysql-community-common-8.0.22-1.el7.x86_64 8.9 M
mysql-community-libs x86_64 8.0.22-1.el7 /mysql-community-libs-8.0.22-1.el7.x86_64 22 M
mysql-community-libs-compat x86_64 8.0.22-1.el7 /mysql-community-libs-compat-8.0.22-1.el7.x86_64 6.1 M
Transaction Summary
===========================================================================================================================================
Install 5 Packages
Upgrade 4 Packages
Total size: 3.4 G
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : mysql-community-common-8.0.22-1.el7.x86_64 1/13
Installing : mysql-community-client-plugins-8.0.22-1.el7.x86_64 2/13
Updating : mysql-community-libs-8.0.22-1.el7.x86_64 3/13
Updating : mysql-community-client-8.0.22-1.el7.x86_64 4/13
Installing : mysql-community-server-8.0.22-1.el7.x86_64 5/13
Installing : mysql-community-test-8.0.22-1.el7.x86_64 6/13
Installing : mysql-community-devel-8.0.22-1.el7.x86_64 7/13
Updating : mysql-community-libs-compat-8.0.22-1.el7.x86_64 8/13
Installing : mysql-community-embedded-compat-8.0.22-1.el7.x86_64 9/13
Cleanup : mysql-community-libs-compat-5.7.32-1.el7.x86_64 10/13
Cleanup : mysql-community-client-5.7.32-1.el7.x86_64 11/13
Cleanup : mysql-community-libs-5.7.32-1.el7.x86_64 12/13
Cleanup : mysql-community-common-5.7.32-1.el7.x86_64 13/13
Verifying : mysql-community-libs-8.0.22-1.el7.x86_64 1/13
Verifying : mysql-community-client-plugins-8.0.22-1.el7.x86_64 2/13
Verifying : mysql-community-common-8.0.22-1.el7.x86_64 3/13
Verifying : mysql-community-server-8.0.22-1.el7.x86_64 4/13
Verifying : mysql-community-embedded-compat-8.0.22-1.el7.x86_64 5/13
Verifying : mysql-community-devel-8.0.22-1.el7.x86_64 6/13
Verifying : mysql-community-client-8.0.22-1.el7.x86_64 7/13
Verifying : mysql-community-test-8.0.22-1.el7.x86_64 8/13
Verifying : mysql-community-libs-compat-8.0.22-1.el7.x86_64 9/13
Verifying : mysql-community-client-5.7.32-1.el7.x86_64 10/13
Verifying : mysql-community-libs-compat-5.7.32-1.el7.x86_64 11/13
Verifying : mysql-community-libs-5.7.32-1.el7.x86_64 12/13
Verifying : mysql-community-common-5.7.32-1.el7.x86_64 13/13
Installed:
mysql-community-client-plugins.x86_64 0:8.0.22-1.el7 mysql-community-devel.x86_64 0:8.0.22-1.el7
mysql-community-embedded-compat.x86_64 0:8.0.22-1.el7 mysql-community-server.x86_64 0:8.0.22-1.el7
mysql-community-test.x86_64 0:8.0.22-1.el7
Updated:
mysql-community-client.x86_64 0:8.0.22-1.el7 mysql-community-common.x86_64 0:8.0.22-1.el7
mysql-community-libs.x86_64 0:8.0.22-1.el7 mysql-community-libs-compat.x86_64 0:8.0.22-1.el7
Complete!
[root@mgr62 /]# mv /usr/lib64/mysql/plugin/validate_password.so /usr/lib64/mysql/plugin/validate_password.so_bk
[root@mgr62 /]# mv /usr/lib64/mysql/plugin/component_validate_password.so /usr/lib64/mysql/plugin/component_validate_password.so_bk
[root@mgr62 /]# systemctl start mysqld
[root@mgr62 /]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 11:49:55 CST; 1s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1345 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 1418 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/docker-3705d9583d6d8f8574a839ddb4e6a46b2055efc86d6e7f0b5b26a074483f6ff6.scope/system.slice/mysqld.service
└─1418 /usr/sbin/mysqld
Oct 15 11:49:48 mgr62 systemd[1]: Starting MySQL Server...
Oct 15 11:49:55 mgr62 systemd[1]: Started MySQL Server.
[root@mgr62 /]# grep 'temporary password' /var/log/mysqld.log
2022-10-15T03:49:49.759486Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r7pD1vOPKf!j
[root@mgr62 /]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22
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 with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.02 sec)
mysql> grant all on *.* to root@'localhost' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> create user root@'%' identified with mysql_native_password by 'wcb';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql> select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| user | host | grant_priv | super_priv | authentication_string | password_last_changed |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
| root | % | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:50:25 |
| mysql.infoschema | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:49:50 |
| mysql.session | localhost | N | Y | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:49:50 |
| mysql.sys | localhost | N | N | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | 2022-10-15 11:49:50 |
| root | localhost | Y | Y | *1095707276B0CF42657EB271586320F15FD4D6AB | 2022-10-15 11:50:17 |
+------------------+-----------+------------+------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.01 sec)
修改MySQL参数
[root@mgr60 /]# cat > /etc/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=
server-id = 802233060
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin =
binlog_format=row
binlog_checksum=NONE
skip-name-resolve
log_slave_updates = 1
gtid-mode=ON
enforce-gtid-consistency=on
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=lhrmgr60-relay-bin
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.68.60:33061"
loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62"
report_host=192.168.68.60
report_port=3306
EOF
[root@mgr60 /]# systemctl restart mysqld
[root@mgr60 /]# mysql -uroot -pwcb -e "select @@hostname,@@server_id,@@server_uuid"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+-------------+--------------------------------------+
| @@hostname | @@server_id | @@server_uuid |
+------------+-------------+--------------------------------------+
| mgr60 | 802233060 | 92e7d67b-4c3b-11ed-a1a5-0242ac110004 |
+------------+-------------+--------------------------------------+
[root@mgr61 /]# cat > /etc/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=
server-id = 802233061
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin =
binlog_format=row
binlog_checksum=NONE
log_slave_updates = 1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=lhrmgr61-relay-bin
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.68.61:33061"
loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62"
report_host=192.168.68.61
report_port=3306
EOF
[root@mgr61 /]# systemctl restart mysqld
[root@mgr61 /]# mysql -uroot -pwcb -e "select @@hostname,@@server_id,@@server_uuid"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+-------------+--------------------------------------+
| @@hostname | @@server_id | @@server_uuid |
+------------+-------------+--------------------------------------+
| mgr61 | 802233061 | 0c738122-4c3c-11ed-a575-0242ac110005 |
+------------+-------------+--------------------------------------+
[root@mgr62 /]# cat > /etc/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=
server-id = 802233062
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin =
binlog_format=row
binlog_checksum=NONE
log_slave_updates = 1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=lhrmgr62-relay-bin
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.68.62:33061"
loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62"
report_host=192.168.68.62 report_port=3306
EOF
[root@mgr62 /]# systemctl restart mysqld
[root@mgr62 /]# mysql -uroot -pwcb -e "select @@hostname,@@server_id,@@server_uuid"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+-------------+--------------------------------------+
| @@hostname | @@server_id | @@server_uuid |
+------------+-------------+--------------------------------------+
| mgr62 | 802233062 | 6adef8e0-4c3c-11ed-af3b-0242ac110006 |
+------------+-------------+--------------------------------------+
安装MGR插件
[root@mgr60 /]# mysql -uroot -pwcb
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 9
Server version: 8.0.22 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> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.06 sec)
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
Query OK, 0 rows affected (0.01 sec)
mysql> show plugins;
[root@mgr61 /]# mysql -uroot -pwcb
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 9
Server version: 8.0.22 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> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.05 sec)
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
Query OK, 0 rows affected (0.02 sec)
mysql> show plugins;
[root@mgr62 /]# mysql -uroot -pwcb
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 9
Server version: 8.0.22 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> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.07 sec)
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
Query OK, 0 rows affected (0.02 sec)
mysql> show plugins;
设置复制账号
[root@mgr60 /]# mysql -uroot -pwcb
mysql> CREATE USER repl@'%' IDENTIFIED BY 'wcb';
Query OK, 0 rows affected (0.06 sec)
mysql> GRANT REPLICATION SLAVE,BACKUP_ADMIN ON *.* TO repl@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='wcb' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
[root@mgr61 /]# mysql -uroot -pwcb
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='wcb' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
[root@mgr62 /]# mysql -uroot -pwcb
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='wcb' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
启动MGR(主节点)
[root@mgr60 /]# mysql -uroot -pwcb
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, 1 warning (2.20 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 | 92e7d67b-4c3b-11ed-a1a5-0242ac110004 | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 |
+---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+
1 row in set (0.00 sec)
其他节点加入MGR(其他节点)
[root@mgr61 /]# mysql -uroot -pwcb
mysql> reset master;
Query OK, 0 rows affected (0.04 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (3.51 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 | 0c738122-4c3c-11ed-a575-0242ac110005 | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 92e7d67b-4c3b-11ed-a1a5-0242ac110004 | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 |
+---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+
2 rows in set (0.01 sec)
[root@mgr62 /]# mysql -uroot -pwcb
mysql> reset master;
Query OK, 0 rows affected (0.06 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (2.87 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 | 0c738122-4c3c-11ed-a575-0242ac110005 | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 6adef8e0-4c3c-11ed-af3b-0242ac110006 | 192.168.68.62 report_port=3306 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 92e7d67b-4c3b-11ed-a1a5-0242ac110004 | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.03 sec)
测试同步
[root@mgr60 /]# mysql -uroot -pwcb
mysql> create database db1;
Query OK, 1 row affected (0.09 sec)
mysql> CREATE TABLE db1.`tb1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `hostname` varchar(100) DEFAULT NULL, `server_id` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> insert into db1.tb1(hostname,server_id) select @@hostname,@@server_id;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from db1.tb1;
+----+----------+-----------+
| id | hostname | server_id |
+----+----------+-----------+
| 1 | mgr60 | 802233060 |
+----+----------+-----------+
1 row in set (0.01 sec)
[root@mgr61 /]# mysql -uroot -pwcb
mysql> select * from db1.tb1;
+----+----------+-----------+
| id | hostname | server_id |
+----+----------+-----------+
| 1 | mgr60 | 802233060 |
+----+----------+-----------+
1 row in set (0.02 sec)
[root@mgr62 /]# mysql -uroot -pwcb
mysql> select * from db1.tb1;
+----+----------+-----------+
| id | hostname | server_id |
+----+----------+-----------+
| 1 | mgr60 | 802233060 |
+----+----------+-----------+
1 row in set (0.02 sec)
这里可以看到MGR已经搭建完成,主库的数据可以同步到从库。
其他MGR相关操作
删除节点
mysql> stop GROUP_REPLICATION;
Query OK, 0 rows affected (4.60 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 | 8818cc27-4d2a-11ed-ae32-0242ac110004 | 192.168.68.62 report_port=3306 | 3306 | OFFLINE | | |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
1 row in set (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 | 2d2ef770-4d2a-11ed-af9d-0242ac110002 | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 |
| group_replication_applier | 6367f2b8-4d2a-11ed-bed1-0242ac110003 | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 8818cc27-4d2a-11ed-ae32-0242ac110004 | 192.168.68.62 report_port=3306 | 3306 | ONLINE | SECONDARY | 8.0.22 |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.03 sec)
mysql> select group_replication_set_as_primary('6367f2b8-4d2a-11ed-bed1-0242ac110003');
+--------------------------------------------------------------------------+
| group_replication_set_as_primary('6367f2b8-4d2a-11ed-bed1-0242ac110003') |
+--------------------------------------------------------------------------+
| Primary server switched to: 6367f2b8-4d2a-11ed-bed1-0242ac110003 |
+--------------------------------------------------------------------------+
1 row in set (1.06 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 | 2d2ef770-4d2a-11ed-af9d-0242ac110002 | 192.168.68.60 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 6367f2b8-4d2a-11ed-bed1-0242ac110003 | 192.168.68.61 | 3306 | ONLINE | PRIMARY | 8.0.22 |
| group_replication_applier | 8818cc27-4d2a-11ed-ae32-0242ac110004 | 192.168.68.62 report_port=3306 | 3306 | ONLINE | SECONDARY | 8.0.22 |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
3 rows in set (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 | 2d2ef770-4d2a-11ed-af9d-0242ac110002 | 192.168.68.60 | 3306 | ONLINE | SECONDARY | 8.0.22 |
| group_replication_applier | 6367f2b8-4d2a-11ed-bed1-0242ac110003 | 192.168.68.61 | 3306 | UNREACHABLE | PRIMARY | 8.0.22 |
| group_replication_applier | 8818cc27-4d2a-11ed-ae32-0242ac110004 | 192.168.68.62 report_port=3306 | 3306 | ONLINE | SECONDARY | 8.0.22 |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
停止主库
[root@mysql ~]# docker stop mysql8022mgr33061
mysql8022mgr33061
等待一会后发现已经进行切换
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 | 2d2ef770-4d2a-11ed-af9d-0242ac110002 | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 |
| group_replication_applier | 8818cc27-4d2a-11ed-ae32-0242ac110004 | 192.168.68.62 report_port=3306 | 3306 | ONLINE | SECONDARY | 8.0.22 |
+---------------------------+--------------------------------------+--------------------------------+-------------+--------------+-------------+----------------+
2 rows in set (0.00 sec)