使用MySQL Shell搭建InnoDB Cluster集群

2024年 5月 24日 59.8k 0

InnoDB Cluster 集群介绍

InnoDB Cluster集群是MySQL官方推出的高可用方案。一个InnoDB Cluster至少由三个MySQL Server实例组成,它提供了高可用性和可扩展性。通过使用MySQL Shell包含的AdminAPI,你可以很容易地配置和管理一组至少三个MySQL服务器实例,作为一个InnoDB集群。

InnoDB集群中的每个MySQL服务器实例都运行MySQL组复制,它提供了在InnoDB集群内复制数据的机制,并具有内置的故障转移。AdminAPI消除了在InnoDB集群中直接使用组复制的需要,但要了解更多信息,请参阅组复制,其中解释了详细信息。从MySQL 8.0.27开始,你也可以设置InnoDB ClusterSet,通过连接一个主InnoDB集群和一个或多个自己在其他位置的副本,比如不同的数据中心,为InnoDB集群部署提供容灾能力。

MySQL Router 可以根据你部署的集群自动配置自己,透明地将客户端应用程序连接到服务器实例。如果服务器实例发生意外故障,集群将自动重新配置。在默认的单主模式下,一个InnoDB集群只有一个可读写的服务器实例——主实例。多个辅助服务器实例是主服务器实例的副本。如果主服务器出现故障,备用服务器将自动升级为主服务器。MySQL Router 检测到这一点,并将客户端应用程序转发到新的主服务器。高级用户还可以将集群配置为具有多个主节点。

MySQL Router 软件通常与业务应用部署在一起,业务应用通过MySQL Router对底层MySQL MGR 数据库进行数据请求操作。MySQL Router可动态感知底层MySQL MGR 集群各节点的健康状态并根据这些信息对业务请求进行路由转发,通过不同的端口实现读写分离功能。

InnoDB集群使用了下面的MySQL技术:
1、MySQL Shell,这是MySQL的高级客户端和代码编辑器。
2、MySQL服务器和组复制,这使得一组MySQL实例提供高可用性。InnoDB集群提供了另一种易于使用的编程方式来处理组复制。
3、MySQL Router,一个轻量级中间件,在你的应用程序和InnoDB集群之间提供透明路由。

官方 InnoDB Cluster 架构图

使用MySQL Shell搭建InnoDB Cluster集群-1

环境信息

主机名 ip地址 OS版本 内存、CPU 安装软件 端口
node1 192.168.100.10 Centos7.9 4G 、 1个双核 MySQL Server 8.0.36
MySQL Shell 8.0.36
MySQL Router 8.0.36
3306
/
6446(写)6447(读)
node2 192.168.100.11 Centos7.9 4G 、 1个双核 MySQL Server 8.0.36
MySQL Shell 8.0.36
MySQL Router 8.0.36
3306
/
6446(写)6447(读)
node3 192.168.100.12 Centos7.9 4G 、 1个双核 MySQL Server 8.0.36
MySQL Shell 8.0.36
MySQL Router 8.0.36
3306
/
6446(写)6447(读)

InnoDB Cluster 安装过程

下载软件

MySQL Shell 8.0.36
下载地址:https://downloads.mysql.com/archives/shell/
软件名称:mysql-shell-8.0.36-linux-glibc2.17-x86-64bit.tar.gz

MySQL Router 8.0.36
下载地址:https://downloads.mysql.com/archives/router/
软件名称:mysql-router-8.0.36-linux-glibc2.17-x86_64.tar.xz

MySQL Server 8.0.36
下载地址:https://downloads.mysql.com/archives/router/
软件名称:mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz

配置系统

关闭防火墙

systemctl disable firewalld.service
systemctl stop firewalld.service
systemctl status firewalld

关闭SELINUX

vi /etc/selinux/config
修改SELINUX=disabled
或者
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

配置/etc/hosts

vi /etc/hosts
192.168.100.10 node1
192.168.100.11 node2
192.168.100.12 node3

配置内核

禁用透明大页

#检查THP开启情况
cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

### 关闭THP
echo never > /sys/kernel/mm/transparent_hugepage/enabled
## 设置重启后自动关闭
chmod +x /etc/rc.d/rc.local
systemctl enable rc-local.service

cat >> /etc/rc.d/rc.local /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF

配置资源限制

cat >>/etc/security/limits.conf >/etc/sysctl.conf alter user root@'localhost' identified by 'Root';
mysql> flush privileges;
mysql> SET SQL_LOG_BIN=1;

配置MySQL SERVER

--三个节点都执行
--使用mysql shell连接到mysql实例
\c root@localhost?socket=(/data/data/mysql.sock)

--配置实例
dba.configureInstance()
这一步会创建可以远程连接的账号

