5.9 KiB
You can implement ProxySQL on an existing MariaDB setup, including an already running master-slave replication configuration. The process involves setting up ProxySQL as a proxy layer between your application and the MariaDB servers, and configuring it to route queries intelligently (e.g., read queries to slaves, write queries to the master).
Here’s how you can integrate ProxySQL with an existing MariaDB master-slave replication setup:
Step-by-Step Process to Integrate ProxySQL with Existing MariaDB Setup
1. Verify the Existing MariaDB Setup
Ensure you have the following:
- MariaDB master-slave replication working.
- Your master server (e.g.,
master-db-server
) is accepting write queries. - Your slave servers (e.g.,
slave1-db-server
,slave2-db-server
) are replicating the data from the master and can handle read queries.
If your master-slave replication is set up correctly and you can write to the master and read from the slaves, you can move to the next steps.
2. Install ProxySQL
You can install ProxySQL on a separate server or on the same server as your application. ProxySQL will sit between your application and MariaDB, acting as a load balancer for database queries.
Install ProxySQL on Ubuntu/Debian:
sudo apt-get update
sudo apt-get install proxysql
Install ProxySQL using Docker:
docker pull proxysql/proxysql
docker run -d -p 6033:6033 -p 6032:6032 proxysql/proxysql
3. Connect to ProxySQL Admin Interface
ProxySQL has an admin interface available on port 6032
by default. You’ll need to connect to it to configure the servers and query rules.
mysql -u admin -p -h 127.0.0.1 -P 6032
By default, the admin user is admin
, and the password is admin
.
4. Configure the Master and Slave Servers in ProxySQL
Now, you will configure ProxySQL to know about your MariaDB master and slave servers.
- Define the servers in ProxySQL's
mysql_servers
table. Each server is assigned a hostgroup. The master will be in hostgroup0
, and the slaves will be in hostgroup1
.
-- Define the master server
INSERT INTO mysql_servers (hostgroup_id, hostname, port)
VALUES (0, 'master-db-server', 3306);
-- Define the slave servers
INSERT INTO mysql_servers (hostgroup_id, hostname, port)
VALUES (1, 'slave1-db-server', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port)
VALUES (1, 'slave2-db-server', 3306);
- Apply the changes:
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
5. Create Query Rules for Routing Queries
Next, you need to create rules that define how ProxySQL should route queries:
- Read queries (
SELECT
) should go to the slave servers. - Write queries (
INSERT
,UPDATE
,DELETE
) should go to the master server.
-- Route SELECT queries to slaves (hostgroup 1)
INSERT INTO mysql_query_rules (active, match_pattern, destination_hostgroup)
VALUES (1, '^SELECT', 1);
-- Route INSERT, UPDATE, DELETE queries to the master (hostgroup 0)
INSERT INTO mysql_query_rules (active, match_pattern, destination_hostgroup)
VALUES (1, '^(INSERT|UPDATE|DELETE)', 0);
-- Reload the query rules to apply them
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;
6. Set Up the Application to Use ProxySQL
Now that ProxySQL is configured, modify your application's database connection settings to connect to ProxySQL instead of directly connecting to the MariaDB servers.
Change your application’s database configuration (hostname or connection string) to point to ProxySQL’s IP address and port 6033
(the MySQL protocol port used by ProxySQL):
For example, if ProxySQL is running on 192.168.1.100
, your database configuration should look like this:
host=192.168.1.100; port=6033; user=dbuser; password=dbpassword; database=yourdatabase;
Now, ProxySQL will:
- Route read queries (
SELECT
) to the slave servers. - Route write queries (
INSERT
,UPDATE
,DELETE
) to the master server.
7. Test the Setup
You can now test if the queries are being routed correctly:
- Run a
SELECT
query from your application, and verify it gets routed to one of the slave servers. - Run an
INSERT
,UPDATE
, orDELETE
query, and verify it gets routed to the master server.
You can check ProxySQL's internal tables to see the status of servers and query rules:
-- Check the list of servers
SELECT * FROM mysql_servers;
-- Check the query rules
SELECT * FROM mysql_query_rules;
8. Monitor ProxySQL
You can monitor ProxySQL's performance and traffic using the admin interface or by querying its runtime state.
Example:
-- Show server status
SELECT * FROM stats_mysql_connection_pool;
-- Show query rules
SELECT * FROM mysql_query_rules;
9. Optional: Set Up ProxySQL for High Availability
If you want ProxySQL to be highly available (HA), you can set up ProxySQL in a clustered configuration. ProxySQL supports replication for its configuration database, so you can run multiple ProxySQL instances and synchronize them.
For high availability:
- Use ProxySQL replication to replicate the configuration across multiple ProxySQL instances.
- Use a load balancer (e.g., HAProxy) to distribute traffic to the ProxySQL instances.
Conclusion
Yes, you can integrate ProxySQL with an existing MariaDB setup. ProxySQL will:
- Automatically route read and write queries to the appropriate servers (slaves for reads, master for writes).
- Help scale horizontally by directing traffic to slave servers for read operations and using the master for writes.
This setup should work well for your existing MariaDB master-slave configuration without requiring any changes to the database itself, just the addition of ProxySQL to act as an intermediary between your application and the database.