TheRobotLearns Platform · ProxySQL: from first install to production
Sharding by User, Schema, and Data
Learning objectives
- Implement user-based sharding by assigning distinct `default_hostgroup` values to application users in `mysql_users`.
- Implement schema-based sharding with active `mysql_query_rules` that match `schemaname` and set `destination_hostgroup`.
- Implement data-based sharding with active `mysql_query_rules` matching shard-key text for INSERT/UPDATE/DELETE/SELECT statements.
- Load `mysql_query_rules` into runtime and save them to disk so shard routing persists.
- Prefer user or schema sharding when architecture allows, since data-based sharding needs rules per shard-key value and statement type.
- Plan application-level cross-shard join handling, because each query reaches one destination hostgroup.
Choose your shard path
You need CRM queries to land on the accounts shard, while OLTP queries land on the transactions shard. ProxySQL supports user-based, schema-based, and data-based sharding. The routing target is a destination_hostgroup set in mysql_users or a row in mysql_query_rules. A destination_hostgroup names the group where ProxySQL sends queries. ProxySQL routes a query to one hostgroup, so joins across shards must be handled at the application level. User-based routing is the simplest of the three and needs no query rules. The example maps accounts, transactions, and logging to hostgroups 0, 1, and 2. Schema-based routing maps schema names to destination hostgroups with query rules. Data-based routing is the most flexible, but it needs careful rule design. Data-based routing matches shard-key text in SQL statements. It can route by INSERT patterns or direct loc_id conditions. It can combine with read/write split by adding rules per shard. If the app can separate work by user or schema name, choose those first.
Takeaway: Use user or schema routing when possible; data routing adds shard-key rules.
Route by user or schema
You connect to CRM as accounts, and every query from that user should go to shard 0. In mysql_users, the default_hostgroup value gives that routing target. Insert each application user with its own default_hostgroup, as shown for accounts, transactions, and logging. The examples use hostgroups 0, 1, and 2 for those users. Load users into runtime with LOAD MYSQL USERS TO RUNTIME, then save them to disk with SAVE MYSQL USERS TO DISK. Users with different default groups can route to different shards.
If your databases follow a naming convention such as shard_0, shard_1, and shard_2, use mysql_query_rules. Each row maps one schemaname to one destination_hostgroup. The examples set active to 1 and apply to 1. The rule for shard_0 sends queries to hostgroup 0, and the rule for shard_1 sends queries to hostgroup 1. Load schema rules with LOAD MYSQL QUERY RULES TO RUNTIME, then save them with SAVE MYSQL QUERY RULES TO DISK. SAVE MYSQL USERS TO DISK and SAVE MYSQL QUERY RULES TO DISK write rows to disk.
This inserts one user route and one schema route.
INSERT INTO mysql_users (username, password, active, default_hostgroup, comment)
VALUES ('accounts', 'shard0_pass', 1, 0, 'Routed to the accounts shard');
INSERT INTO mysql_query_rules (rule_id, active, schemaname, destination_hostgroup, apply)
VALUES (1, 1, 'shard_0', 0, 1);Takeaway: Set default_hostgroup for user routes, or set schemaname rules for schema routes.
Match shard keys in rules
You must send rows with loc_id = 20086020554955909836090724037181646035 to hostgroup 0, while rows with another loc_id go to hostgroup 1. Data-based sharding routes specific rows identified by a shard key. The example schema has loc_account_data and loc_mapping, each with loc_id values such as 20086020554955909836090724037181646035, 21503957780049285539986052866765125704, and 75863560943999160082133817802533222835. For INSERT statements, the rule match_pattern contains the table name and the shard-key value, such as loc_account_data.*20086020554955909836090724037181646035. For UPDATE, DELETE, and SELECT statements, the rule matches the shard key directly, such as loc_id = 20086020554955909836090724037181646035. Each row uses active 1, destination_hostgroup, and apply 1. The example maps the three loc_id values to hostgroups 0, 1, and 2. INSERT rules and direct-key rules are separate examples. They use different match_pattern text. The rule_id values differ in the examples. Data-based routing can also combine with read/write split by adding rules per shard. Load the rules into runtime with LOAD MYSQL QUERY RULES TO RUNTIME, then save them with SAVE MYSQL QUERY RULES TO DISK.
This inserts one INSERT rule and one direct loc_id rule, then loads and saves them.
INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, apply)
VALUES
(1, 1, 'loc_account_data.*20086020554955909836090724037181646035', 0, 1),
(4, 1, 'loc_id = 20086020554955909836090724037181646035', 0, 1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;Takeaway: For data sharding, add INSERT and direct-key rules, then load and save query rules.
Data rules need statement types
One shard-key value needs rules for each statement type: INSERT, UPDATE, DELETE, and SELECT. Data-based sharding requires a rule for every shard key value and every statement type. The example uses three loc_id values, separate INSERT patterns, and direct loc_id patterns. User and schema sharding are easier to implement and keep simple, so pick them when the architecture allows. A user route needs only user rows, LOAD MYSQL USERS TO RUNTIME, and SAVE MYSQL USERS TO DISK. A schema route needs active rules with schemaname and destination_hostgroup. Cross-shard joins need application handling, because ProxySQL routes a query to one hostgroup. If you combine sharding with read/write split, add more rules per shard. These three methods are the most common, but sharding is not limited to them. Run LOAD MYSQL USERS TO RUNTIME for users and LOAD MYSQL QUERY RULES TO RUNTIME for rules. Run SAVE MYSQL USERS TO DISK for users and SAVE MYSQL QUERY RULES TO DISK for rules. Each additional shard-key value adds more rules, and each additional statement type adds more rules.
Takeaway: Data sharding needs rules per shard-key value and statement type; use user or schema routes when possible.
Route two users and two schemas
INSERT INTO mysql_users (username, password, active, default_hostgroup, comment)
VALUES
('accounts', 'shard0_pass', 1, 0, 'Routed to the accounts shard'),
('transactions', 'shard1_pass', 1, 1, 'Routed to the transactions shard');
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;
INSERT INTO mysql_query_rules (rule_id, active, schemaname, destination_hostgroup, apply)
VALUES
(1, 1, 'shard_0', 0, 1),
(2, 1, 'shard_1', 1, 1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;The first INSERT INTO mysql_users statement adds accounts and transactions with default_hostgroup values 0 and 1. LOAD MYSQL USERS TO RUNTIME loads those user rows into runtime. SAVE MYSQL USERS TO DISK saves those user rows to disk. The INSERT INTO mysql_query_rules statement adds rule_id values 1 and 2 for shard_0 and shard_1. LOAD MYSQL QUERY RULES TO RUNTIME loads those rule rows into runtime. SAVE MYSQL QUERY RULES TO DISK saves those rule rows to disk.
Try it: query_rule_playground
Add the logging user and the shard_2 schema rule shown in the examples.
Insert logging with password shard2_pass, active 1, default_hostgroup 2, and comment Routed to the logging shard.
Insert a mysql_query_rules row with rule_id 3, active 1, schemaname shard_2, destination_hostgroup 2, and apply 1.
Run LOAD MYSQL USERS TO RUNTIME, then run SAVE MYSQL USERS TO DISK.
Run LOAD MYSQL QUERY RULES TO RUNTIME, then run SAVE MYSQL QUERY RULES TO DISK.
Sources
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
- How to Set Up ProxySQL Sharding
Do more with an account
- Graded quizzes is available on your account.
- Saving your progress is available on your account.
- Hands-on labs is available on your account.
- AI tutoring is available on your account.