Table of Contents
The MySQL Router is a building block for high availability (HA) solutions. Router simplifies high available application development by intelligently routing connections to MySQL servers for increased performance, robustness, and up time.
MySQL Router also connects seamlessly with MySQL Fabric permitting Fabric to store and manage the high availability groups for routing, making it simpler to manage groups of MySQL Servers for redundancy and continued operation.
Thus, you can use the router to build more robust applications with connection routing provided by the router. You can also build your solutions to be Fabric aware and allow Fabric to maintain the list of servers available for connection routing.
The router is a single executable that runs on the same platform as the application. While it can be run in the background, you can turn on logging features and view the logs console (or log to a file), which may be helpful for application development and tuning.
Like the MySQL server, Router uses a configuration file with information for how to perform routing. The configuration file can contain several sections to permit several applications to use a single router instance. We will see more about the configuration file in later sections.
The next section explains the main features of MySQL Router in more detail.
The MySQL Router 2.0.3 has three major features; simple connection routing, Fabric integration connection routing using the Fabric cache plugin, and support for plugins via the MySQL Harness.
While Router does not support SSL connections, it is possible to secure network traffic using 3rd party tools that provide SSL tunneling services, such as stunnel.
Connection routing (also called standalone routing to distinguish it from Fabric integrated connection routing - see below) is the ability to permit the redirection of connections sent to the router to an available MySQL server. Connection routing is simply the redirection of the MySQL packets in their entirety without inspection.
This means you can setup your application to connect to the router and should the current MySQL server fail, retry the connection and the router will select a new MySQL server to redirect the connection.
We call this simple redirect connection routing because it requires the application to retry the connection. That is, if a connection from MySQL Router to the MySQL server is interrupted (such as, the server goes offline), the application will encounter a connection failure. However, a new connection attempt will trigger the router to find another server to connect.
We specify the servers we want to use in a routing strategy in
the configuration file. For example, the following section tells
the router to listen for connections on port 7002 of the
localhost, and then redirect those connections to any of the
servers in the list named by the destinations
option, including servers running on the localhost listening on
ports 3306, 3307, and 3308. Finally, we use the
mode option to tell the router to allow both
readers and writers. For more information about the available
modes, see the section entitled,
Configuration
File Setup below.
[routing:simple_redirect]
bind_port = 7002
mode = read-write
destinations = localhost:3306,localhost:3307,localhost:3308
Notice that the section is entitled,
routing:simple_redirect. The first part,
routing is called the section name and is
used internally to determine which plugin to load. The last part
is an option section key (name) you can optionally provide
should you want to set up more than one routing strategy.
When a server is no longer reachable, the router moves to the next one in the list. When the list is exhausted, the router halts redirecting.
Using MySQL Fabric with the router is another form of connection routing (also called Fabric integration) but instead of configuring the destinations manually, the list is provided by MySQL Fabric. Recall Fabric allows you to manage a farm of MySQL servers by grouping the servers in high availability groups for redundancy (allowing for automatic failover) and for easier management. Thus, Fabric maintains a list of the servers in the farm including their roles and how each is configure. Fabric can supply this list to the router via the Fabric cache.
With the router, we can use Fabric to supply the routes for connections to an appropriated MySQL Server based on information provided in Fabric via the Fabric cache. Essentially, we connect the router to Fabric and get the high availability feature for free.
Fortunately, this is not complicated to set up. All you need is another routing strategy like we saw with simple redirect connection routing. In this case, we set up a new section in the configuration file as demonstrated below.
We set up the router to listen on port 19906
on the localhost with a mode set to
read-only, which is typical in an application
written to use Fabric (or replication in general). We need two
sections in the configuration file like so:
[fabric_cache:webfarm] address = fabric.example.com user = fabricuser [routing:webfarm] bind_port = 19906 destinations = fabric+cache://webfarm/group/homepage/ mode = read-only
The first section sets the address and user for the Fabric connection. The second sets the routing strategy.
Notice here we set the destinations option to
point to the Fabric cache via a URI. When you set up a Fabric
integration like this, you will be prompted for the Fabric
password when you start the router.
We used the section name "fabric_cache" to
indicate this is a fabric integrated connection routing
strategy. Finally, notice we provide the Fabric user account
using the user option.
One of the interesting features of MySQL Router is not something the average user will see - it is under the hood. More specifically, the router is designed to permit the use of plugins to enable features. The plugin technology is called MySQL Harness.
MySQL Harness provides a number of advantages for the router including the ability to load new functionality without having to alter the original mysqlrouter application, which means faster development times for new features and the possibility of selectively loading features. The harness also provides logging, which enables developers to turn on a log of events without having to write any specialized logging code.
The features provided my MySQL Harness include the following:
Dependency tracking for plugins
Version handling for plugins
Loading, starting, and stopping plugins
Permits creation of third-party plugins
Provides a configuration file library for reading configuration files
Provides platform agnostic file support
Provides a robust logging facility
While the harness feature set and usage may not be obvious initially, the long-term benefits of developing with a plugin-based design are well documented.
The architecture of the MySQL Router is a single application built as a host incorporating the MySQL Harness plugin feature. Major features for MySQL Router are loaded as plugins. For example, both the connection routing and Fabric cache components are plugins. The following figure illustrates how the router is built.
Notice the application, mysqlrouter, is based
on the harness. Plugins are therefore enabled within the
application.
Notice also the arrows pointing to boxes outside of the router application. On the left we see the application that is connected to the router. On the right, we see two connection destinations. On the top is Fabric cache and below that a representation of a set of MySQL servers in a replication topology.
Now, let us follow the arrows as they pass through the router.
Start with the connection routing plugin. This is simple redirection. Thus, when the application connects to the router, the router reads the destinations from the configuration file and redirects to one of the servers in the list.
Now let us see how the Fabric cache plugin is used. To use the Fabric cache, the routing strategy specifies the URL for the Fabric installation. In this case, the application would connect to the router, then the router would get the destination list from Fabric, and then redirect the connection to the one of the servers in the list.
As you can see, the router is designed to be modular and take advantage of such architectures when implementing features. Thus, future features of the router will appear along with the two plugins shown in this drawing.
For more information about the Fabric Cache and Connection Routing plugins, see the section entitled, Plugins.
Now that we understand what the router does and how it is configured at a high level, let us discuss how best to use the router in your environment.
In its current form, the MySQL Router is best used alongside the application. That is, you should install the router on the same machines that hosts the application. While this is not a rule or requirement, it is the recommended practice.
Since the router is on the same machine as the application, this
means you can write your application to monitor the
mysqlrouter executable and restart it should
you need to. For example, if the list of servers in the
destinations option is exhausted, you can
restart the router with a new destinations list or restart the
router to retry the servers in the list.
The following graphic represents how you could employ the router with your application:
You can run several instances of the router throughout your network. You do not need to isolate the router to a single machine or even a single instance. This is because there is nothing in the router to give it affinity for any one server or machine. It is essentially agnostic in that manner.
Developing applications for use with the router does not require any special application library or interface. Aside from managing the MySQL Router instance, your application can be written as if the router were not there.
The only difference you may want to do is how you use connections to the MySQL server. If your application uses a single connection made at startup and does not test for connection errors, you may need to change that behavior.
This is because the router merely redirects connections when the connection is attempted. The router does not read packets or perform any form of analysis. Thus, if a server goes down, the router will return a connection error to the host application.
Thus, the application should be written to test for connection errors and, if encountered, retry the connection. If this technique or one similar is employed in your application, using MySQL Router will be seamless.
The following gives you a better idea of why you may want to use the router, and a look into how it is used from an application.
There are several key user stories for MySQL Router. These include the following.
As a developer, I want my application connect to a service so it gets a connection to, by default, the current Master (primary) of a highly available MySQL Fabric group.
As an administrator, I want to set up multiple services so MySQL Router listens on a different port for each highly available Fabric group.
As an administrator, I want to be able to run a connection routing service on port 3306 so it is more transparent to a user/application.
As an administrator, I want to configure a mode for each connection routing service so I can specify whether a Master (primary) or Slave (secondary) is returned.
The workflow for using the router is as follows.
MySQL Client/Connector connects to MySQL Router on, for example, port 8500.
Router checks which MySQL Server is available.
Router bridges the connection from client to the MySQL server.
Applications can connect to the router on the machine and port specified in the router's configuration file. For example, port 8500. The following shows an example in Python using the MySQL Connector/Python connector library.
cnx = mysql.connector.connect(host='router.example.com', port=8500, user='scott', password='tiger')
cur = cnx.cursor()
cnx.execute("SELECT ...")
The following lists recommendations for using MySQL Router.
Install and run the router on the same host as the application.
Bind the router to the localhost using bind_port
= 127.0.0.1:<port> in the configuration
file.
MySQL Router works best with MySQL Fabric, but can be used without Fabric.
Now that we know what the MySQL Router is and how it is used, let us dive deeper into installing and configuring the router.