MySQL InnoDB Cluster 高可用集群部署

news2024/11/16 19:54:34

MySQL InnoDB Cluster 简介

官方文档:https://dev.mysql.com/doc/refman/8.4/en/mysql-innodb-cluster-introduction.html

本章介绍 MySQL InnoDB Cluster,它结合了 MySQL 技术,使您能够部署和管理完整的 MySQL 集成高可用性解决方案。

说明:InnoDB Cluster 不提供对 MySQL NDB Cluster 的支持。

InnoDB Cluster至少由三个MySQL Server实例组成,它提供高可用性和扩展功能。 InnoDB Cluster 使用以下 MySQL 技术:

  • MySQL Shell,它是 MySQL 的高级客户端和代码编辑器。
  • MySQL server 和 Group Replication,,使一组MySQL实例能够提供高可用性。 InnoDB Cluster 提供了一种替代的、易于使用的编程方式来使用组复制。
  • MySQL Router,一种轻量级中间件,可在应用程序和 InnoDB Cluster 之间提供透明路由。

下图显示了这些技术如何协同工作的概述:
在这里插入图片描述

基于MySQL Group Replication 构建,提供自动成员管理、容错、自动故障转移等功能。 InnoDB Cluster通常以单主模式运行,具有一个主实例(读写)和多个辅助实例(只读)。高级用户还可以利用多主模式,其中所有实例都是主实例。您甚至可以在 InnoDB Cluster 在线时更改集群的拓扑,以确保尽可能高的可用性。

集群部署节点规划

必须部署至少三台MySQL服务器,用于mysql innodb节点,至少一台MySQL Router服务器。

本示例使用7台虚拟机进行部署。部署架构如下图所示:

在这里插入图片描述

节点规划清单如下:

HostIP操作系统角色
mysql-innodb01192.168.72.50Ubuntu 22.04mysql innodb/mysql shell
mysql-innodb02192.168.72.51Ubuntu 22.04mysql innodb/mysql shell
mysql-innodb03192.168.72.52Ubuntu 22.04mysql innodb/mysql shell
mysql-router01192.168.72.41Ubuntu 22.04mysql router/mysql client
mysql-router02192.168.72.42Ubuntu 22.04mysql router/mysql client
lb01192.168.72.33Ubuntu 22.04haproxy/keepalived
lb02192.168.72.34Ubuntu 22.04haproxy/keepalived
mysql-db-vip192.168.72.200N/AN/A

以下操作在所有mysql-router节点及mysql-innodb节点执行。

分别在每个节点配置主机名

hostnamectl set-hostname mysql-innodb01
hostnamectl set-hostname mysql-innodb02
hostnamectl set-hostname mysql-innodb03
hostnamectl set-hostname mysql-router01
hostnamectl set-hostname mysql-router02

分别在每个节点配置hosts解析

cat << EOF > /etc/hosts
192.168.72.41 mysql-router01 mysql-router02
192.168.72.42 mysql-router02 mysql-router02
192.168.72.50 mysql-innodb01 mysql-innodb01
192.168.72.51 mysql-innodb02 mysql-innodb02
192.168.72.52 mysql-innodb03 mysql-innodb03
EOF

添加mysql官方软件源

下载地址:https://dev.mysql.com/downloads/repo/apt/

wget https://repo.mysql.com//mysql-apt-config_0.8.30-1_all.deb
dpkg -i mysql-apt-config_0.8.30-1_all.deb
apt update -y

安装innodb集群

以下操作在所有innodb节点执行。

在安装过程中,您需要为MySQL root用户设置密码。

apt update -y
apt install -y mysql-server mysql-shell

查看安装版本

root@mysql-innodb01:~# mysql -V
mysql  Ver 8.0.37 for Linux on x86_64 (MySQL Community Server - GPL)

root@mysql-innodb01:~# mysqlsh -V
mysqlsh   Ver 8.0.37 for Linux on x86_64 - for MySQL 8.0.37 (MySQL Community Server (GPL))

在innodb01节点执行

连接到mysql-shell

root@mysql-innodb01:~# mysqlsh

连接到数据库,输入数据库root用户密码

 MySQL  SQL > \connect root@localhost
Creating a session to 'root@localhost'
Please provide the password for 'root@localhost': ************
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching global names for auto-completion... Press ^C to stop.
Your MySQL connection id is 13 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:33060+ ssl  SQL > 

或者直接连接

mysqlsh -uroot -pMysql@123456 -h localhost

查看数据库信息

 MySQL  localhost:33060+ ssl  SQL > \sql

 MySQL  localhost:33060+ ssl  SQL > show schemas;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.0011 sec)

或者切换到JS模式

 MySQL  localhost:33060+ ssl  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost:33060+ ssl  JS > 
MySQL  localhost:33060+ ssl  JS > \sql show schemas;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.0011 sec)
 MySQL  localhost:33060+ ssl  JS > 

为innodb集群创建独立账号icadmin,密码为icadminPassw0rd!

MySQL  localhost:33060+ ssl  JS > dba.configureInstance()
Configuring local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as innodb01:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

ERROR: User 'root' can only connect from 'localhost'. New account(s) with proper source address specification to allow remote connection from all instances must be created to manage the cluster.

1) Create remotely usable account for 'root' with same grants and password
2) Create a new admin account for InnoDB Cluster with minimal required grants
3) Ignore and continue
4) Cancel

Please select an option [1]: 2
Please provide an account name (e.g: icroot@%) to have it created with the necessary
privileges or leave empty and press Enter to cancel.
Account Name: icadmin
Password for new account: icadmin
Confirm password: ***********

applierWorkerThreads will be set to the default value of 4.

NOTE: Some configuration options need to be fixed:
+--------------------------+---------------+----------------+--------------------------------------------------+
| Variable                 | Current Value | Required Value | Note                                             |
+--------------------------+---------------+----------------+--------------------------------------------------+
| enforce_gtid_consistency | OFF           | ON             | Update read-only variable and restart the server |
| gtid_mode                | OFF           | ON             | Update read-only variable and restart the server |
| server_id                | 1             | <unique ID>    | Update read-only variable and restart the server |
+--------------------------+---------------+----------------+--------------------------------------------------+

Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y

