Percona's Docker modified for automated replication setup for testing Character_sets and Collations
Python
Percona's Percona-Server56 Docker, with primary and replica
How To use this project, a little prep work is needed.
Working withDockercreds:
Note:
Iwillchangethepasswordsinthenextfew days.
Createafile called .envintherootoftheprojectandaddthe following:
MYSQL_ROOT_PASSWORD=s3cr3tMYSQL_REPLICA_PASSWORD=slAv3
Replica password
This is a quick and dirty project for testing. Change the passwords.
Not very secure, but it is a test project.
docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mysql56-docker-primary-1 mysql56-docker-primary "/docker-entrypoint.…" primary 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:3306->3306/tcp
mysql56-docker-proxysql-1 mysql56-docker-proxysql "bash /entrypoint.sh" proxysql 3 minutes ago Up 3 minutes 0.0.0.0:6032-6033->6032-6033/tcp, 0.0.0.0:6080->6080/tcp
mysql56-docker-replica-1 mysql56-docker-replica "/docker-entrypoint.…" replica 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:3307->3306/tcp
docker exec -it mysql56-docker-proxysql-1 bash
root@a41e1300f8a4:/# mysql -h 127.0.0.1 -P 6032 -u admin -padmin --prompt 'ProxySQL Admin >'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
ProxySQL Admin >ProxySQL Admin >SELECT * FROM mysql_users\G*************************** 1. row ***************************
username: repl
password: slAv3
active: 1
use_ssl: 0
default_hostgroup: 20
default_schema:
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
attributes:
comment:
*************************** 2. row ***************************
username: klarsen
password: ou812
active: 1
use_ssl: 0
default_hostgroup: 10
default_schema:
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
attributes:
comment:
2 rows inset (0.00 sec)
Time for testing... in the ProxySQL Container we will start ngrep to listen to the traffic
root@a41e1300f8a4:/# ngrep -q -t -W byline -i 'SELECT|INSERT|UPDATE|SET' port 6033
interface: eth0 (172.22.0.0/255.255.0.0)
filter: ( port 6033 ) and ((ip || ip6) || (vlan && (ip || ip6)))
match (JIT): SELECT|INSERT|UPDATE|SET
T 2023/11/22 04:33:42.614288 192.168.65.1:34009 -> 172.22.0.4:6033 [AP] #125
=....INSERT INTO employees (name, salary) VALUES ('Chaos2', 5000)
T 2023/11/22 04:33:42.621886 192.168.65.1:34009 -> 172.22.0.4:6033 [AP] #128
.....INSERT INTO test_table (name, additional_info) VALUES ('Chaos2', 'Some additional info with German umlauts: ., ., ., .. Here are some words: sch.n, fr.ulein, gr..en')
T 2023/11/22 04:34:57.284057 192.168.65.1:34066 -> 172.22.0.4:6033 [AP] #147!....select @@version_comment limit 1
T 2023/11/22 04:34:57.284342 192.168.65.1:34066 -> 172.22.0.4:6033 [AP] #150
....select $$
T 2023/11/22 04:34:57.285451 192.168.65.1:34066 -> 172.22.0.4:6033 [AP] #153
%....select @@hostname, @@version, @@port
T 2023/11/22 04:34:57.286048 192.168.65.1:34066 -> 172.22.0.4:6033 [AP] #156
8....select * from chaos.employees, chaos.test_table limit 3
In a seperate terminal, we will run the Python script, insert some data, watch the traffic and validate the data
mysql56-docker on main via 🐳 desktop-linux via 🐍 v3.11.5 (mysql56-docker)
❯ ./test.py
Connected to server 192.168.50.75 on port 6033
Current character set: latin1 and collation: latin1_swedish_ci
[(1,)]
Attempting to connect to the database with character set latin1 and collation latin1_swedish_ci
Attempting to connect to the database with character set latin1 and collation latin1_swedish_ci
Latin1 character set is accepted when adding an employee.
Connected to the database. Adding employee Chaos2 with salary 5000
Employee Chaos2 added successfully. Committing the transaction.
mysql --defaults-group-suffix=_proxysql1 -e "select @@hostname, @@version, @@port; select * from chaos.employees, chaos.test_table limit 3"
+--------------+-----------------+--------+
| @@hostname | @@version | @@port |
+--------------+-----------------+--------+
| ab1fe00558db | 5.6.51-91.0-log | 3306 |
+--------------+-----------------+--------+
+----+----------------------------------------------------------------------------------------------------------------------------+
| id | name | salary | id | name | additional_info |
+----+--------+--------+----+--------------------------
| 62 | Chaos2 | 5000 | 62 | Chaos2 | Some additional info with German umlauts: � , , � Here are some words: schn, fr�lein, gr�n || 63 | Chaos2 | 5000 | 62 | Chaos2 | Some additional info with German umlauts: � , , � Here are some words: schn, fr�lein, gr�n || 64 | Chaos2 | 5000 | 62 | Chaos2 | Some additional info with German umlauts: � , , � Here are some words: schn, fr�lein, gr�n |
+----+----+-----------------------------------------------------------------------------------------------------------------------+
When you attempt to utilize ProxySQL for a task that it may not have direct capabilities for, but can still achieve indirectly, you can effectively leverage its potential.
At first I was using ngrep then I used Python with ngrep and while that got me half way there,
I switch my thinking and used Python to query a table that ProxySQL already uses to record all of the queries going through it.
I thought surely it would record SET NAMES latin1 as well. Yes, Yes it does. I use that information,
that is queried from the stats.stats_mysql_query_digest and write that to an output.sql that then is applied back to ProxySQL
to in a sense, a non routable blackhole with the user to a hostgroup that does not exist.
ProxySQL Admin >SELECT * FROM stats.stats_mysql_query_digest WHERE digest_text LIKE '%SET NAMES latin1%'\G*************************** 1. row ***************************
hostgroup: 10
schemaname: chaos
username: flyway
client_address:
digest: 0x8436fac4a1e9c6f9
digest_text: SET NAMES latin1
count_star: 14
first_seen: 1701218045
last_seen: 1701218178
sum_time: 0
min_time: 0
max_time: 0
sum_rows_affected: 0
sum_rows_sent: 0
1 row inset (0.00 sec)
ProxySQL Admin >SELECT * FROM stats_mysql_global WHERE variable_name LIKE '%names%';
+------------------------+----------------+
| Variable_Name | Variable_Value |
+------------------------+----------------+
| Com_backend_set_names | 10 || Com_frontend_set_names | 28 |
+------------------------+----------------+
2 rows inset (0.01 sec)
ProxySQL Admin >SELECT digest_text, count_star FROM stats.stats_mysql_query_digest ORDER BY count_star DESC LIMIT 5;
+------------------------------------------------------------+------------+
| digest_text | count_star |
+------------------------------------------------------------+------------+
|set autocommit=?| 14 || SET NAMES latin1 | 14 || SET NAMES ? COLLATE ?| 14 || commit | 7 || INSERT INTO test_table (name,additional_info) VALUES (?,?) | 7 |
+------------------------------------------------------------+------------+
5 rows inset (0.01 sec)
ProxySQL Admin >SELECT * FROM stats.stats_mysql_query_rules ORDER BY hits DESC;
+---------+------+
| rule_id | hits |
+---------+------+
| 1 | 1 || 200 | 1 || 100 | 0 || 300 | 0 |
+---------+------+
4 rows inset (0.01 sec)
From the Python3 script proxyadd2.py:
nohup python3 proxyadd2.py &
root@0d74ce2134f9:/# tail -f nohup.out
INFO:root:Found 'SET NAMES latin1'for user: flyway
mysql: [Warning] Using a password on the command line interface can be insecure.
DEBUG:root:Fetched 1 rows
DEBUG:root:Writing 1 rows to file
INFO:root:Found 'SET NAMES latin1'for user: flyway
mysql: [Warning] Using a password on the command line interface can be insecure.
DEBUG:root:Fetched 1 rows
DEBUG:root:Writing 1 rows to file
INFO:root:Found 'SET NAMES latin1'for user: flyway
mysql: [Warning] Using a password on the command line interface can be insecure.