过程如下:
使用MySQL Shell搭建InnoDB Cluster集群-2
使用MySQL Shell搭建InnoDB Cluster集群-3

创建InnoDB Cluster集群

var cluster = dba.createCluster('mycluster');

过程如下:
使用MySQL Shell搭建InnoDB Cluster集群-4

添加InnoDB Cluster集群其他节点

c.addInstance('192.168.100.11:3306') --选择clone的恢复方式
c.addInstance('192.168.100.12:3306') --选择clone的恢复方式

过程如下:
使用MySQL Shell搭建InnoDB Cluster集群-5
使用MySQL Shell搭建InnoDB Cluster集群-6

查看InnoDB Cluster集群状态

c.status()

过程如下:
使用MySQL Shell搭建InnoDB Cluster集群-7
使用MySQL Shell搭建InnoDB Cluster集群-8

使用mysql客户端查询mgr成员信息

mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+----------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+----------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 05c27710-1843-11ef-80a9-00505621b833 | 192.168.100.12 | 3306 | ONLINE | SECONDARY | 8.0.36 | MySQL |
| group_replication_applier | 122d7061-1843-11ef-8ddb-005056286a23 | 192.168.100.10 | 3306 | ONLINE | PRIMARY | 8.0.36 | MySQL |
| group_replication_applier | 141bfb53-1843-11ef-b4e7-005056393b43 | 192.168.100.11 | 3306 | ONLINE | SECONDARY | 8.0.36 | MySQL |
+---------------------------+--------------------------------------+----------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.00 sec)

配置 MySQL Router

安装 MySQL Router

生产环境中一般是在应用服务器上安装 MySQL Router,并且做Router的高可用。这里测试,在三个节点使用mysql用户安装MySQL Router

tar -xvf /soft/mysql-router-8.0.36-linux-glibc2.17-x86_64.tar.xz -C /data/
cd /data
mv mysql-router-8.0.36-linux-glibc2.17-x86_64 mysqlrouter

初始化 MySQL Router

在三个节点做初始化

cd /data/mysqlrouter/bin

--初始化,地址写primary的地址
mysqlrouter --bootstrap root@192.168.100.10:3306 --directory /data/mysqlrouter/router --conf-base-port 6446 --conf-use-sockets --force
初始化后,创建了配置文件:/data/mysqlrouter/router/mysqlrouter.conf

过程如下:

[mysql@node1 mysqlrouter]$ mysqlrouter --bootstrap root@192.168.100.10:3306 --directory /data/mysqlrouter/router --conf-base-port 6446 --conf-use-sockets --force
Please enter MySQL password for root:
# Bootstrapping MySQL Router 8.0.36 (MySQL Community - GPL) instance at '/data/mysqlrouter/router'...

- 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 /data/mysqlrouter/router/mysqlrouter.conf

# MySQL Router configured for the InnoDB Cluster 'mycluster'

After this MySQL Router has been started with the generated configuration

$ mysqlrouter -c /data/mysqlrouter/router/mysqlrouter.conf

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

## MySQL Classic protocol

- Read/Write Connections: localhost:6446, /data/mysqlrouter/router/mysql.sock
- Read/Only Connections: localhost:6447, /data/mysqlrouter/router/mysqlro.sock

## MySQL X protocol

- Read/Write Connections: localhost:6448, /data/mysqlrouter/router/mysqlx.sock
- Read/Only Connections: localhost:6449, /data/mysqlrouter/router/mysqlxro.sock

[mysql@node1 mysqlrouter]$

查看生成的配置文件

[mysql@node1 router]$ cat mysqlrouter.conf
# File automatically generated during MySQL Router bootstrap
[DEFAULT]
logging_folder=/data/mysqlrouter/router/log
runtime_folder=/data/mysqlrouter/router/run
data_folder=/data/mysqlrouter/router/data
keyring_path=/data/mysqlrouter/router/data/keyring
master_key_path=/data/mysqlrouter/router/mysqlrouter.key
connect_timeout=5
read_timeout=30
dynamic_state=/data/mysqlrouter/router/data/state.json
client_ssl_cert=/data/mysqlrouter/router/data/router-cert.pem
client_ssl_key=/data/mysqlrouter/router/data/router-key.pem
client_ssl_mode=PREFERRED
server_ssl_mode=AS_CLIENT
server_ssl_verify=DISABLED
unknown_config_option=error

[logger]
level=INFO

[metadata_cache:bootstrap]
cluster_type=gr
router_id=1
user=mysql_router1_5ru2e8qxks27
metadata_cluster=mycluster
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2
use_gr_notifications=0