Creating user icadmin
@%.
Account icadmin@% was successfully created.

Configuring instance...
The instance 'mysql-innodb01:3306' was configured to be used in an InnoDB Cluster.
Restarting MySQL...
NOTE: MySQL server at mysql-innodb01:3306 was restarted.
 MySQL  localhost:33060+ ssl  JS > 

或者使用以下命令

 MySQL  localhost:33060+ ssl  JS > dba.configureInstance('root@localhost:3306', {clusterAdmin: "'icadmin'@'%'", clusterAdminPassword: 'icadminPassw0rd!'});

查看数据库信息,查看本节点数据库实例Uptime参数,确认已重启

 MySQL  localhost:33060+ ssl  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost:33060+ ssl  SQL > \s
MySQL Shell version 8.4.0

Error Retrieving Status:      MySQL Error 2006: MySQL server has gone away
The global session got disconnected..
Attempting to reconnect to 'mysqlx://root@localhost:33060'..
The global session was successfully reconnected.
 MySQL  localhost:33060+ ssl  SQL > 
 MySQL  localhost:33060+ ssl  SQL > \s
MySQL Shell version 8.4.0

Connection Id:                9
Default schema:               
Current schema:               
Current user:                 root@localhost
SSL:                          Cipher in use: TLS_AES_128_GCM_SHA256 TLSv1.3
Using delimiter:              ;
Server version:               8.4.0 MySQL Community Server - GPL
Protocol version:             X protocol
Client library:               8.4.0
Connection:                   localhost via TCP/IP
TCP port:                     33060
Server characterset:          utf8mb4
Schema characterset:          utf8mb4
Client characterset:          utf8mb4
Conn. characterset:           utf8mb4
Result characterset:          utf8mb4
Compression:                  Enabled (DEFLATE_STREAM)
Uptime:                       2 min 55.0000 sec

查看gtid_mode 已启用

 MySQL  localhost:33060+ ssl  SQL > \js
Switching to JavaScript mode...


 MySQL  localhost:33060+ ssl  JS > \sql select  @@gtid_mode, @@server_id
+-------------+-------------+
| @@gtid_mode | @@server_id |
+-------------+-------------+
| ON          |  2728360213 |
+-------------+-------------+
1 row in set (0.0007 sec)

查看生成的mysqld-auto.cnf配置文件

root@innodb01:~# cat /var/lib/mysql/mysqld-auto.cnf 

其中确认参数如下

gtid_mode": {"Value": "ON"
server_id": {"Value": "2728360213

重新连接到集群账号

root@mysql-innodb01:~# fg
 MySQL  localhost:33060+ ssl  JS > \connect icadmin@localhost
Creating a session to 'icadmin@localhost'
Please provide the password for 'icadmin@localhost': **********
Save password for 'icadmin@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching schema names for auto-completion... Press ^C to stop.
Closing old connection...
Your MySQL connection id is 12 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.

检查实例配置,确认status参数为ok。

 MySQL  localhost:33060+ ssl  JS > dba.checkInstanceConfiguration()
Validating local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as innodb01:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'mysql-innodb01:3306' is valid for InnoDB Cluster usage.

{
    "status": "ok"
}
 MySQL  localhost:33060+ ssl  JS > 

连接到innodb02

连接到mysql-shell

root@mysql-innodb02:~# mysqlsh
MySQL Shell 8.4.0

Copyright (c) 2016, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.

 MySQL  JS > 

连接到数据库

 MySQL  SQL > \connect root@localhost
Creating a session to 'root@localhost'
Please provide the password for 'root@localhost': ************
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching global names for auto-completion... Press ^C to stop.
Your MySQL connection id is 9 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:33060+ ssl  SQL > 

为innodb集群创建独立账号icadmin,密码为icadminPassw0rd!

 MySQL  localhost:33060+ ssl  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost:33060+ ssl  JS > dba.configureInstance()
Configuring local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as innodb02:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

ERROR: User 'root' can only connect from 'localhost'. New account(s) with proper source address specification to allow remote connection from all instances must be created to manage the cluster.

1) Create remotely usable account for 'root' with same grants and password
2) Create a new admin account for InnoDB Cluster with minimal required grants
3) Ignore and continue
4) Cancel

Please select an option [1]: 2
Please provide an account name (e.g: icroot@%) to have it created with the necessary
privileges or leave empty and press Enter to cancel.
Account Name: icadmin
Password for new account: ************
Confirm password: ************

applierWorkerThreads will be set to the default value of 4.

NOTE: Some configuration options need to be fixed:
+--------------------------+---------------+----------------+--------------------------------------------------+
| Variable                 | Current Value | Required Value | Note                                             |
+--------------------------+---------------+----------------+--------------------------------------------------+
| enforce_gtid_consistency | OFF           | ON             | Update read-only variable and restart the server |
| gtid_mode                | OFF           | ON             | Update read-only variable and restart the server |
| server_id                | 1             | <unique ID>    | Update read-only variable and restart the server |
+--------------------------+---------------+----------------+--------------------------------------------------+

Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y

Creating user icadmin@%.
Account icadmin@% was successfully created.

Configuring instance...
The instance 'mysql-innodb02:3306' was configured to be used in an InnoDB Cluster.
Restarting MySQL...
NOTE: MySQL server at mysql-innodb02:3306 was restarted.
 MySQL  localhost:33060+ ssl  JS > 

连接到集群账号

 MySQL  localhost:33060+ ssl  JS > \connect icadmin@localhost
Creating a session to 'icadmin@localhost'
Please provide the password for 'icadmin@localhost': ************
Save password for 'ic@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching schema names for auto-completion... Press ^C to stop.
Closing old connection...
Your MySQL connection id is 9 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.

