ProxySQL: Setup and Use Case for High Availability & Load Balancing

Table of Contents
Why ProxySQL?
Benefits
Common Use Cases
ProxySQL Setup Guide
Instance Creation
Installing ProxySQL
Configuring MySQL Servers in ProxySQL
User Setup
Query Routing Rules
Monitoring & Logging
Scenario: Using ProxySQL in Production
Challenges & How ProxySQL Helps
Implementation Steps
[Final Verification & Conclusion
](https://chatgpt.com/?temporary-chat=true#5-final-verification--conclusion)
1. Introduction
ProxySQL is a high-performance MySQL proxy designed to enhance efficiency, load balancing, high availability, and query routing.
2. Why ProxySQL?
2.1 Benefits of ProxySQL
Read-Write Splitting: Directs write queries to the master and read queries to replicas, optimizing database performance.
High Availability: Supports failover mechanisms to ensure continuous database access during master or replica downtime.
Connection Pooling: Efficiently manages and reuses database connections, reducing database overhead.
Query Caching: Caches frequently execute queries to improve response times and reduce database load.
Query Routing and Filtering: Routes queries to specific backend servers based on rules, enabling better load distribution and resource utilization.
User-based Access Control: Implements fine-grained access control, restricting users to specific queries or databases.
Scalability: Allows seamless horizontal scaling by adding database nodes without changing applications.
Query Insights: Provides real-time monitoring and query statistics to identify performance bottlenecks.
Support for Private Networks: Works with private IP databases, enhancing security in cloud environments.
2.2 Common Use Cases
Applications handling high read/write workloads.
Environments requiring automatic failover.
Systems needing load balancing across multiple replicas.
Hybrid cloud/multi-cloud database architectures.
3. ProxySQL Setup
3.1 Instance Creation
Creating a ProxySQL VM Instance
Select a machine configuration that meets workload needs.
Set Boot Disk Size based on database query load.
Attach the VM to a custom VPC for private networking.
Creating Cloud SQL Instances (Master & Replica)
Step 1: Create Master Instance
Open Google Cloud Console → Go to Cloud SQL → Click Create Instance → Choose MySQL.
Select Edition → Set MySQL Version 8.0.
Choose Private IP and attach it to the VPC network.
Click Create and wait for provisioning.
Step 2: Create Replica
Go to Cloud SQL Instances → Select the Master → Click Create Read Replica.
Ensure Private IP is enabled and choose the same VPC Network.
Provide a unique name and create the replica.
Step 3: Verify Setup
Check Replica tab in Cloud SQL.
Perform write operations on the Master and verify replication on the Replica.
3.2 Installing ProxySQL
Install ProxySQL on a VM
|
Add ProxySQL Repository
|
Install ProxySQL
|
Start ProxySQL Service
|
Verify Installation
|
Install MySQL Client
|
3.3 Configuring ProxySQL
Login to ProxySQL Admin Interface
|
Add MySQL Backend Servers (Master & Replica)
|
3.4 Setting Up Users in ProxySQL
|
3.5 Query Routing Rules
Write Queries → Master (Hostgroup 10)
|
Read Queries → Replica (Hostgroup 20)
|
|
MySQL Server Configuration
- Log in to the MySQL server as the root user:
|
- For full access
|
Read-only access
For ex: - ranjith
(read-only):
|
Monitor User Configuration:
- Create Monitor User in MySQL:
|
If already monitor present then alter the password
|
- Configure Monitor User in ProxySQL:
|
- Check Monitor Configuration:
|
- Apply Changes:
|
Check Server Health and Logs in Proxysql:
- Check MySQL Server Connection Logs:
|
- Check MySQL Server Ping Logs:
|
4. Scenario: Using ProxySQL in Production
4.1 Background
A high-traffic web application faces:
Overloaded Primary DB due to excessive read queries.
Manual Failover Issues when the master database crashes.
Inefficient Query Routing where all queries hit the master.
4.2 How ProxySQL Solves These Issues
Problem | Solution with ProxySQL |
High Read Traffic | ProxySQL routes SELECT queries to replicas. |
Failover Issues | ProxySQL automatically reroutes traffic when a server fails. |
Query Optimization | Query caching & rewriting improve performance. |
4.3 ProxySQL Monitoring
|
5. Final Verification & Conclusion
5.1 Verify ProxySQL Connectivity
|
5.2 View ProxySQL Logs
|
5.3 Track Server Load Distribution:
|
5.4 View Client Statistics with Network Data:
|
5.5 Monitor Runtime Actions:
|
5.6 Examine Memory Usage:
|
5.7 Time_out
|
5.8 Conclusion
ProxySQL ensures load balancing & automatic failover.
Improves query performance through caching & optimization.
Supports seamless scaling without modifying applications.
Subscribe to my newsletter
Read articles from Ranjith Subramani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
