MySQL Router的安装部署


Posted in MySQL onApril 24, 2021

MySQL Router介绍

    前面的文章中,我们说了ReplicaSet的基本概念MySQL Shell工具以及如何使用MySQL Shell搭建Innodb Replicaset。今天我们来看InnoDB ReplicaSet部署过程中的另一个重要组件MySQL Router。

MySQL Router是什么?

    为了更好的理解Innodb Replicaset,我们把之前的那张图搬迁过来,如下: 

MySQL Router的安装部署

通过上面的图,不难看出,MySQL Router是应用程序连接的入口,它的存在,让底层的架构对应用程序透明,应用程序只需要连接MySQL Router就可以和底层的数据库打交道,而数据库的主从架构,都是记录在MySQL Router的原信息里面的。

版本

    MySQL Router是MySQL官方的一款轻量级的高性能中间件,它介于应用程序和MySQL Server之间,应用程序通过连接MySQL Router和底层的MySQL Server之间通信,它对应用程序是透明的。官方文档建议搭配MySQL8.0或者MySQL5.7的Server使用。目前最新的版本是MySQL Router 8,如果您使用过MySQL Router的2.0或者2.1版本,强烈建议升级到MySQL Router 8。

MySQL Router特点?

1、对应用透明。MySQL Router要做到对应用透明,它必须连接底层的MySQL,并知道当前哪个节点是Primary,这样,才能够在发生故障的时候进行故障转移。

2、使用场景。基于这个特性,它可以用在Innodb Cluster、Innodb Replicaset或者MGR的环境中。

3、MySQL Router会保留在线的MySQL实例的缓存列表,或者已经配置好的Innodb Cluster集群的拓扑关系,除此启动的时候,这些信息将从MySQL Router的配置表中获取。

4、为保证缓存中的元信息能够得到即时更新,MySQL Router需要保证至少能够和集群中的一个正常节点保持通信,它会从当前实例的Performance_schema表中获取数据库的原信息和实时状态。

5、当集群中和MySQL Router通信的节点关闭时,MySQL Router会尝试访问集群中的其他节点。并重新获取相关元数据信息。

02 MySQL Router的安装部署

      为了获取更好的性能,通常情况下,MySQL  Router会和应用程序部署在一起,这通常是基于下面的考虑:

1、可以通过socket套接字连接到MySQL Router,而不是tcp/ip方法

2、减少了网络上的延时

3、可以配置指定的账户来访问数据库,例如myapp@'host'而不是类似myapp@'%'这种全网段的账号,有利于提升安全性

4、通常,相比数据库服务器,应用服务器更容易扩展。

官方给的部署架构图如下:

MySQL Router的安装部署

下载安装过程:

1、直接登录官网下载MySQL Router的对应版本。

https://downloads.mysql.com/archives/router/

2、下载完成之后,解压,以8.0.20为例,由于是tar.xz格式的文件,解压命令如下:

xz -d xxx.tar.xz  (解压成tar格式)

tar xvf xxx.tar     (即可)

接下来就是初始化过程了,初始化的时候,需要我们的MySQL Server部署完毕,在之前的文章中,我们已经部署好了一个Innodb Replicaset架构,它有一主一从,IP地址分别是:

192.168.1.10  5607 Primary

192.168.1.20  5607 Secondary

初始化过程:

1、利用初始化命令初始化MySQL Router:

mysqlrouter
   --bootstrap superdba@'10.13.3.129':5607 
   --directory /data1/yazhou5/mysql/mysql-router
   --conf-use-sockets 
   --account routerfriend 
   --account-create always

这里,需要解释一下其中的几个参数:

--bootstrap          代表引导的实例,后面接一个连接信息的URL;
--directory          代表生成的配置目录
--conf-use-sockets   代表是否启用套接字连接(是否生成套接字文件)
--account            代表初始化后MySQL Router使用什么账号连接MySQL Server
--account-create     代表账号创建策略,always代表只有在account不存在的时候才进行bootstrap操作

这个命令敲下去之后,返回报错信息如下:

Error: You are bootstraping as a superuser.
This will make all the result files (config etc.) privately owned by the superuser.
Please use --user=username option to specify the user that will be running the router.
Use --user=root if this really should be the superuser.