检查实例

 MySQL  localhost:33060+ ssl  JS > dba.checkInstanceConfiguration()
Validating local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as mysql-innodb02:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'mysql-innodb02:3306' is valid for InnoDB Cluster usage.

{
    "status": "ok"
}
 MySQL  localhost:33060+ ssl  JS > 

连接到innodb03

root@innodb03:~# mysqlsh
MySQL Shell 8.4.0

Copyright (c) 2016, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.

连接到数据库账号

 MySQL  SQL > \connect root@localhost
Creating a session to 'root@localhost'
Please provide the password for 'root@localhost': ************
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching global names for auto-completion... Press ^C to stop.
Your MySQL connection id is 9 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:33060+ ssl  SQL > 

为innodb集群创建独立账号icadmin,密码为icadminPassw0rd!

 MySQL  localhost:33060+ ssl  SQL > \js
Switching to JavaScript mode...
 MySQL  localhost:33060+ ssl  JS > dba.configureInstance()
Configuring local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as mysql-innodb03:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

ERROR: User 'root' can only connect from 'localhost'. New account(s) with proper source address specification to allow remote connection from all instances must be created to manage the cluster.

1) Create remotely usable account for 'root' with same grants and password
2) Create a new admin account for InnoDB Cluster with minimal required grants
3) Ignore and continue
4) Cancel

Please select an option [1]: 2
Please provide an account name (e.g: icroot@%) to have it created with the necessary
privileges or leave empty and press Enter to cancel.
Account Name: ic
Password for new account: **********
Confirm password: *********

applierWorkerThreads will be set to the default value of 4.

NOTE: Some configuration options need to be fixed:
+--------------------------+---------------+----------------+--------------------------------------------------+
| Variable                 | Current Value | Required Value | Note                                             |
+--------------------------+---------------+----------------+--------------------------------------------------+
| enforce_gtid_consistency | OFF           | ON             | Update read-only variable and restart the server |
| gtid_mode                | OFF           | ON             | Update read-only variable and restart the server |
| server_id                | 1             | <unique ID>    | Update read-only variable and restart the server |
+--------------------------+---------------+----------------+--------------------------------------------------+

Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y

Creating user icadmin@%.
Account icadmin@% was successfully created.

Configuring instance...
The instance 'mysql-innodb03:3306' was configured to be used in an InnoDB Cluster.
Restarting MySQL...
NOTE: MySQL server at mysql-innodb03:3306 was restarted.
 MySQL  localhost:33060+ ssl  JS > 

连接到集群账号

 MySQL  localhost:33060+ ssl  JS > \connect icadmin@localhost
Creating a session to 'icadmin@localhost'
Please provide the password for 'icadmin@localhost': **
Save password for 'icadmin@localhost'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching schema names for auto-completion... Press ^C to stop.
Closing old connection...
Your MySQL connection id is 9 (X protocol)
Server version: 8.4.0 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.

检查实例

 MySQL  localhost:33060+ ssl  JS > dba.checkInstanceConfiguration()
Validating local MySQL instance listening at port 3306 for use in an InnoDB Cluster...

This instance reports its own address as mysql-innodb03:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'mysql-innodb03:3306' is valid for InnoDB Cluster usage.

{
    "status": "ok"
}
 MySQL  localhost:33060+ ssl  JS > 

创建innodb集群

以下所有操作在innodb01节点执行

 MySQL  localhost:33060+ ssl  JS > dba.createCluster('my_innodb_cluster')
A new InnoDB Cluster will be created on instance 'mysql-innodb01:3306'.

Validating instance configuration at localhost:3306...

This instance reports its own address as innodb01:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'mysql-innodb01:3306'. Use the localAddress option to override.

* Checking connectivity and SSL configuration...

Creating InnoDB Cluster 'my_innodb_cluster' on 'innodb01:3306'...

Adding Seed Instance...
Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.

<Cluster:my_innodb_cluster>
 MySQL  localhost:33060+ ssl  JS > 

查看集群状态

 MySQL  localhost:33060+ ssl  JS > dba.getCluster()
<Cluster:my_innodb_cluster>
 MySQL  localhost:33060+ ssl  JS > herc = dba.getCluster()
