Aus Gründen einer effizienten Lastverteilung über den Cluster solltest du ProxySQL auf den Client-Knoten installieren – ganz ohne Änderungen an den Anwendungen, die die Anfragen erzeugen. Dies ist die empfohlene Hochverfügbarkeitslösung für Percona XtraDB Cluster.
Einige der beliebten Funktionen von ProxySQL sind:
- Hohe Leistung
- Effiziente Lastverwaltung
- Query-Caching
- Query-Routing
- Unterstützt Failover
- Erweiterte Konfiguration ohne Ausfallzeit
- Proxy auf Anwendungsebene
- Plattformübergreifend
- Unterstützung erweiterter Topologien
- Firewall
Umgebungsbeschreibung:
192.168.56.115
centos
Voraussetzungen:
Wir müssen auf allen Servern die folgenden Ports öffnen.
firewall-cmd --zone=public --add-service=mysql --permanent firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --zone=public --add-port=4567/tcp --permanent firewall-cmd --zone=public --add-port=4568/tcp --permanent firewall-cmd --zone=public --add-port=4444/tcp --permanent firewall-cmd --zone=public --add-port=4567/udp --permanent
Erlaube in der Linux-Firewall den ProxySQL-Dienstport 6033/tcp (das Gegenstück zum MySQL-Standardport 3306).
firewall-cmd --permanent --add-port=6033/tcp firewall-cmd --reload
ProxySQL-Load-Balancer für Percona XtraDB Cluster auf CentOS 7 installieren
ProxySQL v2 unterstützt Percona XtraDB Cluster nativ. Zur Installation von ProxySQL nutzt du das Percona-Repository.
sudo yum install proxysql2
Zum Verbinden mit der ProxySQL-Admin-Oberfläche benötigst du einen MySQL-Client.
yum install Percona-XtraDB-Cluster-client-57
Jetzt startest du den proxysql-Dienst.
[example@sqldat.com ~]# systemctl start proxysql.service
Den Port auf dem Cluster-Knoten prüfst du mit der folgenden Abfrage.
mysql -uroot -p -e "SHOW GLOBAL VARIABLES LIKE 'PORT'";
Verbinde dich nun mit dem ProxySQL-Admin-Panel und konfiguriere den Load Balancer.
mysql -u admin -p123 -h 127.0.0.1 -P6032 --prompt='ProxySQL> ' INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.56.110',3306); INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.56.113',3306); INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.56.114',3306); ProxySQL> SELECT * FROM mysql_servers; +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 0 | 192.168.56.110 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.56.113 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.56.114 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.00 sec)
ProxySQL-Knotenüberwachung konfigurieren
Melde dich an der MySQL-Dateninstanz an und führe die folgenden Befehle aus, um den Überwachungsbenutzer mit USAGE-Privileg zu erstellen.
on any percona NODE:
CREATE USER 'proxysql'@'%' IDENTIFIED BY 'ProxySQL'; GRANT USAGE ON *.* TO 'proxysql'@'%'; mysql> CREATE USER 'proxysql'@'%' IDENTIFIED BY 'ProxySQL'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT USAGE ON *.* TO 'proxysql'@'%'; Query OK, 0 rows affected (0.02 sec) UPDATE global_variables SET variable_value='proxysql' WHERE variable_name='mysql-monitor_username'; UPDATE global_variables SET variable_value='ProxySQL' WHERE variable_name='mysql-monitor_password'; LOAD MYSQL VARIABLES TO RUNTIME; SAVE MYSQL VARIABLES TO DISK; ProxySQL> UPDATE global_variables SET variable_value='proxysql' WHERE variable_name='mysql-monitor_username'; Query OK, 1 row affected (0.01 sec) ProxySQL> UPDATE global_variables SET variable_value='ProxySQL' WHERE variable_name='mysql-monitor_password'; Query OK, 1 row affected (0.00 sec) ProxySQL> LOAD MYSQL VARIABLES TO RUNTIME; Query OK, 0 rows affected (0.00 sec) ProxySQL> SAVE MYSQL VARIABLES TO DISK; Query OK, 136 rows affected (0.01 sec) ProxySQL> LOAD MYSQL SERVERS TO RUNTIME; Query OK, 0 rows affected (0.01 sec)
ProxySQL-Client-Benutzer erstellen
Provide read/write access to the cluster for ProxySQL, add this user on one of the Percona XtraDB Cluster nodes: CREATE USER 'lbuser'@'192.168.56.115' IDENTIFIED BY 'lbpass'; GRANT ALL ON *.* TO 'lbuser'@'192.168.56.115';
ProxySQL-Client-Benutzer erstellen:
ProxySQL> INSERT INTO mysql_users (username,password) VALUES ('lbuser','lbpass'); Query OK, 1 row affected (0.00 sec) ProxySQL> LOAD MYSQL USERS TO RUNTIME; Query OK, 0 rows affected (0.00 sec) ProxySQL> SAVE MYSQL USERS TO DISK; Query OK, 0 rows affected (0.02 sec)
Dass der Benutzer korrekt eingerichtet ist, kannst du durch einen Login prüfen:
[example@sqldat.com ~]# mysql -u lbuser -plbpass -h 127.0.0.1 -P 6033 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 4 Server version: 5.5.30 (ProxySQL) Copyright (c) 2009-2019 Percona LLC and/or its affiliates Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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. mysql>
Sehen wir, mit welchem Knoten des Clusters sich unser Client verbindet.
[example@sqldat.com ~]# mysql -u lbuser -plbpass -h 127.0.0.1 -P 6033 -e "select @@hostname;"; mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------+ | @@hostname | +----------------------+ | percona3.localdomain | +----------------------+
Der Proxy-Server ist also mit percona3 verbunden.
In einer weiteren Putty-Sitzung stellt er eine Verbindung zu percona2 her:
[example@sqldat.com ~]# mysql -u lbuser -plbpass -h 127.0.0.1 -P 6033 -e "select @@hostname;"; mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------+ | @@hostname | +----------------------+ | percona2.localdomain | +----------------------+
Nach einigen Sitzungen wird die Verbindung zu percona1 hergestellt:
[example@sqldat.com ~]# mysql -u lbuser -plbpass -h 127.0.0.1 -P 6033 -e "select @@hostname;"; mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------+ | @@hostname | +----------------------+ | percona1.localdomain | +----------------------+
Verbindung aus MySQL Workbench
So sieht die Konfiguration meiner Sitzung in MySQL Workbench aus:

Ich habe getestet, mit welchem Knoten die Verbindung aus MySQL Workbench hergestellt wird.

Failover
Nun prüfen wir, wie sich das Failover verhält.
[example@sqldat.com mysql]# service mysql stop Redirecting to /bin/systemctl stop mysql.service ProxySQL> select hostgroup_id,hostname,port,status from runtime_mysql_servers; +--------------+----------------+------+---------+ | hostgroup_id | hostname | port | status | +--------------+----------------+------+---------+ | 0 | 192.168.56.110 | 3306 | ONLINE | | 0 | 192.168.56.114 | 3306 | SHUNNED | | 0 | 192.168.56.113 | 3306 | ONLINE | +--------------+----------------+------+---------+ 3 rows in set (0.01 sec)
Jetzt starten wir mysql auf Knoten 3 neu und prüfen den Cluster-Status erneut vom Proxy-Server.
[example@sqldat.com mysql]#systemctl start mysql.service ProxySQL> SELECT hostgroup_id hg,count(status) cnt from main.runtime_mysql_servers WHERE status = "ONLINE" GROUP BY hg having cnt ; +----+-----+ | hg | cnt | +----+-----+ | 0 | 3 | +----+-----+ 1 row in set (0.00 sec) ProxySQL> select hostgroup_id,hostname,port,status from runtime_mysql_servers; +--------------+----------------+------+--------+ | hostgroup_id | hostname | port | status | +--------------+----------------+------+--------+ | 0 | 192.168.56.110 | 3306 | ONLINE | | 0 | 192.168.56.114 | 3306 | ONLINE | | 0 | 192.168.56.113 | 3306 | ONLINE | +--------------+----------------+------+--------+ 3 rows in set (0.00 sec)
HINWEIS: Zeigt ein Knoten den Status SHUNNED, versuche mehrfach eine Verbindung, damit der Client den aktuellen Cluster-Status erhält; runtime_mysql_servers speichert den Status des Knotens vom letzten Verbindungsversuch.
Cluster mit SysBench testen
yum install sysbench
SysBench benötigt die Zugangsdaten des ProxySQL-Client-Benutzers (lbuser/lbpass) aus dem Abschnitt „ProxySQL-Client-Benutzer erstellen“.
sysbench /usr/share/sysbench/oltp_read_only.lua --threads=4 --mysql-host=127.0.0.1 --mysql-user=lbuser --mysql-password=lbpass --mysql-port=6033 --tables=10 --table-size=10000 prepare sysbench /usr/share/sysbench/oltp_read_only.lua --threads=4 --events=0 --time=300 --mysql-host=27.0.0.1 --mysql-user=lbuser --mysql-password=lbpass --mysql-port=6033 --tables=10 --table-size=10000 --range_selects=off --db-ps-mode=disable --report-interval=1 run
Die Anzahl der auf dem Cluster ausgeführten Befehle siehst du hier:
proxysql> SELECT * FROM stats_mysql_commands_counters;