系统检测到我们使用root账号来进行的MySQL Router初始化,提示我们如果使用root操作,需要在最后面补充--user=root

2、补充--user=root之后,重新执行命令,结果如下:

[root mysql-router]# /usr/local/mysql-router-8.0.20/bin/mysqlrouter --bootstrap superdba@10.185.13.195:5607  --directory /data1/yazhou5/mysql/mysql-router --conf-use-sockets  --account routerfriend  --account-create always --user=root --force
Please enter MySQL password for superdba:   # 这里输入我们已知的superdba账号密码
# Bootstrapping MySQL Router instance at '/data1/yazhou5/mysql/mysql-router'...

Please enter MySQL password for routerfriend: # 这里创建新的account账号的密码
- Creating account(s) 
- 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 /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf

# MySQL Router configured for the InnoDB ReplicaSet 'yeyz_test'

After this MySQL Router has been started with the generated configuration

    $ /usr/local/mysql-router-8.0.20/bin/mysqlrouter -c /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf

the cluster 'yeyz_test' can be reached by connecting to:

## MySQL Classic protocol

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

## MySQL X protocol

- Read/Write Connections: localhost:64460, /data1/yazhou5/mysql/mysql-router/mysqlx.sock
- Read/Only Connections:  localhost:64470, /data1/yazhou5/mysql/mysql-router/mysqlxro.sock

可以看到,提示我们输入两次密码之后,bootstrap的操作就算成功了。

3、此时我们进入参数中指定的--directory目录中,查看生成的初始化文件,可以看到:

drwx------ 2 root root 4096 Apr 12 23:15 data
drwx------ 2 root root 4096 Apr 12 23:15 log
-rw------- 1 root root 1532 Apr 12 23:15 mysqlrouter.conf
-rw------- 1 root root  104 Apr 12 23:15 mysqlrouter.key
drwx------ 2 root root 4096 Apr 12 23:15 run
-rwx------ 1 root root  353 Apr 12 23:15 start.sh
-rwx------ 1 root root  209 Apr 12 23:15 stop.sh

生成了一些配置文件和启停脚本,我们打开这个配置文件mysqlrouter.conf看看内容:

# File automatically generated during MySQL Router bootstrap
[DEFAULT]
user=root
logging_folder=/data1/yazhou5/mysql/mysql-router/log
runtime_folder=/data1/yazhou5/mysql/mysql-router/run
data_folder=/data1/yazhou5/mysql/mysql-router/data
keyring_path=/data1/yazhou5/mysql/mysql-router/data/keyring
master_key_path=/data1/yazhou5/mysql/mysql-router/mysqlrouter.key
connect_timeout=15
read_timeout=30
dynamic_state=/data1/yazhou5/mysql/mysql-router/data/state.json

[logger]
level = INFO

[metadata_cache:yeyz_test]
cluster_type=rs
router_id=1
user=routerfriend
metadata_cluster=yeyz_test
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2

[routing:yeyz_test_rw]
bind_address=0.0.0.0
bind_port=6446
socket=/data1/yazhou5/mysql/mysql-router/mysql.sock
destinations=metadata-cache://yeyz_test/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:yeyz_test_ro]
bind_address=0.0.0.0
bind_port=6447
socket=/data1/yazhou5/mysql/mysql-router/mysqlro.sock
destinations=metadata-cache://yeyz_test/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic

[routing:yeyz_test_x_rw]
bind_address=0.0.0.0
bind_port=64460
socket=/data1/yazhou5/mysql/mysql-router/mysqlx.sock
destinations=metadata-cache://yeyz_test/?role=PRIMARY
routing_strategy=first-available
protocol=x

[routing:yeyz_test_x_ro]
bind_address=0.0.0.0
bind_port=64470
socket=/data1/yazhou5/mysql/mysql-router/mysqlxro.sock
destinations=metadata-cache://yeyz_test/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x

4、分析生成的配置文件,不难发现,MySQL Router配置了4个端口,分别是6446、6447、64460、64470和对应的套接字文件。

当然,我们可以通过一些参数的配置改变默认的端口和套接字,例如:

--conf-use-sockets:(可选)为所有四种连接类型启用UNIX域套接字,。
--conf-skip-tcp:   (可选)禁用TCP端口,如果只希望使用套接字,则可以通过--conf-use-sockets传递该选项。
--conf-base-port:  (可选)更改端口范围,而不使用默认端口。 默认为6446。
--conf-bind-address:(可选)更改每个路由的bind_address值。 

5、使用命令在本地启动MySQL Router,指定本地生成的配置文件,命令如下:

[root@ mysql-router]# /usr/local/mysql-router-8.0.20/bin/mysqlrouter -c /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf &

启动之后,我们使用刚才创建的routerfriend账号以及6446这个读写端口来连接MySQL Router:

[root@ mysql-router]# mysql -u routerfriend -h 127.0.0.1 -P 6446  -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 95696
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2013, 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.

routerfriend@127.0.0.1 [(none)] 23:42:00>
routerfriend@127.0.0.1 [(none)] 23:42:01>select @@port;
+--------+
| @@port |
+--------+
|   5607 |
+--------+
1 row in set (0.00 sec)

我们通过6446端口连接MySQL Router之后,然后在MySQL Router中执行select @@port命令查看当前的端口号信息,可以看到,返回值是5607,说明MySQL Router已经帮我们路由到了底层的MySQL Server上面。

   这个routerfriend账号的权限可能不够,我们也可以换成superdba的高权限账号去连接mysqlrouter,这样就可以对MySQL Server中的库表进行读写操作。

03 查看MySQL Router的元信息

    MySQL Router搭建完毕后,可以通过查看元信息库mysql_innodb_cluster_metadata里面的表信息,包含cluster表、router表、以及instances表,对应的如下:

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:20>select * from instances;
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
| instance_id | cluster_id                           | address            | mysql_server_uuid                    | instance_name      | addresses                              | attributes | description |
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
|           1 | 94d5f935-990e-11eb-8832-fa163ebd2444 | 192.168.1.10:5607 | 0609f966-690f-11eb-bd89-fa163ebd2444 | 192.168.1.10:5607 | {"mysqlClassic": "192.168.1.10:5607"} | {}         | NULL        |
|           2 | 94d5f935-990e-11eb-8832-fa163ebd2444 | 192.168.1.20:5607  | c6ba0bf0-6d4d-11eb-aa4b-b00875209c1c | 192.168.1.20:5607  | {"mysqlClassic": "192.168.1.20:5607"}  | {}         | NULL        |
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
2 rows in set (0.00 sec)

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:30>
superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:30>select * from clusters;
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
| cluster_id                           | cluster_name | description        | options | attributes                                     | cluster_type | primary_mode | router_options |
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
| 94d5f935-990e-11eb-8832-fa163ebd2444 | yeyz_test    | Default ReplicaSet | NULL    | {"adopted": 0, "opt_gtidSetIsComplete": false} | ar           | pm           | NULL           |
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
1 row in set (0.00 sec)

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:57>
superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:58>select * from routers;
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
| router_id | router_name | product_name | address     | version | last_check_in       | attributes                                                                                                                   | cluster_id                           | options |
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
|         1 |             | MySQL Router | 10.13.3.129 | 8.0.20  | 2021-04-12 23:52:29 | {"ROEndpoint": "6447", "RWEndpoint": "6446", "ROXEndpoint": "64470", "RWXEndpoint": "64460", "MetadataUser": "routerfriend"} | 94d5f935-990e-11eb-8832-fa163ebd2444 | NULL    |
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
1 row in set (0.00 sec)

还可以从表中查看当前的primary节点信息,primary_master字段为1的,即为primary节点。

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:52:29>select * from async_cluster_members;
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
| cluster_id                           | view_id | instance_id | master_instance_id | primary_master | attributes                                                                                                       |
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       2 |           1 |               NULL |              1 | {"instance.address": "192.168.1.10:5607", "instance.mysql_server_uuid": "0609f966-690f-11eb-bd89-fa163ebd2444"} |
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       3 |           1 |               NULL |              1 | {"instance.address": "192.168.1.10:5607", "instance.mysql_server_uuid": "0609f966-690f-11eb-bd89-fa163ebd2444"} |
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       3 |           2 |                  1 |              0 | {"instance.address": "192.168.1.20:5607", "instance.mysql_server_uuid": "c6ba0bf0-6d4d-11eb-aa4b-b00875209c1c"}  |
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)

以上就是MySQL Router的安装部署的详细内容,更多关于MySQL Router的资料请关注三水点靠木其它相关文章!

MySQL 相关文章推荐
MySQL pt-slave-restart工具的使用简介
Apr 07 MySQL
MySQL中你可能忽略的COLLATION实例详解
May 12 MySQL
超详细教你怎么升级Mysql的版本
May 19 MySQL
MySQL中IF()、IFNULL()、NULLIF()、ISNULL()函数的使用详解
Jun 26 MySQL
mysql定时自动备份数据库的方法步骤
Jul 07 MySQL
Mysql关于数据库是否应该使用外键约束详解说明
Oct 24 MySQL
Linux7.6二进制安装Mysql8.0.27详细操作步骤
Nov 27 MySQL
Mysql中一千万条数据怎么快速查询
Dec 06 MySQL
深入理解mysql事务隔离级别和存储引擎
Apr 12 MySQL
Windows 64位 安装 mysql 8.0.28 图文教程
Apr 19 MySQL
Mysql中常用的join连接方式
May 11 MySQL
MySQL存储过程及语法详解
Aug 05 MySQL
MySQL Shell的介绍以及安装
MySQL InnoDB ReplicaSet(副本集)简单介绍
Apr 24 #MySQL
MySQL 角色(role)功能介绍
Apr 24 #MySQL
浅谈Mysql多表连接查询的执行细节
Mysql基础之常见函数
Apr 22 #MySQL
MySQL分库分表与分区的入门指南
MySQL8.0.24版本Release Note的一些改进点
Apr 22 #MySQL
You might like
深入分析php之面向对象
2013/05/15 PHP
PHP定时任务获取微信access_token的方法
2016/10/10 PHP
redirect_uri参数错误的解决方法(必看)
2017/02/16 PHP
php之header的不同用法总结(实例讲解)
2017/11/28 PHP
纯js实现的论坛常用的运行代码的效果
2008/07/15 Javascript
一个页面元素appendchild追加到另一个页面元素的问题
2013/01/27 Javascript
jquery的ajaxSubmit()异步上传图片并保存表单数据演示代码
2013/06/04 Javascript
浅析js预加载/延迟加载
2014/09/25 Javascript
BootStrap中按钮点击后被禁用按钮的最佳实现方法
2016/09/23 Javascript
AngularJS中的按需加载ocLazyLoad示例
2017/01/11 Javascript
微信小程序授权获取用户详细信息openid的实例详解
2017/09/20 Javascript
微信小程序websocket实现聊天功能
2020/03/30 Javascript
微信小程序发送短信验证码完整实例
2019/01/07 Javascript
基于vue+axios+lrz.js微信端图片压缩上传方法
2019/06/25 Javascript
JavaScript在web自动化测试中的作用示例详解
2019/08/25 Javascript
JavaScript中的函数申明、函数表达式、箭头函数
2019/12/06 Javascript
浅谈Vue开发人员的7个最好的VSCode扩展
2021/01/20 Vue.js
[47:04]EG vs RNG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
python3+PyQt5+Qt Designer实现扩展对话框
2018/04/20 Python
python 3.3 下载固定链接文件并保存的方法
2018/12/18 Python
Python3.5模块的定义、导入、优化操作图文详解
2019/04/27 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
2019/05/06 Python
基于pytorch的lstm参数使用详解
2020/01/14 Python
Python内存映射文件读写方式
2020/04/24 Python
python里反向传播算法详解
2020/11/22 Python
德国体育用品网上商店:SC24.com
2016/08/01 全球购物
Shopty西班牙:缝纫机在线销售
2018/01/26 全球购物
垃圾回收的优点和原理
2014/05/16 面试题
物流专业大学生职业生涯规划书范文
2014/01/15 职场文书
自行车广告词大全
2014/03/21 职场文书
社区居务公开实施方案
2014/03/27 职场文书
大学生标准自荐书
2014/06/15 职场文书
幼儿园庆六一主持词
2015/06/30 职场文书
党性教育心得体会(共6篇)
2016/01/21 职场文书
springcloud之Feign超时问题的解决
2021/06/24 Java/Android
MySQL 数据库范式化设计理论
2022/04/22 MySQL