<Cluster:my_innodb_cluster>

 MySQL  localhost:33060+ ssl  JS > herc.status()
{
    "clusterName": "my_innodb_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "mysql-innodb01:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures.", 
        "topology": {
            "innodb01:3306": {
                "address": "mysql-innodb01:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "mysql-innodb01:3306"
}
 MySQL  localhost:33060+ ssl  JS > 

添加innodb02节点到集群

 MySQL  localhost:33060+ ssl  JS > herc.addInstance('icadmin@mysql-innodb02')

NOTE: The target instance 'mysql-innodb02:3306' has not been pre-provisioned (GTID set is empty). The Shell is unable to decide whether incremental state recovery can correctly provision it.
The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of 'innodb02:3306' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'.

The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'.


Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone): C
Validating instance configuration at innodb02:3306...

This instance reports its own address as innodb02:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'mysql-innodb02:3306'. Use the localAddress option to override.

* Checking connectivity and SSL configuration...

A new instance will be added to the InnoDB Cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster...

Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: innodb02:3306 is being cloned from innodb01:3306
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: innodb02:3306 is shutting down...

* Waiting for server restart... ready 
* innodb02:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 74.14 MB transferred in about 1 second (~74.14 MB/s)

State recovery already finished for 'mysql-innodb02:3306'

The instance 'mysql-innodb02:3306' was successfully added to the cluster.

检查集群状态

 MySQL  localhost:33060+ ssl  JS > herc.status()
{
    "clusterName": "my_innodb_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "mysql-innodb01:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures.", 
        "topology": {
            "innodb01:3306": {
                "address": "mysql-innodb01:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }, 
            "innodb02:3306": {
                "address": "mysql-innodb02:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "mysql-innodb01:3306"
}
 MySQL  localhost:33060+ ssl  JS > 

在innnodb01节点检查super_read_only参数,只读节点数为0

MySQL  localhost:33060+ ssl  JS > \sql select @@super_read_only;
+-------------------+
| @@super_read_only |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.0007 sec)

在innnodb02节点检查super_read_only参数,只读节点数为1

 MySQL  localhost:33060+ ssl  JS > \sql select @@super_read_only;
+-------------------+
| @@super_read_only |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.0004 sec)

添加innodb03节点到集群

 MySQL  localhost:33060+ ssl  JS > herc.addInstance('icadmin@mysql-innodb03')

NOTE: The target instance 'mysql-innodb03:3306' has not been pre-provisioned (GTID set is empty). The Shell is unable to decide whether incremental state recovery can correctly provision it.
The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of 'mysql-innodb03:3306' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'.

The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'.



Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone): C
Validating instance configuration at mysql-innodb03:3306...

This instance reports its own address as mysql-innodb03:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'mysql-innodb03:3306'. Use the localAddress option to override.

* Checking connectivity and SSL configuration...

A new instance will be added to the InnoDB Cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster...

Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: mysql-innodb03:3306 is being cloned from mysql-innodb01:3306
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: innodb03:3306 is shutting down...

* Waiting for server restart... ready 
* innodb03:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 74.41 MB transferred in about 1 second (~74.41 MB/s)

State recovery already finished for 'mysql-innodb03:3306'

The instance 'mysql-innodb03:3306' was successfully added to the cluster.

查看集群状态

 MySQL  localhost:33060+ ssl  JS > herc.status()
{
    "clusterName": "my_innodb_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "mysql-innodb01:3306", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "mysql-innodb01:3306": {
                "address": "mysql-innodb01:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }, 
            "mysql-innodb02:3306": {
                "address": "mysql-innodb02:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }, 
            "mysql-innodb03:3306": {
                "address": "mysql-innodb03:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "innodb01:3306"
}
 MySQL  localhost:33060+ ssl  JS > 

关键字段如下:

"status": "OK" 表示集群状态是正常的
"topologyMode": "Single-Primary" 表示是单主模式
"mode": "R/W" 表示可读可写
"mode": "R/O" 表示只读

查看库及表

 MySQL  localhost:33060+ ssl  JS > \sql
Switching to SQL mode... Commands end with ;
 MySQL  localhost:33060+ ssl  SQL > show schemas;
+-------------------------------+
| Database                      |
+-------------------------------+
| information_schema            |
| mysql                         |
| mysql_innodb_cluster_metadata |
| performance_schema            |
| sys                           |
+-------------------------------+
5 rows in set (0.0013 sec)
 MySQL  localhost:33060+ ssl  SQL > use mysql_innodb_cluster_metadata;
Default schema set to `mysql_innodb_cluster_metadata`.
Fetching global names, object names from `mysql_innodb_cluster_metadata` for auto-completion... Press ^C to stop.
 MySQL  localhost:33060+ ssl  mysql_innodb_cluster_metadata  SQL > 
 MySQL  localhost:33060+ ssl  mysql_innodb_cluster_metadata  SQL > show tables;
+-----------------------------------------+
| Tables_in_mysql_innodb_cluster_metadata |
+-----------------------------------------+
| async_cluster_members                   |
| async_cluster_views                     |
| clusters                                |
| clusterset_members                      |
| clusterset_views                        |
| clustersets                             |
| instances                               |
| router_rest_accounts                    |
| routers                                 |
| schema_version                          |
| v2_ar_clusters                          |
| v2_ar_members                           |
| v2_clusters                             |
| v2_cs_clustersets                       |
| v2_cs_members                           |
| v2_cs_router_options                    |
| v2_gr_clusters                          |
| v2_instances                            |
| v2_router_options                       |
| v2_router_rest_accounts                 |
| v2_routers                              |
| v2_this_instance                        |
+-----------------------------------------+
22 rows in set (0.0020 sec)
 MySQL  localhost:33060+ ssl  mysql_innodb_cluster_metadata  SQL > use performance_schema;
Default schema set to `performance_schema`.
Fetching global names, object names from `performance_schema` for auto-completion... Press ^C to stop.
 MySQL  localhost:33060+ ssl  performance_schema  SQL > show tables;
+------------------------------------------------------+
| Tables_in_performance_schema                         |
+------------------------------------------------------+
| accounts                                             |
| binary_log_transaction_compression_stats             |
| clone_progress                                       |
| clone_status                                         |
| cond_instances                                       |
| data_lock_waits                                      |
| data_locks                                           |
| error_log                                            |
| events_errors_summary_by_account_by_error            |
| events_errors_summary_by_host_by_error               |
| events_errors_summary_by_thread_by_error             |
| events_errors_summary_by_user_by_error               |
| events_errors_summary_global_by_error                |
| events_stages_current                                |
| events_stages_history                                |
| events_stages_history_long                           |
| events_stages_summary_by_account_by_event_name       |
| events_stages_summary_by_host_by_event_name          |
| events_stages_summary_by_thread_by_event_name        |
| events_stages_summary_by_user_by_event_name          |
| events_stages_summary_global_by_event_name           |
| events_statements_current                            |
| events_statements_histogram_by_digest                |
| events_statements_histogram_global                   |
| events_statements_history                            |
| events_statements_history_long                       |
| events_statements_summary_by_account_by_event_name   |
| events_statements_summary_by_digest                  |
| events_statements_summary_by_host_by_event_name      |
| events_statements_summary_by_program                 |
| events_statements_summary_by_thread_by_event_name    |
| events_statements_summary_by_user_by_event_name      |
| events_statements_summary_global_by_event_name       |
| events_transactions_current                          |
| events_transactions_history                          |
| events_transactions_history_long                     |
| events_transactions_summary_by_account_by_event_name |
| events_transactions_summary_by_host_by_event_name    |
| events_transactions_summary_by_thread_by_event_name  |
| events_transactions_summary_by_user_by_event_name    |
| events_transactions_summary_global_by_event_name     |
| events_waits_current                                 |
| events_waits_history                                 |
| events_waits_history_long                            |
| events_waits_summary_by_account_by_event_name        |
| events_waits_summary_by_host_by_event_name           |
| events_waits_summary_by_instance                     |
| events_waits_summary_by_thread_by_event_name         |
| events_waits_summary_by_user_by_event_name           |
| events_waits_summary_global_by_event_name            |
| file_instances                                       |
| file_summary_by_event_name                           |
| file_summary_by_instance                             |
| global_status                                        |
| global_variables                                     |
| host_cache                                           |
| hosts                                                |
| innodb_redo_log_files                                |
| keyring_component_status                             |
| keyring_keys                                         |
| log_status                                           |
| memory_summary_by_account_by_event_name              |
| memory_summary_by_host_by_event_name                 |
| memory_summary_by_thread_by_event_name               |
| memory_summary_by_user_by_event_name                 |
| memory_summary_global_by_event_name                  |
| metadata_locks                                       |
| mutex_instances                                      |
| objects_summary_global_by_type                       |
| performance_timers                                   |
| persisted_variables                                  |
| prepared_statements_instances                        |
| processlist                                          |
| replication_applier_configuration                    |
| replication_applier_filters                          |
| replication_applier_global_filters                   |
| replication_applier_status                           |
| replication_applier_status_by_coordinator            |
| replication_applier_status_by_worker                 |
| replication_asynchronous_connection_failover         |
| replication_asynchronous_connection_failover_managed |
| replication_connection_configuration                 |
| replication_connection_status                        |
| replication_group_communication_information          |
| replication_group_configuration_version              |
| replication_group_member_actions                     |
| replication_group_member_stats                       |
| replication_group_members                            |
| rwlock_instances                                     |
| session_account_connect_attrs                        |
| session_connect_attrs                                |
| session_status                                       |
| session_variables                                    |
| setup_actors                                         |
| setup_consumers                                      |
| setup_instruments                                    |
| setup_meters                                         |
| setup_metrics                                        |
| setup_objects                                        |
| setup_threads                                        |
| socket_instances                                     |
| socket_summary_by_event_name                         |
| socket_summary_by_instance                           |
| status_by_account                                    |
| status_by_host                                       |
| status_by_thread                                     |
| status_by_user                                       |
| table_handles                                        |
| table_io_waits_summary_by_index_usage                |
| table_io_waits_summary_by_table                      |
| table_lock_waits_summary_by_table                    |
| threads                                              |
| tls_channel_status                                   |
| user_defined_functions                               |
| user_variables_by_thread                             |
| users                                                |
| variables_by_thread                                  |
| variables_info                                       |
+------------------------------------------------------+
118 rows in set (0.0031 sec)
 MySQL  localhost:33060+ ssl  performance_schema  SQL > 

查看replication_group_members

MySQL  localhost:33060+ ssl  performance_schema  SQL > select * from replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST       | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 7149de5f-34f4-11ef-91fc-005056aaaba0 | mysql-innodb03    |        3306 | ONLINE       | SECONDARY   | 8.4.0          | MySQL                      |
| group_replication_applier | 7970842a-34f4-11ef-9af0-005056aafb5f | mysql-innodb02    |        3306 | ONLINE       | SECONDARY   | 8.4.0          | MySQL                      |
| group_replication_applier | 8091d83c-34f4-11ef-aac6-005056aa2dba | mysql-innodb01    |        3306 | ONLINE       | PRIMARY     | 8.4.0          | MySQL                      |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.0010 sec)
 MySQL  localhost:33060+ ssl  performance_schema  SQL > select CHANNEL_NAME,MEMBER_ID,MEMBER_HOST,MEMBER_STATE,MEMBER_ROLE from replication_group_members;
+---------------------------+--------------------------------------+-------------+--------------+-------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST       | MEMBER_STATE | MEMBER_ROLE |
+---------------------------+--------------------------------------+-------------+--------------+-------------+
| group_replication_applier | 7149de5f-34f4-11ef-91fc-005056aaaba0 | mysql-innodb03    | ONLINE       | SECONDARY   |
| group_replication_applier | 7970842a-34f4-11ef-9af0-005056aafb5f | mysql-innodb02    | ONLINE       | SECONDARY   |
| group_replication_applier | 8091d83c-34f4-11ef-aac6-005056aa2dba | mysql-innodb01    | ONLINE       | PRIMARY     |
+---------------------------+--------------------------------------+-------------+--------------+-------------+
3 rows in set (0.0017 sec)
 MySQL  localhost:33060+ ssl  performance_schema  SQL > 

至此,3节点mysql innodb集群部署已经完成。

部署mysql-router

安装mysql-router

以下操作在所有mysql-router节点执行

apt install -y mysql-router mysql-client

查看安装版本

root@mysql-router01:~# mysqlrouter -V
MySQL Router  Ver 8.0.37 for Linux on x86_64 (MySQL Community - GPL)

root@mysql-router01:~# mysql -V
mysql  Ver 8.0.37 for Linux on x86_64 (MySQL Community Server - GPL)
root@mysql-router01:~# 

修改apparmor配置,末尾添加如下内容

root@mysql-router01:~# nano /etc/apparmor.d/usr.bin.mysqlrouter
......
# Allow directory for MySQL InnoDB cluster
  /var/lib/mysqlrouter/ rw,
  /var/lib/mysqlrouter/** rw, 
}

重新加载apparmor服务

systemctl reload apparmor.service 

连接innodb集群

mysqlrouter --bootstrap icadmin@mysql-innodb01:3306 --directory /var/lib/mysqlrouter \
--conf-bind-address 0.0.0.0 --user=mysqlrouter

命令输出示例

root@mysql-router01:~# mysqlrouter --bootstrap ic@innodb01:3306 --directory /var/lib/mysqlrouter --conf-bind-address 0.0.0.0 --user=mysqlrouter --force
Please enter MySQL password for ic: 
# Bootstrapping MySQL Router 8.4.0 (MySQL Community - GPL) instance at '/var/lib/mysqlrouter'...

- Creating account(s) (only those that are needed, if any)
- Verifying account (using it to run SQL queries that would be run by Router)
- Storing account in keyring
- Adjusting permissions of generated files
- Creating configuration /var/lib/mysqlrouter/mysqlrouter.conf

# MySQL Router configured for the InnoDB Cluster 'my_innodb_cluster'

After this MySQL Router has been started with the generated configuration

    $ mysqlrouter -c /var/lib/mysqlrouter/mysqlrouter.conf

InnoDB Cluster 'my_innodb_cluster' can be reached by connecting to:

## MySQL Classic protocol

- Read/Write Connections: localhost:6446
- Read/Only Connections:  localhost:6447
- Read/Write Split Connections: localhost:6450

## MySQL X protocol

- Read/Write Connections: localhost:6448
- Read/Only Connections:  localhost:6449

查看监听端口

root@router01:~/data# ss -antulp
Netid            State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process                                               
udp              UNCONN            0                 0                            127.0.0.53%lo:53                              0.0.0.0:*               users:(("systemd-resolve",pid=823,fd=13))            
udp              UNCONN            0                 0                                127.0.0.1:323                             0.0.0.0:*               users:(("chronyd",pid=865,fd=5))                     
udp              UNCONN            0                 0                                    [::1]:323                                [::]:*               users:(("chronyd",pid=865,fd=6))                     
tcp              LISTEN            0                 4096                         127.0.0.53%lo:53                              0.0.0.0:*               users:(("systemd-resolve",pid=823,fd=14))            
tcp              LISTEN            0                 128                                0.0.0.0:22                              0.0.0.0:*               users:(("sshd",pid=900,fd=3))                        
tcp              LISTEN            0                 128                                   [::]:22                                 [::]:*               users:(("sshd",pid=900,fd=4))  

手动临时启动服务

root@mysql-router01:~# cd /var/lib/mysqlrouter/
root@mysql-router01:/var/lib/mysqlrouter# ./start.sh
root@mysql-router01:/var/lib/mysqlrouter# PID 4310 written to '/var/lib/mysqlrouter/mysqlrouter.pid'
stopping to log to the console. Continuing to log to filelog
^C

重新查看监听端口,mysql router读写端口为6446,mysql router只读端口为6447

root@mysql-router01:/var/lib/mysqlrouter# ss -tulnp
Netid            State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process                                               
udp              UNCONN            0                 0                            127.0.0.53%lo:53                              0.0.0.0:*               users:(("systemd-resolve",pid=823,fd=13))            
udp              UNCONN            0                 0                                127.0.0.1:323                             0.0.0.0:*               users:(("chronyd",pid=865,fd=5))                     
udp              UNCONN            0                 0                                    [::1]:323                                [::]:*               users:(("chronyd",pid=865,fd=6))                     
tcp              LISTEN            0                 1024                               0.0.0.0:6448                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=12))               
tcp              LISTEN            0                 1024                               0.0.0.0:6449                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=14))               
tcp              LISTEN            0                 1024                               0.0.0.0:6450                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=16))               
tcp              LISTEN            0                 4096                         127.0.0.53%lo:53                              0.0.0.0:*               users:(("systemd-resolve",pid=823,fd=14))            
tcp              LISTEN            0                 128                                0.0.0.0:22                              0.0.0.0:*               users:(("sshd",pid=900,fd=3))                        
tcp              LISTEN            0                 128                                0.0.0.0:8443                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=10))               
tcp              LISTEN            0                 1024                               0.0.0.0:6446                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=13))               
tcp              LISTEN            0                 1024                               0.0.0.0:6447                            0.0.0.0:*               users:(("mysqlrouter",pid=4310,fd=15))               
tcp              LISTEN            0                 128                                   [::]:22                                 [::]:*               users:(("sshd",pid=900,fd=4))                        
root@mysql-router01:/var/lib/mysqlrouter#

查看配置文件监听端口

root@mysql-router01:~# cat /var/lib/mysqlrouter/mysqlrouter.conf 
# File automatically generated during MySQL Router bootstrap

[routing:bootstrap_rw]
bind_address=0.0.0.0
bind_port=6446
destinations=metadata-cache://my_innodb_cluster/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:bootstrap_ro]
bind_address=0.0.0.0
bind_port=6447
destinations=metadata-cache://my_innodb_cluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic

查看缓存的集群信息

root@mysql-router01:~# cat /var/lib/mysqlrouter/data/state.json 
{
    "metadata-cache": {
        "group-replication-id": "806533fa-36ac-11ef-9f9b-005056aaaba0",
        "cluster-metadata-servers": [
            "mysql://mysql-innodb03:3306",
            "mysql://mysql-innodb02:3306",
            "mysql://mysql-innodb01:3306"
        ]
    },
    "version": "1.0.0"
}root@mysql-router01:~# 

禁用默认的systemd服务,配置mysql-router服务开机自启动

systemctl stop mysqlrouter.service
rm -rf /etc/init.d/mysqlrouter
systemctl daemon-reload

新建systemd文件

cat >/etc/systemd/system/mysqlrouter.service<<EOF
[Unit]
Description=MySQL Router
After=network.target
After=syslog.target
[Service]
Type=notify
User=mysqlrouter
Group=mysqlrouter
# Start main service
ExecStart=/usr/bin/mysqlrouter -c /var/lib/mysqlrouter/mysqlrouter.conf --user=mysqlrouter
# Sets open_files_limit
LimitNOFILE = 10000
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

启动服务

systemctl enable --now mysqlrouter.service

连接数据库

创建示例用户

root@mysql-innodb01:~# mysql -uroot -p
Enter password: 

mysql> create user app@'%' identified by 'App@123456';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all on *.* to app@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> 

通过mysql-router连接到读写数据库

root@mysql-router01:~# mysql -h127.0.0.1 -P 6446 -u app -p'App@123456'
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 0
Server version: 8.4.0-router MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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 @@hostname;
+------------+
| @@hostname |
+------------+
| mysql-innodb01   |
+------------+
1 row in set (0.00 sec)

mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)

mysql> 

通过mysql-router连接到只读数据库

root@mysql-router01:~# mysql -h127.0.0.1 -P 6447 -u app -p'App@123456'
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 0
Server version: 8.4.0-router MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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 @@hostname;
+------------+
| @@hostname |
+------------+
| mysql-innodb02   |
+------------+
1 row in set (0.00 sec)

mysql> create database mydb1;
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
mysql> 
mysql> select user, host from mysql.user;
+---------------------------------+-----------+
| user                            | host      |
+---------------------------------+-----------+
| app                             | %         |
| ic                              | %         |
| mysql_innodb_cluster_2444823474 | %         |
| mysql_innodb_cluster_2728360213 | %         |
| mysql_innodb_cluster_3109234783 | %         |
| mysql_router1_d99tpklyj1n3      | %         |
| mysql.infoschema                | localhost |
| mysql.session                   | localhost |
| mysql.sys                       | localhost |
| root                            | localhost |
+---------------------------------+-----------+
10 rows in set (0.00 sec)

mysql> \q
Bye

部署负载局衡器

以下操作在所有lb节点执行。

配置主机名

hostnamectl set-hostname lb01
hostnamectl set-hostname lb02

安装haproxy和keepalived

apt install -y haproxy keepalived

创建haproxy配置文件,两个节点配置相同

root@lb01:~# cat /etc/haproxy/haproxy.cfg
# HAProxy Configuration for lb01
global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode tcp
    option tcplog
    option dontlognull
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend db_rw_front
    bind *:6446
    default_backend db_rw_back

backend db_rw_back
    balance roundrobin
    server router01 192.168.72.41:6446 check
    server router02 192.168.72.42:6446 check

frontend db_ro_front
    bind *:6447
    default_backend db_ro_back

backend db_ro_back
    balance roundrobin
    server router01 192.168.72.41:6447 check
    server router02 192.168.72.42:6447 check

创建keepalived配置文件,两个节点配置相同

root@lb01:~# cat /etc/keepalived/keepalived.conf 
# Keepalived Configuration for lb01
global_defs {
    router_id 51
    script_user root
    enable_script_security
}

vrrp_script check_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    fall 2       # require 2 failures for KO
    rise 2       # require 2 successes for OK
}

vrrp_instance db_vip {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass secret456
    }
    virtual_ipaddress {
        192.168.72.200
    }
    track_script {
        check_haproxy
    }
}

启动服务

systemctl restart keepalived.service
systemctl restart haproxy.service

验证通过VIP连接读写端口

root@mysql-router01:~# mysql -h192.168.72.200 -P 6446 -u app -p'App@123456'
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 392427
Server version: 8.0.37 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> 

验证通过VIP连接只读端口

root@mysql-router01:~# mysql -h192.168.72.200 -P 6447 -u app -p'App@123456'
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 71
Server version: 8.0.37 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> 

集群角色管理

切换为多个Primary模式

 MySQL  localhost:33060+ ssl  JS > herc.switchToMultiPrimaryMode()

切换为单个Primary模式

 MySQL  localhost:33060+ ssl  JS > herc.switchToSinglePrimaryMode()

指定节点切换为Primary模式

 MySQL  localhost:33060+ ssl  JS > herc.setPrimaryInstance('mysql-innodb02')

从集群删除节点

 MySQL  localhost:33060+ ssl  JS > herc.removeInstance('mysql-innodb03')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1884825.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Labview绘制柱状图

废话不多说&#xff0c;直接上图 我喜欢用NXG风格&#xff0c;这里我个人选的是xy图。 点击箭头指的地方 选择直方图 插值选择第一个 直方图类型我选的是第二个效果如图。 程序部分如图。 最后吐槽一句&#xff0c;现在看CSDN好多文章都要收费了&#xff0c;哪怕一些简单的入…

运营商如何通过PCDN技术提高用户服务?

着互联网的快速发展&#xff0c;用户对网络速度和质量的要求越来越高。为了满足这些需求&#xff0c;内容分发网络(CDN)成为了关键的基础设施。而在CDN技术中&#xff0c;PCDN(PersonalCDN)作为一种新兴的技术&#xff0c;为运营商和用户提供了新的解决方案。本文将重点介绍PCD…

vue3中使用Antv G6渲染树形结构并支持节点增删改

写在前面 在一些管理系统中&#xff0c;会对组织架构、级联数据等做一些管理&#xff0c;你会怎么实现呢&#xff1f;在经过调研很多插件之后决定使用 Antv G6 实现&#xff0c;文档也比较清晰&#xff0c;看看怎么实现吧&#xff0c;先来看看效果图。点击在线体验 实现的功能…

图形的搭建

例一&#xff1a; 输入描述&#xff1a; 多组输入&#xff0c;一个整数&#xff08;2~20&#xff09;&#xff0c;表示输出的行数&#xff0c;也表示组成“X”的反斜线和正斜线的长度。 输出描述&#xff1a; 针对每行输入&#xff0c;输出用“*”组成的X形图案。 示例一&…

macos m2 百度paddleocr文字识别 python

创建了一个虚拟环境&#xff1a;conda create -n orc python3.11.7 进入虚拟环境后执行2条命令 pip install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple​ ​ 安装好后&#xff0c;在网…

VUE2及其生态查漏补缺

1、数据代理概括 数据代理过程相当于是进行了 vm 代理 vm_data中的属性&#xff0c;vm._data 是与 我们vue文件中写的 data是全等的 //创建Vue实例let data { //data中用于存储数据&#xff0c;数据供el所指定的容器去使用&#xff0c;值我们暂时先写成一个对象。name:atguig…

[C++][设计模式][迭代器模式]详细讲解

目录 1.动机2.模式定义3.要点总结4.代码感受 1.动机 在软件构建过程中&#xff0c;集合对象内部结构常常变化各异。但对于这些集合对象&#xff0c;我们希望不暴露其内部结构的同时&#xff0c;可以让外部客户代码透明地访问其中包含的元素&#xff1b; 同时这种”透明遍历“也…

每天五分钟深度学习:解决for循环效率慢的关键在于向量化

本文重点 上一节课程中,我们学习了多样本的线性回归模型,但是我们的伪代码实现中使用了大量的for循环,这样代码的问题是效率很低。为了克服这一瓶颈,向量化技术应运而生,成为提升程序执行效率、加速数据处理速度的重要手段。 向量化技术概述 向量化(Vectorization)是…

DC/AC电源模块:为智能家居设备提供恒定的电力供应

BOSHIDA DC/AC电源模块&#xff1a;为智能家居设备提供恒定的电力供应 DC/AC电源模块是一种常见的电源转换器&#xff0c;它将直流电源&#xff08;DC&#xff09;转换为交流电源&#xff08;AC&#xff09;&#xff0c;为智能家居设备提供恒定的电力供应。在智能家居系统中&a…

用 AI 生成绘本,含大量 prompt

画图过程&#xff0c;为了保证绘本输出的风格统一&#xff0c;角色连贯&#xff0c;画面内容与故事保持一致 1、画风统一的解决办法&#xff1a;固定一个插画师的风格&#xff0c;可以输入插画师的名字&#xff0c;或者垫图&#xff0c;即上传你需要借鉴风格的图片 2、角色连贯…

探索IT世界的第一步:高考后的暑期学习指南

目录 前言1. IT领域概述1.1 IT领域的发展与现状1.2 IT领域的主要分支1.2.1 软件开发1.2.2 数据科学1.2.3 网络与安全1.2.4 系统与运维 2. 学习路线图2.1 基础知识的学习2.1.1 编程语言2.1.2 数据结构与算法 2.2 实战项目的实践2.2.1 个人项目2.2.2 团队项目 2.3 学习资源的利用…

高考假期预习指南,送给迷茫的你

高考结束&#xff0c;离别了熟悉的地方&#xff0c;踏上远方。 你&#xff0c;&#xff0c;迷茫吗&#xff1f; 大学是什么&#xff1f;到了大学我该怎样学习&#xff1f;真像网上说的毕业即失业吗&#xff1f; 大学是一个让你学会一技之长的地方&#xff0c;到了大学找到自…

非平稳信号的自适应局部迭代滤波(MATLAB)

仍以滚动轴承故障诊断为例&#xff0c;在滚动轴承的运行过程中&#xff0c;其振动信号包含了大量的系统运行状态信息。利用振动信号进行滚动轴承的故障诊断&#xff0c;实际上就是分析振动信号、提取信息的过程。由于非线性力的作用&#xff0c;滚动轴承的故障信号往往是非平稳…

为什么需要做网络安全服务?

网络安全服务之所以重要&#xff0c;是因为它在保护数字资产、维护企业运营、确保法规遵从、防范恶意行为以及建立信任等方面扮演着关键角色。以下是一些主要的理由&#xff1a; 保护核心资产和数据&#xff1a; 数字化转型使得企业数据变得极其宝贵&#xff0c;包括知识产权、…

离散模态信息作为细粒度标记用于多模态知识图谱补全--MyGO

多模态知识图谱&#xff08;MMKG&#xff09;存储了包含有丰富的多模态描述信息的、结构化的世界知识。为了克服其固有的不完整性&#xff0c;多模态知识图谱补全&#xff08;MMKGC&#xff09;希望利用三元组的结构信息及实体的多模态信息&#xff0c;从给定的MMKG中发掘未观察…

智能工厂解决方案—智能数据采集平台

智慧工厂总体架构图 由智能化生产、智能化管理和产业链互联三个层面构成,前两个层面立足于企业自身,以智能工厂为建设目标,实习企业机体自身的智能化,而产业链互联则是以互联网技术为基础,将企业融入到产业链的整体生态环境中,逐步实现制造资源的服务化和云化,并与生态系…

爬数据是什么意思?

爬数据的意思是&#xff1a;通过网络爬虫程序来获取需要的网站上的内容信息&#xff0c;比如文字、视频、图片等数据。网络爬虫&#xff08;网页蜘蛛&#xff09;是一种按照一定的规则&#xff0c;自动的抓取万维网信息的程序或者脚本。 学习一些爬数据的知识有什么用呢&#x…

SAP 接口-银行账号主数据维护接口【MDM->SAP】开发说明书(包括测试样例、程序代码仅作参考,不保证一定可以运行)

接口映射字段 开发通用说明 根据MDM传输字段调用BAPI生成银行账号及开户行。 开户行维护BAPI【BAPI_BANK_CREATE】 银行账号维护BAPI【BAPI_FCLM_BAM_AMD_BNKANT】 接口字段【ZZZH 主账户标识】=1时字段【DTAAI】DME标识赋值:常用; 接口字段【ZZZH 主账户标识】=0时字段…

mysql中的递归函数recursive

递归部门 WITH recursive dept_tree AS (SELECTsd.mine_id AS mine_id,sd.dept_id AS dept_id,sd.tenant_id AS tenant_id,sd.order_num,sd.dept_name AS topName,sd.dept_id AS topIdFROMsys_dept sdWHERE<!-- 加上or后也会查询出dept节点 sd.parent_id #{deptId} or sd.…

关于电商平台分类||电商平台商品分类接口|电商平台商品数据

电商平台 做电商&#xff0c;则要有电商平台&#xff0c;一个为 企业 或 个人 提供网上交易洽谈的平台。. 企业电子商务平台是建立在 Internet 网上进行商务活动的虚拟网络空间和保障商务顺利运营的管理环境&#xff1b;是协调、整合 信息流 、货物流、 资金流 有序、关联、高效…