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官方的一款轻量级的高性能中间件,它介于应用程序和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、可以配置指定的账户来访问数据库,例如而不是类似这种全网段的账号,有利于提升安全性
4、通常,相比数据库服务器,应用服务器更容易扩展。
官方给的部署架构图如下:
下载安装过程:
1、直接登录官网下载mysql 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的资料请关注www.887551.com其它相关文章!