TheRobotLearns Platform · ProxySQL: from first install to production
Backend Hostgroups, Monitoring, and Replication Routing
Learning objectives
- Add backend MySQL servers to `mysql_servers` using explicit hostgroup, hostname, and port values.
- Create a MySQL monitor user, configure ProxySQL monitoring credentials and intervals, and load them into runtime.
- Configure `mysql_replication_hostgroups` so read_only=0 servers act as writers and read_only=1 servers act as readers.
- Assign `default_hostgroup` values in `mysql_users` so application traffic has a baseline routing destination.
- Route selected queries by adding active `mysql_query_rules` that apply in ascending `rule_id` order and stop on `apply=1`.
- Inspect `stats_mysql_query_digest` by hostgroup to confirm where query traffic lands.
- Apply `cache_ttl` to reader-targeted rules and rewrite queries with `match_pattern` and `replace_pattern` when digest matching is insufficient.
Add backend servers and monitoring
You connect to the ProxySQL Admin interface with the MySQL client. You add backend MySQL nodes to mysql_servers. Each row needs an explicit hostgroup_id, hostname, and port. A hostgroup is the group identified by hostgroup_id.
Set read_only = 0 on primary servers. Set read_only = 1 on replicas. ProxySQL uses this value for routing decisions.
ProxySQL needs a monitor user on MySQL. You run CREATE USER monitor IDENTIFIED BY 'monitor';. You run GRANT USAGE, REPLICATION CLIENT ON *.* TO monitor;. You store mysql-monitor_username and mysql-monitor_password in global_variables.
You set the connect, ping, and read-only monitor intervals to 2000. You load those settings with LOAD MYSQL VARIABLES TO RUNTIME. You save them with SAVE MYSQL VARIABLES TO DISK. After the server rows exist, you load them with LOAD MYSQL SERVERS TO RUNTIME.
The monitor schema shows backend health in mysql_server_connect_log and mysql_server_ping_log.
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'10.0.0.1',3306);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'10.0.0.2',3306);This adds two servers to hostgroup 1.
Takeaway: Add server rows to mysql_servers, configure monitor variables, then load settings and server rows.
Map writers, readers, and users
You add a mapping in mysql_replication_hostgroups. ProxySQL moves servers between hostgroups based on read_only. The mapping stores a writer_hostgroup and a reader_hostgroup. It also stores a comment.
A writer hostgroup is the hostgroup for read_only=0. A reader hostgroup is the hostgroup for read_only=1. You insert writer hostgroup 1 and reader hostgroup 2. Hostgroup 1 receives writers, and hostgroup 2 receives readers.
You load the mapping with LOAD MYSQL SERVERS TO RUNTIME. You can view recent read_only checks in monitor.mysql_server_read_only_log. The example orders rows by time_start_us descending.
Application traffic needs a row in mysql_users. You add the application username, password, and default_hostgroup. The default_hostgroup controls where traffic from that user is routed by default.
You load user changes with LOAD MYSQL USERS TO RUNTIME. You save them with SAVE MYSQL USERS TO DISK.
INSERT INTO mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment) VALUES (1,2,'cluster1');
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('stnduser','stnduser',1);
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL USERS TO RUNTIME;This maps hostgroup 1 to writers, hostgroup 2 to readers, and gives stnduser a default hostgroup of 1.
Takeaway: Map writer and reader hostgroups, then give each user a default_hostgroup.
Route queries and check stats
The user default_hostgroup is the default route. Selected queries can be routed by active rows in mysql_query_rules. A rule can match the query digest or the full query text. ProxySQL evaluates rules in ascending rule_id order.
Only active=1 rules are considered. A rule with apply=1 stops further evaluation for that query. The match_digest value uses a regular expression. The example sends a matching query to hostgroup 2.
INSERT INTO mysql_query_rules(rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'stnduser','^SELECT c FROM sbtest1 WHERE id=?',2,1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SELECT hostgroup hg, SUM(sum_time), SUM(count_star) FROM stats_mysql_query_digest GROUP BY hostgroup;This inserts an active rule, loads query rules, and totals query counts by hostgroup.
ProxySQL stores per-query statistics in stats_mysql_query_digest. You group hostgroup, sum_time, and count_star to inspect traffic by hostgroup. Cached results appear in stats_mysql_query_digest with hostgroup = -1.
You can cache results for active rules that select hostgroup 2 by setting cache_ttl=5000. The value is milliseconds. You then run LOAD MYSQL QUERY RULES TO RUNTIME.
To rewrite query text, you set match_pattern and replace_pattern on a rule. Prefer match_digest over match_pattern where possible. Digests are smaller, and matching is faster.
Takeaway: Add active mysql_query_rules, load them, and inspect stats_mysql_query_digest by hostgroup.
Configure routing and check stats
This adds a backend server, configures monitoring, maps writer and reader hostgroups, adds an application user, adds one query rule, and loads each area into runtime.
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'10.0.0.1',3306);
UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_password';
UPDATE global_variables SET variable_value='2000' WHERE variable_name IN ('mysql-monitor_connect_interval','mysql-monitor_ping_interval','mysql-monitor_read_only_interval');
LOAD MYSQL VARIABLES TO RUNTIME;
INSERT INTO mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment) VALUES (1,2,'cluster1');
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('stnduser','stnduser',1);
INSERT INTO mysql_query_rules(rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'stnduser','^SELECT c FROM sbtest1 WHERE id=?',2,1);
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL USERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
SELECT hostgroup hg, SUM(sum_time), SUM(count_star) FROM stats_mysql_query_digest GROUP BY hostgroup;The first statement adds 10.0.0.1:3306 to hostgroup 1. The second statement stores monitor in mysql-monitor_username. The third statement stores monitor in mysql-monitor_password. The fourth statement sets the three monitor intervals to 2000. The fifth statement loads the monitoring variables into runtime. The sixth statement maps writer hostgroup 1 to reader hostgroup 2. The seventh statement gives stnduser default hostgroup 1. The eighth statement sends a matching digest for stnduser to hostgroup 2. The ninth statement loads server and replication mapping changes into runtime. The tenth statement loads the user row into runtime. The eleventh statement loads the query rule into runtime. The twelfth statement totals sum_time and count_star for each hostgroup in stats_mysql_query_digest.
Try it: query_rule_playground
You add rule 10 with active=1, match_digest='^SELECT c FROM sbtest1 WHERE id=?', destination_hostgroup=2, and apply=1. You run LOAD MYSQL QUERY RULES TO RUNTIME. You run SELECT hostgroup hg, SUM(sum_time), SUM(count_star) FROM stats_mysql_query_digest GROUP BY hostgroup;. You update active rules that select hostgroup 2 with cache_ttl=5000. You run LOAD MYSQL QUERY RULES TO RUNTIME again. Cached results appear in stats_mysql_query_digest with hostgroup = -1.
Sources
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
- How to Configure ProxySQL for MySQL for the First Time
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.