TheRobotLearns Platform · ProxySQL: from first install to production

Encrypt Client-to-Proxy and Proxy-to-Backend Traffic

  • about 18 min read

Learning objectives

  • Set mysql_servers.use_ssl, load runtime, and verify ProxySQL creates TLS backend connections.
  • Configure mysql-ssl_p2s_cert and mysql-ssl_p2s_key, then load backend TLS variables to runtime.
  • Verify negotiated backend TLS cipher using SHOW SESSION STATUS LIKE Ssl_cipher through ProxySQL.
  • Enable mysql-have_ssl=true and identify generated proxysql-ca.pem, proxysql-cert.pem, and proxysql-key.pem files.
  • Distinguish frontend TLS enablement from mysql_users.use_ssl enforcement for individual users.
  • Renew backend TLS by atomically replacing files or repointing mysql-ssl_p2s_* variables, then runtime loading.

Backend TLS starts with one flag

You mark 127.0.0.1:21891 with use_ssl=1 in mysql_servers. LOAD MYSQL SERVERS TO RUNTIME is the command that makes runtime_mysql_servers show the updated row. ProxySQL supports SSL connections to the backends, and use_ssl carries that request.

Update mysql_servers.use_ssl for each backend that should use TLS. LOAD MYSQL SERVERS TO RUNTIME puts the row into runtime_mysql_servers. If one backend must serve TLS and non-TLS sessions, configure it in two hostgroup_id values and define access rules. Before the update, SELECT * FROM mysql_servers lists those rows. The example uses hostgroup_id 1 and 2, and the rows are ONLINE.

This example changes the flag, loads it, and checks the runtime table:

UPDATE mysql_servers SET use_ssl=1 WHERE port=21891;
LOAD MYSQL SERVERS TO RUNTIME;
SELECT * FROM runtime_mysql_servers;

After you verify the runtime row, SAVE MYSQL SERVERS TO DISK keeps the configuration on disk. The result shows use_ssl=1 for 127.0.0.1:21891. To confirm the negotiated TLS cipher through ProxySQL, connect to ProxySQL and run SHOW SESSION STATUS LIKE "Ssl_cipher".

Takeaway: Set mysql_servers.use_ssl, load servers, then inspect runtime_mysql_servers or the session cipher.

Point ProxySQL at backend keys

To use a specific key and certificate for backend connections, configure the backend SSL variables. The backend SSL variables start with mysql-ssl_p2s_; mysql-ssl_p2s_cert and mysql-ssl_p2s_key set the certificate and private key paths.

Set each variable, then run LOAD MYSQL VARIABLES TO RUNTIME to make the new paths active. ProxySQL then uses them for new backend connections. After the load, SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-ssl%' reads the changed rows in global_variables.

The global_variables rows for mysql-ssl% begin blank, then change after the SET commands. The example paths are /home/vagrant/newcerts/client-cert.pem and /home/vagrant/newcerts/client-key.pem. This example sets the certificate and key, then loads the variables:

SET mysql-ssl_p2s_cert='/home/vagrant/newcerts/client-cert.pem';
SET mysql-ssl_p2s_key='/home/vagrant/newcerts/client-key.pem';
LOAD MYSQL VARIABLES TO RUNTIME;

Run SHOW SESSION STATUS LIKE "Ssl_cipher" through ProxySQL to see the negotiated cipher. If the configuration is good, run SAVE MYSQL VARIABLES TO DISK and SAVE MYSQL SERVERS TO DISK to persist it. ProxySQL also exposes mysql-ssl_p2s_ca, mysql-ssl_p2s_capath, mysql-ssl_p2s_cipher, mysql-ssl_p2s_crl, and mysql-ssl_p2s_crlpath.

Takeaway: Set mysql-ssl_p2s_cert and mysql-ssl_p2s_key, load variables, then check Ssl_cipher.

Frontend TLS and user enforcement

You enable SSL for connections from clients to ProxySQL with mysql-have_ssl=true. ProxySQL creates proxysql-ca.pem, proxysql-cert.pem, and proxysql-key.pem in /var/lib/proxysql.

Enable the variable, then run LOAD MYSQL VARIABLES TO RUNTIME. Only new client connections use SSL after that load. You can replace the generated files with predefined certificate and key files. Having SSL enabled for frontend connections is not the same as enforcing it. Enforcing SSL connections is supported per user through mysql_users.use_ssl.

