搭建PXC環境參考,
https://www.cnblogs.com/nanxiang/p/13948762.html
192.168.150.201 pxc1
192.168.150.202 pxc2
192.168.150.203 pxc3
192.168.150.250 ProxySQL
1、
使用ProxySQL實現對PXC的讀寫分離是官方推薦的一種方案,
https://www.percona.com/doc/percona-xtradb-cluster/5.7/howtos/proxysql.html
ProxySQL是高性能的SQL代理。ProxySQL作為監視程序監視的守護程序運行。該進程監視守護程序,並在發生崩潰的情況下重新啟動它,以最大程度地減少停機時間。
守護程序接受來自MySQL客戶端的傳入流量,並將其轉發到后端MySQL服務器。
代理被設計為無需重新啟動即可連續運行。大多數配置可以在運行時使用類似於SQL語句的查詢來完成。其中包括運行時參數,服務器分組以及與流量相關的設置。
可以從Percona軟件存儲庫中獲得兩個版本的ProxySQL。ProxySQL v1本身不支持Percona XtraDB群集,並且需要自定義bash腳本以使用ProxySQL調度程序跟蹤Percona XtraDB Cluster節點的狀態。
ProxySQL v2本機支持Percona XtraDB群集。在此版本中,該 proxysql-admin
工具不需要自定義腳本來跟蹤Percona XtraDB群集狀態。
我們直接安裝ProxySQL v2版本。
2、
搭建步驟
新開一台虛擬機,192.168.150.250
由於ProxySQL需要mysql客戶端去登錄,需要先安裝mysql客戶端,官方推薦的是安裝【Percona-XtraDB-Cluster-client-57】,因為網絡問題,我安裝的是mysql 5.7客戶端。
yum-config-manager --enable mysql57-community yum-config-manager --disable mysql80-community yum install mysql-community-client -y
3、
先安裝ProxySQL,
下載地址:
https://www.percona.com/downloads/proxysql2/
我選擇使用yum安裝,
yum install https://www.percona.com/downloads/proxysql2/proxysql2-2.0.14/binary/redhat/7/x86_64/proxysql2-2.0.14-1.1.el7.x86_64.rpm -y
service proxysql start [root@pxc_250 ~]# ps -ef|grep proxy root 6950 1 0 13:52 ? 00:00:00 /usr/sbin/gssproxy -D gdm 7914 7624 0 13:52 ? 00:00:00 /usr/libexec/gsd-screensaver-proxy proxysql 9035 1 0 14:38 ? 00:00:00 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf proxysql 9036 9035 0 14:38 ? 00:00:00 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf root 9089 8420 0 14:38 pts/0 00:00:00 grep --color=auto proxy
4、
配置參考:
https://www.percona.com/doc/percona-xtradb-cluster/5.7/howtos/proxysql.html
登錄proxysql
mysql -u admin -padmin -h 127.0.0.1 -P 6032
mysql> show databases; +-----+---------------+-------------------------------------+ | seq | name | file | +-----+---------------+-------------------------------------+ | 0 | main | | | 2 | disk | /var/lib/proxysql/proxysql.db | | 3 | stats | | | 4 | monitor | | | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db | +-----+---------------+-------------------------------------+ 5 rows in set (0.00 sec)
mysql> show tables; +----------------------------------------------------+ | tables | +----------------------------------------------------+ | global_variables | | mysql_aws_aurora_hostgroups | | mysql_collations | | mysql_firewall_whitelist_rules | | mysql_firewall_whitelist_sqli_fingerprints | | mysql_firewall_whitelist_users | | mysql_galera_hostgroups | | mysql_group_replication_hostgroups | | mysql_query_rules | | mysql_query_rules_fast_routing | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | proxysql_servers | | restapi_routes | | runtime_checksums_values | | runtime_global_variables | | runtime_mysql_aws_aurora_hostgroups | | runtime_mysql_firewall_whitelist_rules | | runtime_mysql_firewall_whitelist_sqli_fingerprints | | runtime_mysql_firewall_whitelist_users | | runtime_mysql_galera_hostgroups | | runtime_mysql_group_replication_hostgroups | | runtime_mysql_query_rules | | runtime_mysql_query_rules_fast_routing | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_proxysql_servers | | runtime_restapi_routes | | runtime_scheduler | | scheduler | +----------------------------------------------------+ 32 rows in set (0.00 sec)
注意:
ProxySQL具有配置可以駐留的3個區域:
- 內存(您當前的工作地點)
- RUNTIME(生產設置)
- DISK(持久配置,保存在SQLITE數據庫中)
更改參數時,可以在“存儲器”區域中對其進行更改。這是設計使然的,可讓您在進行生產之前測試更改(運行時)或將其保存到磁盤。
將群集節點添加到ProxySQL
要在ProxySQL中配置后端Percona XtraDB Cluster節點,請在mysql_servers
表中插入相應的記錄。
mysql> INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.150.201',3306); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.150.202',3306); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.150.203',3306); Query OK, 1 row affected (0.00 sec)
mysql> 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.150.201 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.150.202 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.150.203 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | +--------------+-----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.00 sec)
創建ProxySQL監控用戶,需要先在PXC上創建,再配置到ProxySQL中。
PXC創建:
mysql> CREATE USER 'proxysql'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.04 sec) mysql> GRANT USAGE ON *.* TO 'proxysql'@'%'; Query OK, 0 rows affected (0.13 sec)
ProxySQL中修改環境變量,ProxySQL會使用proxysql用戶連接Pxc集群,檢測集群狀態
UPDATE global_variables SET variable_value='proxysql' WHERE variable_name='mysql-monitor_username'; UPDATE global_variables SET variable_value='123456' WHERE variable_name='mysql-monitor_password';
加載環境變量到RUNTIME狀態,持久化環境變量到磁盤中。
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;
檢測連接日志和ping日志
mysql> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 6; +-----------------+------+------------------+-------------------------+---------------+ | hostname | port | time_start_us | connect_success_time_us | connect_error | +-----------------+------+------------------+-------------------------+---------------+ | 192.168.150.203 | 3306 | 1605164324106429 | 1408 | NULL | | 192.168.150.202 | 3306 | 1605164323643886 | 1439 | NULL | | 192.168.150.201 | 3306 | 1605164323181438 | 1315 | NULL | | 192.168.150.203 | 3306 | 1605164264180237 | 1263 | NULL | | 192.168.150.201 | 3306 | 1605164263681094 | 1348 | NULL | | 192.168.150.202 | 3306 | 1605164263181069 | 1538 | NULL | +-----------------+------+------------------+-------------------------+---------------+ 6 rows in set (0.00 sec)
mysql> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 6; +-----------------+------+------------------+----------------------+------------+ | hostname | port | time_start_us | ping_success_time_us | ping_error | +-----------------+------+------------------+----------------------+------------+ | 192.168.150.202 | 3306 | 1605164343613683 | 382 | NULL | | 192.168.150.201 | 3306 | 1605164343496221 | 536 | NULL | | 192.168.150.203 | 3306 | 1605164343379250 | 459 | NULL | | 192.168.150.202 | 3306 | 1605164333514780 | 639 | NULL | | 192.168.150.203 | 3306 | 1605164333446700 | 563 | NULL | | 192.168.150.201 | 3306 | 1605164333378854 | 755 | NULL | +-----------------+------+------------------+----------------------+------------+ 6 rows in set (0.00 sec)
加載MYSQL SERVERS到RUNTIME狀態,持久化MYSQL SERVERS到磁盤中。
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
創建ProxySQL客戶端用戶
INSERT INTO mysql_users (username,password) VALUES ('sbuser','sbpass'); LOAD MYSQL USERS TO RUNTIME; SAVE MYSQL USERS TO DISK;
登錄PXC集群,創建sbuser用戶
CREATE USER 'sbuser'@'%' IDENTIFIED BY 'sbpass'; GRANT ALL ON *.* TO 'sbuser'@'%';
測試讀寫分離,查詢命令可以分發到各個節點上。
[root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 201 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 201 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 203 | +-------------+
創建tt表,測試寫入命令。
mysql> create table tt(id int primary key auto_increment); Query OK, 0 rows affected (0.04 sec)
[root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 203 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 203 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 203 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "use ceshi;begin;insert into tt select null;select @@server_id;commit"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 201 | +-------------+
模擬203節點宕掉,KILL掉MYSQL進程。203狀態已經是SHUNNED
[root@pxc_250 ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 mysql> select * from runtime_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.150.201 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.150.203 | 3306 | 0 | SHUNNED | 1 | 0 | 1000 | 0 | 0 | 0 | | | 0 | 192.168.150.202 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | +--------------+-----------------+------+-----------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.01 sec)
再測試查詢,只能發送到201/202節點上
[root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 201 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 201 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+ [root@pxc_250 ~]# mysql -u sbuser -psbpass -h 127.0.0.1 -P 6033 -e "select @@server_id"; mysql: [Warning] Using a password on the command line interface can be insecure. +-------------+ | @@server_id | +-------------+ | 202 | +-------------+