Oops! Something went wrong while submitting the form.
We use cookies to improve your browsing experience on our website, to show you personalised content and to analize our website traffic. By browsing our website, you consent to our use of cookies. Read privacy policy.
ProxySQL is a powerful SQL-aware proxy designed to sit between database servers and client applications, optimizing database traffic with features like load balancing, query routing, and failover. This article focuses on simplifying the setup of ProxySQL, especially for users implementing data-based sharding in a MySQL database.
What is Sharding?
Sharding involves partitioning a database into smaller, more manageable pieces called shards based on certain criteria, such as data attributes. ProxySQL supports data-based sharding, allowing users to distribute data across different shards based on specific conditions.
Understanding the Need for ProxySQL:
ProxySQL is an intermediary layer that enhances database management, monitoring, and optimization. With features like data-based sharding, ProxySQL is an ideal solution for scenarios where databases need to be distributed based on specific data attributes, such as geographic regions.
Installation & Setup:
There are two ways to install the proxy, either by installing it using packages or running ProxySQL in docker. ProxySQL can be installed using two methods: via packages or running it in a Docker container. For this guide, we will focus on the Docker installation.
1. Install ProxySQL and MySQL Docker Images:
To start, pull the necessary Docker images for ProxySQL and MySQL using the following commands:
The default MySQL protocol port is 3306, but since we cannot access all three of our MySQL containers on the same port, we have set their ports to 3307, 3308, and 3309. Although internally, all MySQL containers will connect using port 3306.
--network=multi-tenant-network. This specifies that the container should be created under the specified network.
We have also specified the root password of the MySQL container to log into it, where the username is “root” and the password is “pass123” for all three of them.
After running the above three commands, three MySQL containers will start running inside the network. You can connect to these three hosts using host = localhost or 127.0.0.1 and port = 3307 / 3308 / 3309.
By running these commands, you will get all the details of the MySQL Docker container under a field named “IPAddress” inside your network. That is the IP address of that particular MySQL container.
Example: mysql_host_1: 172.19.0.2
mysql_host_2: 172.19.0.3
mysql_host_3: 172.19.0.4
Now, create a ProxySQL configuration file named proxysql.cnf. Include details such as IP addresses of MySQL containers, administrative credentials, and MySQL users.
Below is the content that needs to be added to the proxysql.cnf file:
Most of the settings are default; we won’t go into much detail for each setting.
admin_variables: These variables are used for ProxySQL's administrative interface. It allows you to connect to ProxySQL and perform administrative tasks such as configuring runtime settings, managing servers, and monitoring performance.
mysql_variables, monitor_username, and monitor_password are used to specify the username that ProxySQL will use when connecting to MySQL servers for monitoring purposes. This monitoring user is used to execute queries and gather statistics about the health and performance of the MySQL servers. This is the user we created during step 4.
mysql_servers will contain all the MySQL servers we want to be connected with ProxySQL. Each entry will have the IP address of the MySQL container, port, host group, and max_connections. Mysql_users will have all the users we created during step 4.
7. Run ProxySQL Container:
Inside the same directory where the proxysql.cnf file is located, run the following command to start ProxySQL:
Here, port 6032 is used for ProxySQL's administrative interface. It allows you to connect to ProxySQL and perform administrative tasks such as configuring runtime settings, managing servers, and monitoring performance.
Port 6033 is the default port for ProxySQL's MySQL protocol interface. It is used for handling MySQL client connections. Our application will use it to access the ProxySQL db and make SQL queries.
The above command will make ProxySQL run on our Docker with the configuration provided in the proxysql.cnf file.
Inside ProxySQL Container:
8. Access ProxySQL Admin Console:
Now, to access the ProxySQL Docker container, use the following command:
To make queries to the database, we make use of ProxySQL’s 6033 port, where MySQL is being accessed.
9. Define Query Rules:
We can add custom query rules inside the mysql_query_rules table to redirect queries to specific databases based on defined patterns. Load the rules to runtime and save to disk.
12. Sharding Example:
Now, let's illustrate how to leverage ProxySQL's data-based sharding capabilities through a practical example. We'll create three MySQL containers, each containing data from different continents in the “world” database, specifically within the “countries” table.
Step 1: Create 3 MySQL containers named mysql_host_1, mysql_host_2 & mysql_host_3.
Inside all containers, create a database named “world” with a table named “countries”.
i) Inside mysql_host_1: Insert countries using the following query:
Based on the defined query rules, the queries will be redirected to the specified MySQL host groups. If no rules match, the default host group that’s specified in mysql_users inside proxysql.cnf will be used.
Conclusion:
ProxySQL simplifies access to distributed data through effective sharding strategies. Its flexible query rules, combined with regex patterns and host group definitions, offer significant flexibility with relative simplicity.
By following this step-by-step guide, users can quickly set up ProxySQL and leverage its capabilities to optimize database performance and achieve efficient data distribution.
Simplifying MySQL Sharding with ProxySQL: A Step-by-Step Guide
Introduction:
ProxySQL is a powerful SQL-aware proxy designed to sit between database servers and client applications, optimizing database traffic with features like load balancing, query routing, and failover. This article focuses on simplifying the setup of ProxySQL, especially for users implementing data-based sharding in a MySQL database.
What is Sharding?
Sharding involves partitioning a database into smaller, more manageable pieces called shards based on certain criteria, such as data attributes. ProxySQL supports data-based sharding, allowing users to distribute data across different shards based on specific conditions.
Understanding the Need for ProxySQL:
ProxySQL is an intermediary layer that enhances database management, monitoring, and optimization. With features like data-based sharding, ProxySQL is an ideal solution for scenarios where databases need to be distributed based on specific data attributes, such as geographic regions.
Installation & Setup:
There are two ways to install the proxy, either by installing it using packages or running ProxySQL in docker. ProxySQL can be installed using two methods: via packages or running it in a Docker container. For this guide, we will focus on the Docker installation.
1. Install ProxySQL and MySQL Docker Images:
To start, pull the necessary Docker images for ProxySQL and MySQL using the following commands:
The default MySQL protocol port is 3306, but since we cannot access all three of our MySQL containers on the same port, we have set their ports to 3307, 3308, and 3309. Although internally, all MySQL containers will connect using port 3306.
--network=multi-tenant-network. This specifies that the container should be created under the specified network.
We have also specified the root password of the MySQL container to log into it, where the username is “root” and the password is “pass123” for all three of them.
After running the above three commands, three MySQL containers will start running inside the network. You can connect to these three hosts using host = localhost or 127.0.0.1 and port = 3307 / 3308 / 3309.
By running these commands, you will get all the details of the MySQL Docker container under a field named “IPAddress” inside your network. That is the IP address of that particular MySQL container.
Example: mysql_host_1: 172.19.0.2
mysql_host_2: 172.19.0.3
mysql_host_3: 172.19.0.4
Now, create a ProxySQL configuration file named proxysql.cnf. Include details such as IP addresses of MySQL containers, administrative credentials, and MySQL users.
Below is the content that needs to be added to the proxysql.cnf file:
Most of the settings are default; we won’t go into much detail for each setting.
admin_variables: These variables are used for ProxySQL's administrative interface. It allows you to connect to ProxySQL and perform administrative tasks such as configuring runtime settings, managing servers, and monitoring performance.
mysql_variables, monitor_username, and monitor_password are used to specify the username that ProxySQL will use when connecting to MySQL servers for monitoring purposes. This monitoring user is used to execute queries and gather statistics about the health and performance of the MySQL servers. This is the user we created during step 4.
mysql_servers will contain all the MySQL servers we want to be connected with ProxySQL. Each entry will have the IP address of the MySQL container, port, host group, and max_connections. Mysql_users will have all the users we created during step 4.
7. Run ProxySQL Container:
Inside the same directory where the proxysql.cnf file is located, run the following command to start ProxySQL:
Here, port 6032 is used for ProxySQL's administrative interface. It allows you to connect to ProxySQL and perform administrative tasks such as configuring runtime settings, managing servers, and monitoring performance.
Port 6033 is the default port for ProxySQL's MySQL protocol interface. It is used for handling MySQL client connections. Our application will use it to access the ProxySQL db and make SQL queries.
The above command will make ProxySQL run on our Docker with the configuration provided in the proxysql.cnf file.
Inside ProxySQL Container:
8. Access ProxySQL Admin Console:
Now, to access the ProxySQL Docker container, use the following command:
To make queries to the database, we make use of ProxySQL’s 6033 port, where MySQL is being accessed.
9. Define Query Rules:
We can add custom query rules inside the mysql_query_rules table to redirect queries to specific databases based on defined patterns. Load the rules to runtime and save to disk.
12. Sharding Example:
Now, let's illustrate how to leverage ProxySQL's data-based sharding capabilities through a practical example. We'll create three MySQL containers, each containing data from different continents in the “world” database, specifically within the “countries” table.
Step 1: Create 3 MySQL containers named mysql_host_1, mysql_host_2 & mysql_host_3.
Inside all containers, create a database named “world” with a table named “countries”.
i) Inside mysql_host_1: Insert countries using the following query:
Based on the defined query rules, the queries will be redirected to the specified MySQL host groups. If no rules match, the default host group that’s specified in mysql_users inside proxysql.cnf will be used.
Conclusion:
ProxySQL simplifies access to distributed data through effective sharding strategies. Its flexible query rules, combined with regex patterns and host group definitions, offer significant flexibility with relative simplicity.
By following this step-by-step guide, users can quickly set up ProxySQL and leverage its capabilities to optimize database performance and achieve efficient data distribution.
Velotio Technologies is an outsourced software product development partner for top technology startups and enterprises. We partner with companies to design, develop, and scale their products. Our work has been featured on TechCrunch, Product Hunt and more.
We have partnered with our customers to built 90+ transformational products in areas of edge computing, customer data platforms, exascale storage, cloud-native platforms, chatbots, clinical trials, healthcare and investment banking.
Since our founding in 2016, our team has completed more than 90 projects with 220+ employees across the following areas:
Building web/mobile applications
Architecting Cloud infrastructure and Data analytics platforms