MySQL Router

安装前提

  • MGR 集群已安装
  • 已使用 MySQL Shell 注册个集群信息

安装

  • 创建 mysql router 运行用户
1
useradd -s /sbin/nologin -r -m mysqlrouter
  • 创建安装目录并解压
1
2
3
4
5
mkdir -p /data/mysql-router/
tar -xvf mysql-router-8.0.30-linux-glibc2.12-x86_64.tar.xz -C /data/mysql-router/
cd /data/mysql-router
ln -sfn mysql-router-8.0.30-linux-glibc2.12-x86_64/ mysql-router-8.0.30
chown mysqlrouter.mysqlrouter -R .
  • 初始化 mysql router
    • –bootstrap:执行初始化
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
./mysql-router-8.0.30/bin/mysqlrouter --bootstrap mgr-user@172.16.2.181:3306 --user=mysqlrouter --name=mysql-route01 --directory=/data/mysql-router/data
## 输入密码
Please enter MySQL password for mgr-user:
# Bootstrapping MySQL Router instance at '/data/mysql-router/data'...

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

# MySQL Router 'mysql-route01' configured for the InnoDB Cluster 'TEST-MGR'

After this MySQL Router has been started with the generated configuration

    $ ./mysql-router-8.0.30/bin/mysqlrouter -c /data/mysql-router/data/mysqlrouter.conf

InnoDB Cluster 'TEST-MGR' can be reached by connecting to:

## MySQL Classic protocol

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

## MySQL X protocol

- Read/Write Connections: localhost:6448
- Read/Only Connections:  localhost:6449
  • 启动
1
2
cd /data/mysql-router/data
./start.sh

测试

  • 查看端口配置
    • bootstrap_rw:连接 PRIMARY 节点
    • bootstrap_ro:轮询查询 SECONDARY 节点
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$ cat /data/mysql-router/data/mysqlrouter.conf
......
[routing:bootstrap_rw]
bind_address=0.0.0.0
bind_port=6446
destinations=metadata-cache://TEST-MGR/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:bootstrap_ro]
bind_address=0.0.0.0
bind_port=6447
destinations=metadata-cache://TEST-MGR/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic
......
  • 连接 mysql 读写节点(6446)端口
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ /data/mysql/mysql8.0.30/bin/mysql -u xxx -p25241230 -h 127.0.0.1 -P 6446
# 查询节点主机名,确定连接的是 PRIMARY 节点
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| mgr01      |
+------------+
1 row in set (0.00 sec)
# 测试写入数据
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> show databases like "test";
+-----------------+
| Database (test) |
+-----------------+
| test            |
+-----------------+
  • 连接 mysql 只读节点(6447)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ /data/mysql/mysql8.0.30/bin/mysql -u xxx -p25241230 -h 127.0.0.1 -P 6447
# 查询连接节点
mysql>  select @@hostname;
+------------+
| @@hostname |
+------------+
| mgr02      |
+------------+
# 测试写入数据(无法写入)
mysql> create database ccc;
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement