ProxySQL Setup: Streamlining MySQL Read-Write Operations
At Mydbops we always thrive to provide the best MySQL Solutions. We are exploring the modern SQL load balancers. We have planned to write a series of blog on ProxySQL.
The first blog in this series is how to set up ProxySQL for MySQL Replication Topology including Read / Write Split and some background over ProxySQL.
What is ProxySQL ?
- ProxySQL is a open-source high-performance SQL aware proxy. It runs as a daemon watched by a monitoring process.
- ProxySQL seats between application and db servers.
- The daemon accepts incoming traffic from MySQL clients and forwards it to backend MySQL servers.
A few most commonly used features are :
- Read/write split
- On-the-fly queries rewrite
- Query caching
- Real time statistics on web console
- Seamless replication switchover
- Query mirroring
- Monitoring
So the main advantages of using ProxySQL is, it is designed to run continuously without needing to be restarted. Most configuration can be done at runtime using queries similar to SQL statements and it is more light weight.
Let us explore the basic Query Routing (Read/Write split) for the effective load sharing. We have set up 4 nodes to make the architecture .
- node1 (172.17.0.1) , Master
- node2 (172.17.0.2) , Slave
- node3 (172.17.0.3) , Slave
- node4 (172.17.0.4) , ProxySQL
Note: By default, ProxySQL binds with two Ports 6032 and 6033.
6032 is admin port and 6033 is the one which accepts incoming connections from clients.
MySQL Replication setup :
Configuring MySQL’s master-slave replication is outside the scope of this tutorial, we already have nodes with replication running.
Before entering to admin interface of ProxySQL , create one application user with all privileges required to your application and one monitoring user at every MySQL DB server.
ProxySQL Setup :
Install and start ProxySQL :
For Installation kindly refer : https://github.com/sysown/proxysql/wiki
$ service proxysql start
Starting ProxySQL: Main init phase0 completed in 0.000491 secs.
Main init global variables completed in 0.000675 secs.
Main daemonize phase1 completed in 0.00015 secs.
DONE!
Now connect to ProxySQL admin interface to start with configuration :
Configure Backends :
ProxySQL uses the concept of hostgroup. A hostgroup is a group of host with logical functionalities.
In this setup , we have used just need 2 hostgroups:
hostgroup 0 for the master [Used for Write queries ]
hostgroup 1 for the slaves [Used for Read Queries ]
Apart from this we can also have one analytical server as slave of same master and we can assign new hostgroup id for the server and redirect all analytical related queries (long running) at this host.
Note: When we load MYSQL SERVERS , Our writer host also get configured in reader hostgroup automatically by ProxySQL to handle all those queries which are redirected to reader hostgroup in case no slaves are online.
So bonus point here is we can decrease the weightage assigned to master servers inside mysql_server table for reader hostgroup , so that our most of read queries will go on server which has higher weight.
Configure User :
monitor
user will continuously check the status of backend in specified interval.sysbench
is user created for the application.
Configure monitoring :
ProxySQL constantly monitors the servers it has configured. To do so, it is important to configure some interval and timeout variables ( in milliseconds ).
Monitor module regularly check replication lag (using seconds_behind_master) if a server has max_replication_lag set to a non-zero value.
With below configuration, servers will only be shunned in case replication delay exceeds 60 seconds ( 1 min) behind master
If you want to be more accurate while calculating slave lag, kindly refer: http://proxysql.com/blog/proxysql-and-ptheartbeat
There are also other important things in monitoring module which we can configure as per our need. I will prefer writing separate blog in this series.
Configure Query Rules :
To send all SELECT
queries on slave ( based on Regex ).
When the Query Processor scans the query rules trying to find a match with no success and it reaches the end, it will apply the default_hostgroup for the specific user according to mysql_users entry.In our case, user sysbench
has a default_hostgroup=0
, therefore any query not matching the above rules [Eg ALL WRITES ] will be sent to hostgroup 0 [Master].Below stats tables are used to validate if your query rules getting used by incoming traffic.
We can also redirect some specific pattern queries by using digest
in stats_mysql_query_digest
Validate the DB Connection :
Application will connect to 6033
port on host 172.17.0.4
of ProxySQL to send DB traffic.
Check Backend Status :
It shows ProxySQL is able to successfully connect to all backends.
I have executed some sysbench test on cluster to check query distributions .Below table of ProxySQL shows number of queries executed per host.
If any of your server goes unreachable from any hostgroup , status gets changed from ONLINE
to SHUNNED
.
It means ProxySQL wont send any queries to that host until it comes back to ONLINE
.
We can also take any server offline for maintenance. To disable a backend server it is required to change its status to OFFLINE_SOFT
(Gracefully disabling a backend server) or OFFLINE_HARD
(Immediately disabling a backend server.)
In this case no new traffic will be send to the node.
Okay you still worried about reading stale data from slave ?
Then do not worry , ProxySQL is coming up with new feature to make sure your application get latest updated data. Currently this feature is available only with row based replication with GTID enabled. We have also blog on proxysql for Xtradb cluster
For more detailed description on every module: https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration