Load balance read replicas using ProxySQL in Azure Database for MySQL
Published Sep 30 2019 09:56 AM 34.1K Views
Microsoft

Overview

ProxySQLhigh-performance MySQL proxy, enables users to distribute different queries to multiple servers to distribute the load more efficiently.

 

ProxySQL has several benefitsincluding intelligent load balancing across different databases and the ability to determine if a database instance is running so that read traffic can be redirected accordingly. 

 

This blog post shows how to set up ProxySQL as a load balancer to split the read and write workloads to Azure Database for MySQL. 

 

Note: ProxySQL is an open source community tool. It is supported by Microsoft on a best effort basis. In order to get production support with authoritative guidance, you can evaluate and reach out to ProxySQL Product support.

 

ProxySQL on Azure Database for MySQL setup 

The below figure shows the basic setup for ProxySQL with Azure Database for MySQL as a master and read replica server. 

Capture.PNG

Figure 1: ProxySQL with Azure Database for MySQL 

 

You can also set up ProxySQL using Azure Resource Manager. To deploy above setup, please refer to https://github.com/Azure/azure-mysql/tree/master/arm-templates/ExampleWithProxySQL

 

Prerequisites

To prepare for setting up ProxySQL, you need: 

 

 

ImportantEnforcement of SSL connections is enabled by default on Azure Database for MySQL. We recommend avoiding disabling the SSL option and configure ProxySQL to use SSL as outlined below. 

 

Procedure 

The process for setting up ProxySQL as a load balancer in Azure Database for MySQL involves: 

 

  1. Installing ProxySQL on Ubuntu VM.
  2. Setting up ProxySQL. 
  3. Creating MySQL users on master server.
  4. Creating the ProxySQL user. 
  5. Configuring monitoring on ProxySQL. 
  6. Configuring the routing rules for read and write split. 
  7. Saving the changes to the ProxySQL configuration to persist across restarts. 

 

Install ProxySQL on Ubuntu VM

 

ImportantAzure Database for MySQL supports ProxySQL version2.0.6 and later. 

 

  1. Download the latest version of ProxySQL into the /tmp directory. ProxySQL packages are on ProxySQL GitHub release page.
    cd /tmp 
    curl -OL  https://github.com/sysown/proxysql/releases/download/v2.0.6/proxysql_2.0.6-ubuntu18_amd64.deb
     
  2. Install ProxySQL by using dpkg and update the package repository to ensure that you have the latest version build.
    sudo dpkg -i proxysql_* 
    sudo apt-get update
     
  3. Install mysql-client. 
    sudo apt-get install mysql-client​
     
  4. After installing ProxySQLstart the ProxySQL service, as it does not start automatically
    sudo systemctl start proxysql
     
  5. Check the status of service.
    systemctl status proxysql 
     After the ProxySQL service successfully startsmessage similar to the following appears: 

Capture1.PNG

 

Setting up ProxySQL 

On the ProxySQL server, perform the following steps:

 

  1. Connect to the ProxySQL administration interface with the default password ‘admin’.
    mysql –u admin –padmin -h 127.0.0.1 -P 6032 
     
  2. Add the reader and writer nodes ProxySQL server pool

    Important: ProxySQL currently does not support Private link FQDN privatelink.mysql.database.azure.com, which is a known limitation.

    insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'mydemomasterserver.mysql.database.azure.com',3306,1,'Write Group');
    insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'mydemoreplicaserver.mysql.database.azure.com',3306,1,'Read Group'); 
     
  3. Enable SSL support in ProxySQL server pool
    UPDATE mysql_servers SET use_ssl=1 WHERE hostgroup_id=1; 
    UPDATE mysql_servers SET use_ssl=1 WHERE hostgroup_id=2; 
     

Creating the MySQL users on master server

In ProxySQL, the user connects to ProxySQL and in turn ProxySQL passes the connection to the MySQL node. To allow ProxySQL to access to the MySQL database, we need to create a user on MySQL database with the same credentials as on the ProxySQL server. 

 

  1. Create a new user ‘mydemouser’ with the password ‘secretpassword
    CREATE USER 'mydemouser'@'%' IDENTIFIED BY 'secretpassword'; 
     
  2. Grant ‘mydemouser’ privileges to fully access the MySQL server.
    GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'mydemouser'@'%' WITH GRANT OPTION
     
  3. Apply the changes to the permissions
    FLUSH PRIVILEGES; 
      

Creating the ProxySQL user 

Allow the ‘mydemouser’ user to connect to ProxySQL server. 

 

On the ProxySQL server, run the following command:

 

 

insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('mydemouser','secretpassword',1,1);

 

 

Configure monitoring on ProxySQL

Create the monitoring user on the master server and then configure ProxySQL to monitor the nodes.

 

Create monitoring user on the master server:

 

  1. Log into the master server. 

    Note: The login syntax varies depending on whether the master server is running:
       - Single Server: Use mysql -uusername@hostname -hhostname -p
       
    - Flexible Server: Use mysql -uusername -hhostname -p

  2. Create a new user ‘monitoruser with the password ‘secretpassword
    CREATE USER 'monitoruser'@'%' IDENTIFIED BY 'secretpassword'; 
     
  3. Grant ‘monitoruser’ privileges to fully access the MySQL server
    GRANT SELECT ON *.* TO ' monitoruser'@'%' WITH GRANT OPTION; 
     
  4. Apply the changes to the permissions 
    FLUSH PRIVILEGES; 
     
  5. On the server running ProxySQL, configure mysql-monitor to the username of the new account. 
    set mysql-monitor_username='monitoruser'; 
    set mysql-monitor_password='secretpassword'; 
     

