§ Building a Read/Write Splitting Cluster
This article describes how to deploy MySQL Router in front of the MGR cluster to achieve read/write splitting, read load balancing, and automatic failover.
MySQL Router is a lightweight middleware that uses a multi-port solution to implement read/write splitting and read load balancing, and it supports both the MySQL and MySQL X protocols.
It is recommended to deploy MySQL Router on the application servers, with one deployment per application server. In this way, the application can directly connect to the local IP, which is more efficient, and when the backend database changes, the application does not need to modify the IP configuration.
§ Deploying MySQL Router
MySQL Router must be initialized on first startup.
Parameter explanation:
--bootstrap means to start the initialization
GreatSQL@172.16.16.10:3306 is the dedicated account for the MGR service
--user=mysqlrouter is the system username that runs the mysqlrouter process
$ mysqlrouter --bootstrap GreatSQL@172.16.16.10:3306 --user=mysqlrouter
Please enter MySQL password for GreatSQL: <-- Enter the password
# Then mysqlrouter starts the initialization automatically
# It automatically reads the MGR metadata information and automatically generates the configuration file
Please enter MySQL password for GreatSQL:
# Bootstrapping system MySQL Router instance...
- Creating account(s) (only those that are needed, if any)
- Using existing certificates from the '/var/lib/mysqlrouter' directory
- 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 /etc/mysqlrouter/mysqlrouter.conf
# MySQL Router configured for the InnoDB Cluster 'MGR1'
After this MySQL Router has been started with the generated configuration
$ /etc/init.d/mysqlrouter restart
or
$ systemctl start mysqlrouter
or
$ mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf
the cluster 'MGR1' can be reached by connecting to:
## MySQL Classic protocol <-- The two ports of the MySQL protocol
- Read/Write Connections: localhost:6446
- Read/Only Connections: localhost:6447
## MySQL X protocol <-- The two ports of the MySQL X protocol
- Read/Write Connections: localhost:6448
- Read/Only Connections: localhost:6449
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
29
30
31
32
33
34
35
36
37
If you want to customize the name and directory, you can also specify the --name and --directory options during initialization, which allows deploying multiple Router instances on the same server. Refer to this article: Can MySQL Router run multiple instances in the same system environment (opens new window).
§ Starting the mysqlrouter Service
This completes the initialization. According to the prompts above, directly start the mysqlrouter service:
$ systemctl start mysqlrouter
$ ps -ef | grep -v grep | grep mysqlrouter
mysqlro+ 6026 1 5 09:28 ? 00:00:00 /usr/bin/mysqlrouter
$ netstat -lntp | grep mysqlrouter
tcp 0 0 0.0.0.0:6446 0.0.0.0:* LISTEN 6026/mysqlrouter
tcp 0 0 0.0.0.0:6447 0.0.0.0:* LISTEN 6026/mysqlrouter
tcp 0 0 0.0.0.0:6448 0.0.0.0:* LISTEN 6026/mysqlrouter
tcp 0 0 0.0.0.0:6449 0.0.0.0:* LISTEN 6026/mysqlrouter
tcp 0 0 0.0.0.0:8443 0.0.0.0:* LISTEN 6026/mysqlrouter
2
3
4
5
6
7
8
9
10
11
It can be seen that the mysqlrouter service has started normally.
The configuration file automatically generated by mysqlrouter during initialization is /etc/mysqlrouter/mysqlrouter.conf, which mainly contains the configuration for the R/W and RO ports, for example:
[routing:greatsqlMGR_rw]
bind_address=0.0.0.0
bind_port=6446
destinations=metadata-cache://greatsqlMGR/?role=PRIMARY
routing_strategy=first-available
protocol=classic
2
3
4
5
6
You can modify the bound IP address and port as needed, or specify the --conf-base-port option during initialization to customize the initial port numbers.
§ Verifying the Read/Write Splitting Effect
Now, connect to the 6446 (read/write) port with a client and confirm that the connection is to the PRIMARY node:
-- Connect to GreatSQL
-- mysql -h172.16.16.10 -u GreatSQL -p -P6446
-- Remember the following MEMBER_IDs
greatsql> SELECT MEMBER_ID,MEMBER_ROLE FROM performance_schema.replication_group_members;
+--------------------------------------+-------------+
| MEMBER_ID | MEMBER_ROLE |
+--------------------------------------+-------------+
| 4ebd3504-11d9-11ec-8f92-70b5e873a570 | PRIMARY |
| 549b92bf-11d9-11ec-88e1-70b5e873a570 | SECONDARY |
| 5596116c-11d9-11ec-8624-70b5e873a570 | SECONDARY |
+--------------------------------------+-------------+
greatsql> SELECT @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 4ebd3504-11d9-11ec-8f92-70b5e873a570 |
+--------------------------------------+
# It is indeed connected to the PRIMARY node
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Similarly, connect to the 6447 (read-only) port and confirm that the connection is to a SECONDARY node:
-- Connect to GreatSQL
-- mysql -h172.16.16.10 -u GreatSQL -p -P6447
greatsql> SELECT @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 549b92bf-11d9-11ec-88e1-70b5e873a570 |
+--------------------------------------+
# It is indeed connected to a SECONDARY node
2
3
4
5
6
7
8
9
10
§ Verifying the Read Load Balancing Effect
The default strategy for MySQL Router to connect to the read/write node (Primary node) is first-available, which means it only connects to the first available node. The default strategy for Router to connect to read-only nodes (Secondary nodes) is round-robin-with-fallback, which polls among the read-only nodes.
Keep the existing connection to port 6447 open, create new connections to port 6447, and check the server_uuid. You should find that the value of another read-only node is read, because the read load balancing mechanism of mysqlrouter automatically polls among several read-only nodes. Under the default round-robin-with-fallback strategy, read requests are only sent to the PRIMARY node when all read-only nodes are unavailable.
For Router's connection strategies, refer to 24. Can MySQL Router be configured to poll among MGR primary and secondary nodes (opens new window) in the FAQ document, or the MySQL Router official document: routing_strategy parameter/option (opens new window).
§ Verifying the Automatic Failover Feature
Next, simulate the PRIMARY node going down or being switched over, and verify that mysqlrouter can also implement automatic failover.
Log in to any node of the MGR cluster:
$ mysqlsh --uri GreatSQL@172.16.16.10:3306
...
MySQL 172.16.16.10:3306 ssl JS > var c=dba.getCluster();
MySQL 172.16.16.10:3306 ssl JS > c.setPrimaryInstance('172.16.16.11:3306'); <-- Switch the PRIMARY node
Setting instance '172.16.16.11:3306' as the primary instance of cluster 'MGR1'...
Instance '172.16.16.10:3306' was switched from PRIMARY to SECONDARY. <-- Switched, from PRIMARY to SECONDARY
Instance '172.16.16.11:3306' was switched from SECONDARY to PRIMARY. <-- Switched, from SECONDARY to PRIMARY
Instance '172.16.16.12:3306' remains SECONDARY. <-- Remains unchanged
WARNING: The cluster internal session is not the primary member anymore. For cluster management operations please obtain a fresh cluster handle using dba.getCluster().
The instance '172.16.16.11:3306' was successfully elected as primary.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Go back to the session that was connected to port 6446 earlier and query the server_uuid again. At this point, you will find that the connection has been automatically disconnected:
greatsql> SELECT @@server_uuid;
ERROR 2013 (HY000): Lost connection to MySQL server during query
greatsql> SELECT @@server_uuid;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 157990
Current database: *** NONE ***
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 549b92bf-11d9-11ec-88e1-70b5e873a570 | <-- Confirm that the server_uuid has changed to the new one
+--------------------------------------+
2
3
4
5
6
7
8
9
10
11
12
13
14
This implements automatic failover.
At this point, an MGR cluster that supports read/write splitting, read load balancing, and automatic failover has been deployed with MySQL Router in combination with GreatSQL.
Scan the QR code to follow our WeChat official account