This example enables frontend TLS and loads the variables:

SET mysql-have_ssl=true;
LOAD MYSQL VARIABLES TO RUNTIME;

Connect a MySQL client to ProxySQL and run \s to see the client-side cipher. The command uses -h127.0.0.1 -P6033 -uroot -psecret. It pipes \s through grep -P 'SSL|Connection'. The output includes SSL: Cipher in use is DHE-RSA-AES256-SHA and Connection: 127.0.0.1 via TCP/IP. This verifies the client-to-ProxySQL TLS cipher.

Takeaway: Set mysql-have_ssl=true, load variables, and use mysql_users.use_ssl only for per-user enforcement.

Renew TLS material atomically

For backend certificates and keys, ProxySQL has two renewal routes:

  • Swap the files pointed to by the backend mysql-ssl_p2s_* variables with new certificates. This swap must be performed atomically because ProxySQL will be accessing those paths when creating new backend connections.
  • Point the backend mysql-ssl_p2s_* variables to the new file locations and issue LOAD MYSQL VARIABLES TO RUNTIME against the Admin interface. This operation is guaranteed to be atomic.

For frontend certificates, first make sure SSL is enabled for client connections. If it is not, set mysql-have_ssl=true, then issue LOAD MYSQL VARIABLES TO RUNTIME. Next, replace these files in the datadir:

${DATADIR_PATH}/proxysql-ca.pem
${DATADIR_PATH}/proxysql-cert.pem
${DATADIR_PATH}/proxysql-key.pem

Then issue the Admin variable command PROXYSQL RELOAD TLS.

If all operations succeed, check the ProxySQL error log for errors. The new certificates will be loaded and used for all new incoming connections.

Takeaway: Backend renewal by variable repointing plus LOAD MYSQL VARIABLES TO RUNTIME is guaranteed atomic; after frontend file replacement, issue PROXYSQL RELOAD TLS.

Enable backend TLS and verify the cipher

UPDATE mysql_servers SET use_ssl=1 WHERE port=21891;
LOAD MYSQL SERVERS TO RUNTIME;
SELECT * FROM runtime_mysql_servers;
SET mysql-ssl_p2s_cert='/home/vagrant/newcerts/client-cert.pem';
SET mysql-ssl_p2s_key='/home/vagrant/newcerts/client-key.pem';
LOAD MYSQL VARIABLES TO RUNTIME;
SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-ssl%';
SHOW SESSION STATUS LIKE "Ssl_cipher";

The UPDATE statement changes the use_ssl value in mysql_servers for port 21891. LOAD MYSQL SERVERS TO RUNTIME moves that row into runtime_mysql_servers. SELECT * FROM runtime_mysql_servers lists the runtime rows and their use_ssl values. SET mysql-ssl_p2s_cert='/home/vagrant/newcerts/client-cert.pem' sets the backend certificate path. SET mysql-ssl_p2s_key='/home/vagrant/newcerts/client-key.pem' sets the backend private key path. LOAD MYSQL VARIABLES TO RUNTIME makes the SSL variable changes active. SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-ssl%' reads the backend SSL variables. SHOW SESSION STATUS LIKE "Ssl_cipher" returns the cipher value for the session.

Try it: config_layer_sim

Run these statements with a MySQL client connected to ProxySQL:

UPDATE mysql_servers SET use_ssl=1 WHERE port=21891;
LOAD MYSQL SERVERS TO RUNTIME;
SELECT * FROM runtime_mysql_servers;
SET mysql-ssl_p2s_cert='/home/vagrant/newcerts/client-cert.pem';
SET mysql-ssl_p2s_key='/home/vagrant/newcerts/client-key.pem';
LOAD MYSQL VARIABLES TO RUNTIME;
SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-ssl%';

Then connect to ProxySQL and run SHOW SESSION STATUS LIKE "Ssl_cipher". If you are happy with the changes, run SAVE MYSQL SERVERS TO DISK and SAVE MYSQL VARIABLES TO DISK.

Sources

Do more with an account

Encrypt Client-to-Proxy and Proxy-to-Backend Traffic — ProxySQL: from first install to production