Configure the routing rules for read and write split 

  1. On the ProxySQL Server, configure the write traffic to route to the master server
    insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1); 
     
  2. On the ProxySQL Server, configure the read traffic to route to the read replica server
    insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1); 
     

Save the changes made to the ProxySQL configuration to persists across restarts

In ProxySQL configuration system, the changes we made are in memory and to make them persist across the restarts, you must copy settings to runtime and save them to disk. 

 

  1. On the server running ProxySQLexecute the below commands to save the settings to runtime:
    load mysql users to runtime; 
    load mysql servers to runtime; 
    load mysql query rules to runtime; 
    load mysql variables to runtime; 
    load admin variables to runtime; 
     
  2. On the server running ProxySQLexecute the following commands to save the settings to disk:
    save mysql users to disk; 
    save mysql servers to disk; 
    save mysql query rules to disk; 
    save mysql variables to disk; 
    save admin variables to disk; 
     

After successfully completing the above stepsProxySQL is configured and is ready to split the read and write workload. To test the functionality to determine if the read and write splits are being forwarded properly: 

 

  1. Log in to the server running ProxySQL with the ProxySQL user you created.
    mysql –u mydemouser –psecretpassword -h127.0.0.1 -P6033 
     
  2. Run the read and write queries
    SELECT *  
    FROM mydemotable;
    UPDATE mydemotable 
    SET mydemocolumn=value 
    WHERE condition; 

To verify that ProxySQL has routed the above read and write correctly: 

 

  1. Connect to the ProxySQL administration interface with the default password ‘admin’.
    mysql –u admin –padmin -h127.0.0.1 -P6032 
     
  2. Execute the following query:
    SELECT * FROM stats_mysql_query_digest; 
     

If you have trouble setting up ProxySQL on Azure Database for MySQL, please contact the Azure Database for MySQL team at AskAzureDBforMySQL@service.microsoft.com 

 

Thank you! 

Amol Bhatnagar 

Program Manager - Microsoft

10 Comments
Copper Contributor

Hi @ambhatna,

 

When you connect to Azure DB for MySQL do you need to embed the database name in the username or is it not necessary (eg mydemouser@mydemomasterserver)? This limited my use of Azure DB for MySQL with ProxySQL.

 

Thank you.

Microsoft

Hi @wchiquito,

Yes, as of now we do require the server name with the username. Please read  Why is username@servername required to connect to Azure Database for MySQL?  for more details.

Said that, we have made a fix in ProxySQL to take care of this issue. ProxySQL version2.0.6 and later can be used with Azure Database for MySQL. 

 

Please let me know if you are facing issues while setting up ProxySQL.

 

Copper Contributor

Hi @ambhatna,

 

Thanks for the quick reply.

 

To better understand it, does Azure Database for MySQL now understand the new connection attribute _server_host sent from ProxySQL?, see Let ProxySQL support Azure MySQL.

 

Thank you.

Microsoft

Hi @wchiquito,

 

ProxySQL version2.0.6 and later can be used with Azure Database for MySQL. I have discussed with the submitter of the PR: Let ProxySQL support Azure MySQL. and told him that this is already a supported scenario.

 

Please follow the below blogs as per your requirements for setting up the ProxySQL:

Copper Contributor

Hi @ambhatna,

 

I am already using ProxySQL 2.0.6 or later and just what I want is to understand what were the changes ProxySQL 2.0.6 or later and in Azure Database for MySQL because when I connect via MySQL CLI I have to use it

 

mysql -hmydemomaster.mysql.database.azure.com -umydemo@mydemomaster -p​

but in ProxySQL 2.0.6 or later I just insert:

 

insert into mysql_users(username,password)values('mydemouser','secretpassword');

 Thank you.

Microsoft

@wchiquito,

The changes we did was on the ProxySQL side (https://github.com/sysown/proxysql/pull/2185) . If you want to connect to Azure Database for MySQL using CLI then Username@Servername format is still required. 

Copper Contributor

Hi Guys, when i am trying to execute following command, 

Capture.PNG

 

i am getting below screens and cannot move forward to execute next commands (inserting replica & master information). 

Capture2.PNG

Capture3.PNG

Capture4.PNG

Copper Contributor

Hi, @ambhatna  can you please help me with above post in order to proceed further. thank you

Copper Contributor

As per the architecture diagram, there is a single point of failure for Azure VM.
Can we avoid this with help of Load Balancer and VM Scale Sets?

Copper Contributor

Hello @ambhatna ,

 we are having our wordpress application hosted on azure app servie and we are using Azure mysql read replicas to handle heavy workloads. we are trying to distribute our read and write workloads using a proxysql server implemented in a vm now my question is how can I connect my appservice to vm....or is it just giving the master string connection with above username to appservice is enough for my proysql to route traffic for read and write reqest.pls help me with this. Thanks in advance

Co-Authors
Version history
Last update:
‎Oct 04 2023 11:20 AM
Updated by: