what is mysql router
MySQL Router is a lightweight middleware officially provided by MySQL and is part of InnoDB Cluster. It can provide transparent routing between applications and back-end MySQL servers; it is mainly used to solve the problem of MySQL master-slave database. High availability, load balancing, easy expansion and other issues of the cluster. MySQL Router provides a virtual IP for the database cluster as a single connection point for the application. Through this single connection point, it can realize load balancing, read-write separation, failover and other database high-availability solutions.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
1. Introduction
1.1 Introduction to MySQL Router
MySQL Router is a lightweight middleware officially provided by MySQL , part of InnoDB Cluster, provides transparent routing between applications and backend MySQL servers. It is mainly used to solve the problems of high availability, load balancing, and easy expansion of MySQL master-slave database cluster. Router serves as a traffic forwarding layer, located between the application and the MySQL server, and its function is similar to LVS.
1.2 Why use MySQL Router?
Based on the group replication (MGR) mechanism, when the master node goes down and leaves the cluster, the remaining nodes will elect a new master node based on the paxos protocol. There is a problem here. If the application is connected to the master node, and the master node crashes and leaves the cluster, the available database IP address changes. At this time, the client application will still try to connect to the failed node, although the client can be modified. Connection configuration of the end application, but this situation is basically unrealistic.
1.3MySQL Router relationship diagram
Combining MySQL Group Replication and MySQL Shel is as follows:
The above figure fully illustrates the role of MySQL Router in the InnoDB cluster. Its main function is to provide a virtual IP for the database cluster as a single connection point for the application. Through this single connection point, load balancing and read-write separation are achieved. , failover and other database high availability solutions.
MySQL Router is recommended to be installed on the machine where the application is located, for the following reasons:
- Connecting through a local Unix socket instead of TCP/IP improves performance
- Reduce network latency
- MySQL instance does not require additional accounts, only one router@198.51.100.45, instead of myapp@%
- Improves the scalability of the application server
1.4 Introduction to MySQL Cluster
Cluster is a virtual node in this high availability solution. It will create a virtual node on all members of MGR. The database named MySQL_innodb_cluster_metadata stores the metadata information of the cluster, including cluster information, cluster members, group replication information, connected MySQL Router and other information to provide MySQL Router query. It is equivalent to a layer of logical encapsulation of the members in group replication, displayed in a cluster mode. The status of each node is synchronized in real time with the status of the corresponding instance members in group replication, but the cluster nodes and group replication The members of the group are only synchronized when the cluster is created. The member changes in the later group replication are not automatically synchronized to the cluster. You can manually add or remove nodes in the cluster. This makes the application-oriented specific instances more controllable and flexible. Available.
2. Environment preparation
Host name | IP address | Role |
---|---|---|
oratest51 | 172.16.9.51 | primary |
oratest52 | 172.16.9.52 | seconde |
test61 | 172.16.9.61 | seconde |
node4 | 172.16.8.68 | MySQL Router |
Operating system: CentOS Linux release 7.2.1511
MySQL version: mysql-5.7.26-linux-glibc2.12-x86_64
MySQL Router version: mysql-router-8.0.17-linux-glibc2. 12-x86_64
MySQL Shell version: mysql-shell-8.0.17-linux-glibc2.12-x86-64bit
3. Install and configure MySQL Router
MySQL Router has two deployment modes:
- bootstrap mode: supports failover, must be used in conjunction with InnoDB Cluster, and automatically generates the installation directory under the path specified by
--directory
. The ports in the configuration file are 6446 and 6447. - Simple mode: does not support failover and does not need to be used with InnoDB Cluster. It is generally used in master-slave replication or master-master replication modes. Use
/usr/local/mysqlrouter/share/doc/mysqlrouter/ sample_mysqlrouter.conf
This configuration file is deployed.
3.1 Installing MySQL Router in bootstrap mode
Note: The prerequisite for installing MySQL Router in bootstrap mode is that MGR has been installed , MySQL Shell has been installed, InnoDB Cluster has been configured
3.1.1 Install MySQL Router using yum source
# wget -P /software/ https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm # rpm -Uvh /software/mysql80-community-release-el7-3.noarch.rpm # yum -y install mysql-router
3.1.2 View MySQL Router's default configuration
[root@node4 yum.repos.d]# mysqlrouter --help MySQL Router Ver 8.0.16 for Linux on x86_64 (MySQL Community - GPL) Copyright (c) 2015, 2019, 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. Configuration read from the following files in the given order (enclosed in parentheses means not available for reading): /etc/mysqlrouter/mysqlrouter.conf (/root/.mysqlrouter.conf) Plugins Path: /usr/lib64/mysqlrouter Default Log Directory: /var/log/mysqlrouter Default Persistent Data Directory: /var/lib/mysqlrouter Default Runtime State Directory: /run/mysqlrouter
You can see some default paths through mysqlrouter --help:
- Configuration file: /etc/mysqlrouter/mysqlrouter.conf
- Plug-in path: /usr/lib64/mysqlrouter
- Log path: /var/log/mysqlrouter
- Persistent data path: /var/lib/mysqlrouter
- Runtime status path: /run/mysqlrouter
If you use the --config or -c option on the mysqlrouter command line to pass in a user-defined configuration file, The default configuration file will not be loaded.
3.1.3 Initialize MySQL Router, fill in the address of the primary
[root@node4 tmp]# mysqlrouter --bootstrap root@172.16.9.51:3306 --directory /data/mysqlrouter --conf-use-sockets --user=root Please enter MySQL password for root: WARNING: The MySQL server does not have SSL configured and metadata used by the router may be transmitted unencrypted. # Bootstrapping MySQL Router instance at '/data/mysqlrouter'... - Checking for old Router accounts - No prior Router accounts found - Creating mysql account mysql_router1_zhi3m2uhudci@'%' for cluster management - Storing account in keyring - Adjusting permissions of generated files - Creating configuration /data/mysqlrouter/mysqlrouter.conf # MySQL Router configured for the InnoDB cluster 'st' #InnoDB集群名 After this MySQL Router has been started with the generated configuration $ mysqlrouter -c /data/mysqlrouter/mysqlrouter.conf the cluster 'st' can be reached by connecting to: ## MySQL Classic protocol - Read/Write Connections: localhost:6446, /data/mysqlrouter/mysql.sock - Read/Only Connections: localhost:6447, /data/mysqlrouter/mysqlro.sock ## MySQL X protocol - Read/Write Connections: localhost:64460, /data/mysqlrouter/mysqlx.sock - Read/Only Connections: localhost:64470, /data/mysqlrouter/mysqlxro.sock
--bootstrap: Boot and configure the Router to run with the MySQL InnoDB cluster.
3.1.4 Edit the configuration file. Generally, you only need to configure the bootstrap_server_addresses line.
# File automatically generated during MySQL Router bootstrap [DEFAULT] user=root logging_folder=/data/mysqlrouter/log runtime_folder=/data/mysqlrouter/run data_folder=/data/mysqlrouter/data keyring_path=/data/mysqlrouter/data/keyring master_key_path=/data/mysqlrouter/mysqlrouter.key connect_timeout=15 read_timeout=30 #dynamic_state=/data/mysqlrouter/data/state.json [logger] level = INFO [metadata_cache:st] router_id=14 bootstrap_server_addresses=mysql://oratest51:3306,mysql://oratest52:3306,mysql://test61:3306 user=mysql_router14_ebhje7bsnckc metadata_cluster=st ttl=300 use_gr_notifications=0 [routing:st_default_rw] bind_address=0.0.0.0 bind_port=6446 socket=/data/mysqlrouter/mysql.sock destinations=metadata-cache://st/default?role=PRIMARY routing_strategy=first-available protocol=classic [routing:st_default_ro] bind_address=0.0.0.0 bind_port=6447 socket=/data/mysqlrouter/mysqlro.sock destinations=metadata-cache://st/default?role=SECONDARY routing_strategy=round-robin-with-fallback protocol=classic [routing:st_default_x_rw] bind_address=0.0.0.0 bind_port=64460 socket=/data/mysqlrouter/mysqlx.sock destinations=metadata-cache://st/default?role=PRIMARY routing_strategy=first-available protocol=x [routing:st_default_x_ro] bind_address=0.0.0.0 bind_port=64470 socket=/data/mysqlrouter/mysqlxro.sock destinations=metadata-cache://st/default?role=SECONDARY routing_strategy=round-robin-with-fallback protocol=x
3.1.5 Start MySQL Router
[root@node2 mysqlrouter]# cd /data/mysqlrouter/ [root@node2 mysqlrouter]# sh start.sh
The startup process is slow, and the startup information can be seen in the log
3.1.6 Test connection to MySQL Router
By default, 6446 is used as the RW port , use 6447 as RO port
# mysql -uroot -p123456 -h172.16.8.68 -P6446 # mysql -uroot -p123456 -h172.16.8.68 -P6447 # netstat -ntlp |grep mysqlrouter
3.2 Simple mode installation MySQL Router
3.2.1 Download the software package and upload to the server
[root@node4 etc]# mkdir /software;cd /software [root@node4 etc]# wget https://cdn.mysql.com//Downloads/MySQL-Router/mysql-router-8.0.17-linux-glibc2.12-x86_64.tar.xz
3.2.2 Unzip to the specified directory
[root@node4 etc]# tar -Jxvf /software/mysql-router-8.0.17-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ [root@node4 etc]# cd /usr/local [root@node4 etc]# mv mysql-router-8.0.17-linux-glibc2.12-x86_64/ mysqlrouter
3.2.3 Set environment variables
[root@node4 etc]# echo 'export PATH=/usr/local/mysqlrouter/bin/:$PATH' >> /etc/profile [root@node4 etc]# source /etc/profile
3.2.4 Edit Configuration file
# mkdir -p /usr/local/mysqlrouter/etc # cp /usr/local/mysqlrouter/share/doc/mysqlrouter/sample_mysqlrouter.conf /usr/local/mysqlrouter/etc # mv /usr/local/mysqlrouter/etc/sample_mysqlrouter.conf /usr/local/mysqlrouter/etc/mysqlrouter.conf # vim /usr/local/mysqlrouter/etc/mysqlrouter.conf ##部分省略 [DEFAULT] logging_folder =/usr/local/mysqlrouter/log/ plugin_folder = /usr/local/mysqlrouter/lib/mysqlrouter/ config_folder = /usr/local/mysqlrouter/etc/ runtime_folder = /usr/local/mysqlrouter/run/ data_folder = /usr/local/mysqlrouter/data/ keyring_path = /var/lib/keyring-data master_key_path = /var/lib/keyring-key [logger] level = INFO [routing:masters] bind_address = 172.16.8.68:7002 destinations = 172.16.9.61:3306 mode = read-write connect_timeout = 2 [routing:slaves] bind_address = 172.16.8.68:7001 destinations = 172.16.9.51:3306,172.16.9.52:3306 mode = read-only connect_timeout = 1 ##部分省略
/usr/local/mysqlrouter/share/doc/mysqlrouter/sample_mysqlrouter.conf This is the reference configuration file of MySQL Router, so copy the template here first.
Mysql router will look for "mysqlrouter.conf" in the installation directory and ".mysqlrouter.conf" in the home directory by default. You can also use "-c" or "--config" under the binary program mysqlrouter command to manually specify the configuration file.
MySQL Router's configuration file is fragmented. There are three commonly used fragments: [DEFAULT], [logger], [routing:NAME]. The fragment names are case-sensitive and only support a single line of "#". or ";" comment.
- bind_address: Add the IP of the machine where the MySQL Router is located
- destinations: the back-end mysql server IP port
- mode: Provides two modes, read-only and read -write, the scheduling mode of read-write is first-available, and the scheduling mode of read-only is round-robin
- connect_timeout: connection timeout time
- routing_strategy: routing strategy, with round-robin and first-available policy
If it is multi-master mode, the configuration file is as follows
[routing:mutili_rw] bind_address=172.16.8.68 bind_port=7003 destinations=172.16.9.51:3306,172.16.9.52:3306,172.16.9.61:3306 mode=read-write connect_timeout=2 protocol=classic
3.2.5 Create the directory specified in the configuration file in the previous step
[root@node4 etc]# mkdir -p /usr/local/mysqlrouter/run/ [root@node4 etc]# mkdir -p /usr/local/lib/mysqlrouter [root@node4 etc]# mkdir -p /usr/local/mysqlrouter/etc/ [root@node4 etc]# mkdir -p /usr/local/mysqlrouter/run/ [root@node4 etc]# mkdir -p /usr/local/mysqlrouter/data/ [root@node4 etc]# mkdir -p /usr/local/mysqlrouter/log/
3.2.6 Start MySQL Router
[root@node4 etc]# mysqlrouter --config /usr/local/mysqlrouter/etc/mysqlrouter.conf &
3.2.7 Check whether the port listening of MySQL Router is turned on
[root@node4 etc]# netstat -ntlp |grep mysqlrouter tcp 0 0 172.16.8.68:7001 0.0.0.0:* LISTEN 9221/mysqlrouter tcp 0 0 172.16.8.68:7002 0.0.0.0:* LISTEN 9221/mysqlrouter
There is no stopping script for MySQL Router. If you want to stop, you need to kill the process with kill -9
4. Functional testing
The following tests are all in InnoDB Under Cluster, the installation mode of MySQL Router is bootstrap
4.1路由验证
4.1.1测试通过MySQL Router连接6446 RW端口,查看连接的是否为primary
[root@node4 mysqlrouter]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6446 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+
测试结果: 使用6446 RW端口会访问到后端InnoDB Cluster的primary
4.1.2测试通过MySQL Router连接6447 RO端口,查看连接的是否为seconde节点,rr算法是否生效
[root@node4 mysqlrouter]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6447 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+
测试结果: 使用6447 RO端口会连接到后端InnoDB Cluster集群中所有的seconde节点,并且以rr(round-robin)的策略调度
4.2高可用测试
4.2.1模拟其中一台seconde节点宕机,查看MySQL Router是否会自动取消该节点的路由
(1) 停掉一台seconde节点
[root@oratest52 ~]# systemctl status mysql [root@oratest52 ~]# systemctl stop mysql [root@oratest52 ~]# systemctl status mysql
(2) 在InnoDB Cluster中查看集群状态,可以看到oratest52已经处于MISSING状态
MySQL 172.16.9.51:3306 JS > cluster.status() { "clusterName": "st", "defaultReplicaSet": { "name": "default", "primary": "oratest51:3306", "ssl": "DISABLED", "status": "OK_NO_TOLERANCE", "statusText": "Cluster is NOT tolerant to any failures. 1 member is not active", "topology": { "oratest51:3306": { "address": "oratest61:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "oratest52:3306": { "address": "oratest52:3306", "mode": "n/a", "readReplicas": {}, "role": "HA", "shellConnectError": "MySQL Error 2003 (HY000): Can't connect to MySQL server on 'oratest52' (111)", "status": "(MISSING)" }, "test61:3306": { "address": "test51:3306", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "test61:3306" }
(3) 在MGR中查看GR状态,可以看到已经没有oratest52这台机器
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 48cdca42-a386-11e9-95f1-0050569d2587 | test61 | 3306 | ONLINE | | group_replication_applier | e9ee5267-a386-11e9-ac35-0050569d9ef5 | oratest51 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+
(4) 测试通过6447 RO端口连接,查看是否会自动剔除宕机的oratest52节点
[root@node4 ~]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6447 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+
测试结果: MySQL Router会自动剔除宕机的oratest52节点,read请求只会转发到后端正常的MySQL实例,过程无需人工干预。
4.2.2启动上一步中停止的MySQL实例,确认MGR和InnoDB Cluster一切正常后,查看MySQL Router是否会自动加入这台已恢复的实例进行转发
(1) 启动oratest52的MySQL服务
[root@oratest52 ~]# systemctl start mysql
(2)启动MGR,并确认MGR状态正常
mysql> start group_replication; Query OK, 0 rows affected (2.92 sec) mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 48cdca42-a386-11e9-95f1-0050569d2587 | test61 | 3306 | ONLINE | | group_replication_applier | 50bc1160-a386-11e9-92c9-0050569dc0da | oratest52 | 3306 | ONLINE | | group_replication_applier | e9ee5267-a386-11e9-ac35-0050569d9ef5 | oratest51 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+
(3) 检查InnoDB Cluster是否正常
MySQL 172.16.9.51:3306 JS > cluster.status() { "clusterName": "st", "defaultReplicaSet": { "name": "default", "primary": "oratest51:3306", "ssl": "DISABLED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "oratest51:3306": { "address": "oratest61:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "oratest52:3306": { "address": "oratest52:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "test61:3306": { "address": "test51:3306", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "test61:3306" }
(4) 查看恢复正常的实例,能不能自动加入MySQL Router进行转发,可以看到oratest52可以被正常调度
[root@node4 mysqlrouter]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6447 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+
测试结果: 恢复正常的MySQL实例可以自动加入MySQL Router并进行转发,无需人工干预。
4.2.3模拟停掉primary节点,测试MySQL Router如何处理发送RW端口的请求,MySQL Router怎样获取新的primary节点
(1) 当前primary节点是oratest51,在primary节点上停止MySQL,确认停止成功
[root@oratest51 data]# systemctl status mysql [root@oratest51 data]# systemctl stop mysql [root@oratest51 data]# systemctl status mysql [root@oratest51 data]# ps -ef |grep mysql
(2) 查看InnoDB Cluster状态,可以看到oratest51已经处于MISSING状态,primary节点已经切换到了test61
MySQL 172.16.9.61:3306 JS > cluster.status(); { "clusterName": "st", "defaultReplicaSet": { "name": "default", "primary": "test61:3306", "ssl": "DISABLED", "status": "OK_NO_TOLERANCE", "statusText": "Cluster is NOT tolerant to any failures. 1 member is not active", "topology": { "oratest51:3306": { "address": "oratest51:3306", "mode": "n/a", "readReplicas": {}, "role": "HA", "shellConnectError": "MySQL Error 2003 (HY000): Can't connect to MySQL server on 'oratest51' (111)", "status": "(MISSING)" }, "oratest52:3306": { "address": "oratest52:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "test61:3306": { "address": "test61:3306", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "test61:3306" }
(3) 测试连接到RW 6446端口,查看是否可以正常连接到新的primary节点test61,并且写入数据否正常
[root@node2 mysqlrouter]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6446 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+
测试结果: 当primary节点挂掉之后,MySQL Router会自动去连接新的primary节点
4.2.4启动上一步中停掉的oratest51,将状态恢复正常,测试MySQL Router能不能自动加入这台已恢复的节点并调度
(1) 启动oratest51,将状态恢复正常
[root@oratest51 ~]# systemctl start mysql [root@oratest51 ~]# systemctl status mysql [root@oratest51 ~]# mysql -uroot -p123456 mysql> start group_replication; mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID |MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+------------+-------------+--------------+ | group_replication_applier | 48cdca42-a386-11e9-95f1-0050569d2587 |test61 | 3306 | ONLINE | | group_replication_applier | 50bc1160-a386-11e9-92c9-0050569dc0da |oratest52 | 3306 | ONLINE | | group_replication_applier | e9ee5267-a386-11e9-ac35-0050569d9ef5 |oratest51 | 3306 | ONLINE | +---------------------------+--------------------------------------+------------+-------------+--------------+ 3 rows in set (0.00 sec) mysql> quit [root@oratest51 local]# mysqlsh MySQL JS > shell.connect('root@172.16.9.51:3306') MySQL 172.16.9.51:3306 JS > var cluster = dba.getCluster() MySQL 172.16.9.51:3306 JS > cluster.status(); { "clusterName": "st", "defaultReplicaSet": { "name": "default", "primary": "test61:3306", "ssl": "DISABLED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "oratest51:3306": { "address": "oratest51:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "oratest52:3306": { "address": "oratest52:3306", "mode": "R/O", "readReplicas": {}, "role": "HA", "status": "ONLINE" }, "test61:3306": { "address": "test61:3306", "mode": "R/W", "readReplicas": {}, "role": "HA", "status": "ONLINE" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "test61:3306" }
(2) 测试RO端口的Router调度
[root@node4 mysqlrouter]# for ((i=0;i<=5;i++));do mysql -h172.16.8.101 -uroot -p123456 -P6447 -e"select @@hostname;";done; mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest51 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | oratest52 | +------------+ mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | test61 | +------------+
测试结果: 恢复正常的实例加入集群后,会自动加入到MySQL Router并进行调度
五、运维相关
5.1数据库节点变更
MySQL Router在初始化配置的时候是连接到集群节点读取集群的元数据的。如果在集群中新增或减少节点,需要同步更新MySQL Router的配置,否则无法生效
# mysqlrouter --bootstrap 172.16.9.51:3306 --directory /data/mysqlrouter --user=root --conf-use-sockets --force
更新了MySQL Router的配置的配置,需要重启MySQL Router:
#systemctl restart mysqlrouter #systemctl status mysqlrouter
六、问题汇总
6.1初始化MySQL Router前,需要停止group_replication
6.2使用--bootstrap初始化MySQL Router报错Unable to connect to the metadata server
报错如下
[root@oratest51 data]# mysqlrouter --bootstrap root@172.16.9.51:3310 --directory /data/mysqlrouter --conf-use-sockets --user=root Please enter MySQL password for root: Error: Unable to connect to the metadata server: Error connecting to MySQL server at 172.16.9.51:3310: Can't connect to MySQL server on '172.16.9.51' (111) (2003)
原因: 需要配置连接用户的权限
解决办法: 给连接用户配置所有权限,如下:
mysql> grant all privileges on *.* to root@'%' identified by "123456"; mysql> flush privileges;
6.3使用--bootstrap初始化MySQL Router报错缺少mysql_innodb_cluster_metadata.schema_version
表
[root@oratest51 data]# mysqlrouter --bootstrap root@172.16.9.51:3306 --directory /data/mysqlrouter --conf-use-sockets --user=root Please enter MySQL password for root: Error: Expected MySQL Server '172.16.9.51:3306' to contain the metadata of MySQL InnoDB Cluster, but the schema does not exist. Checking version of the metadata schema failed with: Error executing MySQL query: Table 'mysql_innodb_cluster_metadata.schema_version' doesn't exist (1146) See https://dev.mysql.com/doc/refman/en/mysql-innodb-cluster-creating.html for instructions on setting up a MySQL Server to act as an InnoDB Cluster Metadata server
原因: 没有创建InnoDB Cluster集群,创建InnoDB Cluster集群后会自动生成这张表。mysql_innodb_cluster_metadata.schema_version表的用途是MySQL Router在进行调度分配的时候,需要读取这张表的内容来做调度策略。
解决办法: 创建InnoDB Cluster集群
6.4启动MySQL Router hang住不动,查看日志报错Error: bootstrap_server_addresses is not allowed when dynamic state file is used
具体如下:
[root@node2 log]# tailf /root/mysqlrouter/log/mysqlrouter.log 2019-08-02 15:37:52 routing INFO [7f9721e2d700] [routing:st_default_ro] started: listening using /root/mysqlrouter/mysqlro.sock 2019-08-02 15:37:52 routing INFO [7f972162c700] [routing:st_default_rw] started: listening using /root/mysqlrouter/mysql.sock 2019-08-02 15:37:52 routing INFO [7f9720e2b700] [routing:st_default_x_ro] started: listening using /root/mysqlrouter/mysqlxro.sock 2019-08-02 15:37:52 routing INFO [7f9713fff700] [routing:st_default_x_rw] started: listening on 0.0.0.0:64460, routing strategy = first-available 2019-08-02 15:37:52 routing INFO [7f9713fff700] [routing:st_default_x_rw] started: listening using /root/mysqlrouter/mysqlx.sock 2019-08-02 15:37:52 routing INFO [7f9713fff700] [routing:st_default_x_rw] stopped 2019-08-02 15:37:52 routing INFO [7f9721e2d700] [routing:st_default_ro] stopped 2019-08-02 15:37:52 routing INFO [7f972162c700] [routing:st_default_rw] stopped 2019-08-02 15:37:52 routing INFO [7f9720e2b700] [routing:st_default_x_ro] stopped 2019-08-02 15:37:52 main ERROR [7f9726f1b880] Error: bootstrap_server_addresses is not allowed when dynamic state file is used
原因: 开启了dynamic_state
解决办法: 在配置文件中注释掉dynamic_state所在行,例如下列第11行
1 # File automatically generated during MySQL Router bootstrap 2 [DEFAULT] 3 user=root 4 logging_folder=/root/mysqlrouter/log 5 runtime_folder=/root/mysqlrouter/run 6 data_folder=/root/mysqlrouter/data 7 keyring_path=/root/mysqlrouter/data/keyring 8 master_key_path=/root/mysqlrouter/mysqlrouter.key 9 connect_timeout=15 10 read_timeout=30 11 #dynamic_state=/root/mysqlrouter/data/state.json
6.5启动MySQL Router hang住不动,查看日志报错Unable to fetch live group_replication member data from any server in replicaset 'default'
具体如下:
[root@node2 log]# tailf /root/mysqlrouter/log/mysqlrouter.log 2019-08-02 15:46:41 metadata_cache WARNING [7f3030405700] While updating metadata, could not establish a connection to replicaset 'default' through test61:3306 2019-08-02 15:46:51 metadata_cache WARNING [7f3030405700] While updating metadata, could not establish a connection to replicaset 'default' through oratest52:3306 2019-08-02 15:47:01 metadata_cache WARNING [7f3030405700] While updating metadata, could not establish a connection to replicaset 'default' through oratest51:3306 2019-08-02 15:47:01 metadata_cache ERROR [7f3030405700] Unable to fetch live group_replication member data from any server in replicaset 'default'
原因: MySQL Router所在节点没有配置本机DNS解析
解决办法: 在/etc/hosts中添加MGR实例的解析
七、个人总结
- MySQL Router有两种部署模式:
- bootstrap模式:支持failover,必须结合InnoDB Cluster使用,在
--directory
指定的路径下自动生成安装目录,配置文件里的端口为6446和6447。 - 简单模式:不支持failover,无需结合InnoDB Cluster使用,一般在主从复制或者主主复制等模式下使用,使用
/usr/local/mysqlrouter/share/doc/mysqlrouter/sample_mysqlrouter.conf
这个配置文件部署。
- bootstrap模式:支持failover,必须结合InnoDB Cluster使用,在
- MySQL Router使用的是一个IP地址(可以理解为VIP)加一个RW端口和一个RO端口实现读写分离,Router自身不能判断应用程序的请求是读还是写,因此做读写分离时,必须指定两个端口,如果应用程序不便指定两个端口,也可以全部指定RW端口,这样的话从节点就只作高可用,不做读写分离。
- MySQL Router自身存在单点故障隐患,官方推荐在每个应用程序所在机器上部署Router,本机器连接本机器的Router。也可以在Router的上层在搭建一个高可用服务,如果是自建机房可以选择Keepalived、pacemaker等方案;
- 通过bootstrap生成的配置文件,只需要在
bootstrap_server_addresses
这一栏里配置就可以了,格式如下:bootstrap_server_addresses=mysql://oratest51:3306,mysql://oratest52:3306,mysql://test61:3306
Copy after login - MySQL Router的后端MySQL实例挂掉之后,无需人工干预,MySQL Router会自动剔除挂了的MySQL实例,当挂了的MySQL实例恢复后会自动加入MySQL Router的后端服务器,也无需人工干预。
- MGR架构可以实现高可用,但是要实现failover,则需要安装InnoDB Cluster了。MySQL Shell和MySQL Router是InnoDB Cluster集群的一部分
- MySQL Router非常轻量级,性能损耗小于1%,官方建议每台应用上部署一个mysqlrouter节点,优点是节省网络带宽。缺点是mysqlrouter太轻量级了,只能提供简单的基于端口的读写路由(Port based routing)和基于权重轮询的负载均衡(Weighted Balance),不支持基于SQL的读写分离(Query based routing)和空闲优先的负载均衡(Application Layer balancer)
【相关推荐:mysql视频教程】
The above is the detailed content of what is mysql router. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.