[routing:bootstrap_rw]
bind_address=0.0.0.0
bind_port=6446
socket=/data/mysqlrouter/router/mysql.sock
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:bootstrap_ro]
bind_address=0.0.0.0
bind_port=6447
socket=/data/mysqlrouter/router/mysqlro.sock
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic

[routing:bootstrap_x_rw]
bind_address=0.0.0.0
bind_port=6448
socket=/data/mysqlrouter/router/mysqlx.sock
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=x

[routing:bootstrap_x_ro]
bind_address=0.0.0.0
bind_port=6449
socket=/data/mysqlrouter/router/mysqlxro.sock
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x

[http_server]
port=8443
ssl=1
ssl_cert=/data/mysqlrouter/router/data/router-cert.pem
ssl_key=/data/mysqlrouter/router/data/router-key.pem

[http_auth_realm:default_auth_realm]
backend=default_auth_backend
method=basic
name=default_realm

[rest_router]
require_realm=default_auth_realm

[rest_api]

[http_auth_backend:default_auth_backend]
backend=metadata_cache

[rest_routing]
require_realm=default_auth_realm

[rest_metadata_cache]
require_realm=default_auth_realm

启动 MySQL Router

[mysql@node1 router]$ cd /data/mysqlrouter/router
[mysql@node1 router]$ ls
data log mysqlro.sock mysqlrouter.conf mysqlrouter.key mysqlrouter.pid mysql.sock mysqlxro.sock mysqlx.sock run start.sh stop.sh
[mysql@node1 router]$ ./start.sh
[mysql@node1 router]$ PID 16035 written to '/data/mysqlrouter/router/mysqlrouter.pid'
stopping to log to the console. Continuing to log to filelog

node2和node3操作方法和node1相同

查看MySQL Router信息

var cluster=dba.getCluster()
cluster.listRouters()

过程如下:

MySQL 192.168.100.10:33060+ ssl JS > cluster.listRouters()
{
"clusterName": "mycluster",
"routers": {
"node2::system": {
"hostname": "node2",
"lastCheckIn": null,
"roPort": "6447",
"roXPort": "6449",
"rwPort": "6446",
"rwXPort": "6448",
"version": "8.0.36"
}
}
}

配置MySQL Router的启停服务

编写MySQL Router服务配置文件

vi /usr/lib/systemd/system/mysqlrouter.service
[Unit]
Description=MySQL Router
After=syslog.target
After=network.target

[Service]
Type=simple
User=mysql
Group=mysql

ExecStart=/data/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter.conf

Restart=on-failure

PrivateTmp=true

[Install]
WantedBy=multi-user.target

配置服务

systemctl daemon-reload
systemctl enable mysqlrouter.service
systemctl status mysqlrouter.service
systemctl start mysqlrouter.service

服务状态如下:

[root@node2 bin]# systemctl status mysqlrouter.service
● mysqlrouter.service - MySQL Router
Loaded: loaded (/usr/lib/systemd/system/mysqlrouter.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2024-05-23 13:21:54 CST; 1s ago
Main PID: 18082 (mysqlrouter)
Tasks: 25
CGroup: /system.slice/mysqlrouter.service
└─18082 /data/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter.conf

May 23 13:21:54 node2 systemd[1]: Started MySQL Router.

使用MySQL Router路由测试

读写测试

mysql -h192.168.100.11 -uroot -p'Root' -P6446
create database testdb;
use testdb;
create table testtab;

[mysql@node2 ~]$ mysql -h192.168.100.11 -uroot -p'Root%1234' -P6446
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 68303
Server version: 8.0.36 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> create database testdb;
Query OK, 1 row affected (0.01 sec)

mysql> use testdb;
Database changed
mysql> create table testtab(id int);
Query OK, 0 rows affected (0.05 sec)
结论:当使用6446 RW端口连接时,会连接到后端InnoDB Cluster集群中所有的PRIMARY节点,进行读写操作。

只读测试

[mysql@node2 ~]$ mysql -h192.168.100.11 -uroot -p'Root' -P6447 -e"select @@hostname;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| node3 |
+------------+
[mysql@node2 ~]$ mysql -h192.168.100.11 -uroot -p'Root' -P6447 -e"select @@hostname;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| node2 |
+------------+
[mysql@node2 ~]$ mysql -h192.168.100.11 -uroot -p'Root' -P6447 -e"select @@hostname;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| node3 |
+------------+
[mysql@node2 ~]$ mysql -h192.168.100.11 -uroot -p'Root' -P6447 -e"select @@hostname;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| node2 |
+------------+
结论: 当使用6447 RO端口连接时,会连接到后端InnoDB Cluster集群中所有的SECONDARY节点,并且以rr(round-robin)的策略调度。

参考文档

https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-innodb-cluster.html

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论