使用开源ProxySQL构建PolarDB-X标准版高可用路由服务

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 本文将指导如何快速搭建和配置PolarDB-X标准版与ProxySQL,并提供验证高可用路由服务验证测试。

作者:严华

前言

PolarDB-X 作为PolarDB分布式版,是阿里巴巴自主设计研发的高性能云原生分布式数据库产品,采用 Shared-nothing 与存储分离计算架构,支持集中式和分布式一体化形态,具备金融级数据高可用、分布式水平扩展、混合负载、低成本存储和极致弹性等能力,坚定以兼容MySQL开源生态构建分布式能力,为用户提供高吞吐、大存储、低延时、易扩展和超高可用的云时代数据库服务。

2023年10月份,PolarDB-X 开源正式发布V2.3.0版本,重点推出PolarDB-X标准版(集中式形态),将PolarDB-X分布式中的DN节点提供单独服务。支持Paxos协议的多副本模式、lizard分布式事务引擎,采用一主一备一日志的三节点架构,通过Paxos协议多副本同步复制,确保数据的强一致性(RPO=0),可以100%兼容MySQL。

面对PolarDB-X标准版缺乏对应开源Proxy组件的现状,开发者们一直在寻找简便且高效的解决方案。好消息是,ProxySQL作为一款成熟的MySQL中间件,能够无缝对接MySQL协议支持PolarDB-X,并且支持故障切换,动态路由等高可用保障,为我们提供了一个既可用又好用的代理选项。

本文将指导如何快速搭建和配置PolarDB-X标准版与ProxySQL,并提供验证高可用路由服务验证测试。

3.1.jpg

手动部署

PolarDB-X标准版部署


安装PolarDB-X标准版


详情请参考文档《PolarDB-X 开源 | 基于Paxos的MySQL三副本》 PolarDB-X支持多种形态的快速部署能力,可以结合各自需求尽心选择


部署方式 说明 安装工具的快速安装 依赖项
RPM包 零组件依赖,手工快速部署 RPM下载、RPM安装 rpm
PXD 自研快速部署工具,通过yaml文件配置快速部署 PXD安装 python3、docker
K8S 基于k8s operator的快速部署工具 K8S安装 k8s、docker


配置PolarDB-X标准版


1.创建依赖视图,目的让ProxySQL识别PolarDB-X标准版的元数据(Leader、Follower)


CREATE VIEW sys.gr_member_routing_candidate_status AS 
SELECT IF(ROLE='Leader' OR ROLE='Follower', 'YES', 'NO' ) as viable_candidate,
       IF(ROLE <>'Leader', 'YES', 'NO' ) as read_only,
       IF (ROLE = 'Leader', 0, LAST_LOG_INDEX - LAST_APPLY_INDEX) as transactions_behind,
       0 as 'transactions_to_cert' 
FROM information_schema.ALISQL_CLUSTER_LOCAL;
# 创建proxysql的监控账户,ProxySQL运行通用依赖
create user 'proxysql_monitor'@'%' identified with mysql_native_password by '123456';
GRANT SELECT on sys.* to 'proxysql_monitor'@'%';


2.创建测试账户,下面高可用测试依赖


create user 'admin2'@'%' identified with mysql_native_password by '123456';
GRANT all privileges on *.* to 'admin2'@'%';


3.检查配置是否生效


# 本文PolarDB-X标准版的三节点均部署本地,监听端口分别为3301/3302/3303
mysql> select @@port;
+--------+
| @@port |
+--------+
|   3301 |
+--------+
1 row in set (0.00 sec)
mysql> select * from information_schema.ALISQL_CLUSTER_GLOBAL;
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
| SERVER_ID | IP_PORT         | MATCH_INDEX | NEXT_INDEX | ROLE     | HAS_VOTED | FORCE_SYNC | ELECTION_WEIGHT | LEARNER_SOURCE | APPLIED_INDEX | PIPELINING | SEND_APPLIED |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
|         1 | 127.0.0.1:16800 |           1 |          0 | Leader   | Yes       | No         |               5 |              0 |             0 | No         | No           |
|         2 | 127.0.0.1:16801 |           1 |          2 | Follower | Yes       | No         |               5 |              0 |             1 | Yes        | No           |
|         3 | 127.0.0.1:16802 |           1 |          2 | Follower | Yes       | No         |               5 |              0 |             1 | Yes        | No           |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
3 rows in set (0.00 sec)
mysql> select * from sys.gr_member_routing_candidate_status ;
+------------------+-----------+---------------------+----------------------+
| viable_candidate | read_only | transactions_behind | transactions_to_cert |
+------------------+-----------+---------------------+----------------------+
| YES              | NO        |                   0 |                    0 |
+------------------+-----------+---------------------+----------------------+
1 row in set (0.00 sec)
mysql> select User,Host from mysql.user where User in ('admin2', 'proxysql_monitor');
+------------------+------+
| User             | Host |
+------------------+------+
| admin2           | %    |
| proxysql_monitor | %    |
+------------------+------+
2 rows in set (0.01 sec)


ProxySQL部署


安装ProxySQL

ProxySQL文档中有详细的安装教程可以浏览 《ProxygSQL:Introduction》 RPM方式和yum方式都可以安装的,但是当前仅测试验证ProxySQL版本v2.4.8可用(v2.5.0及最新版目前不支持,需要修改ProxySQL代码才能支持) 本文采用RPM方式安装,rpm可以从官网 https://github.com/sysown/proxysql/releases/tag/v2.4.8 下载。 相关命令如下


# rpm下载本地
wget https://github.com/sysown/proxysql/releases/download/v2.4.8/proxysql-2.4.8-1-centos7.x86_64.rpm
# 本地安装
sudo rpm -ivh proxysql-2.4.8-1-centos7.x86_64.rpm --nodeps
# 启动ProxySQL
sudo systemctl start proxysql
# 检查ProxySQL是否启动成功
sudo systemctl status proxysql
● proxysql.service - High Performance Advanced Proxy for MySQL
   Loaded: loaded (/etc/systemd/system/proxysql.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-04-29 00:30:46 CST; 6s ago
  Process: 26825 ExecStart=/usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf $PROXYSQL_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 26828 (proxysql)
    Tasks: 25
   Memory: 14.8M
   CGroup: /system.slice/proxysql.service
           ├─26828 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
           └─26829 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
# 检查ProxySQL端口是否开放
# 6032 是ProxySQL管理端口号
# 6033 是ProxySQL对外服务端口号
# ProxySQL默认用户名和密码都是admin
# ProxySQL默认日志为/var/lib/proxysql/proxysql.log
# ProxySQL默认数据目录为/var/lib/proxysql/
sudo netstat -anlp | grep proxysql
tcp        0      0 0.0.0.0:6032            0.0.0.0:*               LISTEN      26829/proxysql
tcp        0      0 0.0.0.0:6033            0.0.0.0:*               LISTEN      26829/proxysql


配置ProxySQL

PolarDB-X标准版+ProxySQL时的ProxySQL的配置教程, 与MySQL MGR+ProxySQL时的ProxySQL的配置教程基本相同。


1.登陆账户


mysql -uadmin -padmin -h 127.0.0.1 -P 6032 -A


2.检查确保mysql_servers、mysql_group_replication_hostgroups、mysql_query_rules为空


#检查
mysql> select * from mysql_group_replication_hostgroups;
Empty set (0.00 sec)
mysql> select * from mysql_servers;
Empty set (0.00 sec)
mysql> select * from mysql_query_rules;
Empty set (0.00 sec)


3.更新监控账号


UPDATE global_variables SET variable_value='proxysql_monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='123456' WHERE variable_name='mysql-monitor_password';
#检查
mysql> select * from global_variables where variable_name in ('mysql-monitor_username', 'mysql-monitor_password');
+------------------------+------------------+
| variable_name          | variable_value   |
+------------------------+------------------+
| mysql-monitor_password | 123456           |
| mysql-monitor_username | proxysql_monitor |
+------------------------+------------------+
2 rows in set (0.00 sec)


4.添加测试账号


INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('admin2','123456',10);
#检查
mysql> select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | attributes | comment |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| admin2   | 123456   | 1      | 0       | 10                | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |            |         |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
1 row in set (0.00 sec)


5.设置读写组,写组10,备写组20,读组30,离线组40,主节点可作为读节点


INSERT INTO mysql_group_replication_hostgroups (writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active,writer_is_also_reader) VALUES(10,20,30,40,1,1);
# 检查
mysql> select * from mysql_group_replication_hostgroups;
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| writer_hostgroup | backup_writer_hostgroup | reader_hostgroup | offline_hostgroup | active | max_writers | writer_is_also_reader | max_transactions_behind | comment |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| 10               | 20                      | 30               | 40                | 1      | 1           | 1                     | 0                       | NULL    |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
1 row in set (0.00 sec)


6.添加后端mysql_servers,leader节点定义为写组10, follower节点定义为备写库20。 注意这里port为节点的PolarDB-X标准版的监听端口port。


INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'127.0.0.1',3301);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (20,'127.0.0.1',3302);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (20,'127.0.0.1',3303);
#检查
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)


7.配置读写分离规则。对于SELECT FOR UPDATE配置到写库,纯SELECT配置到读库。


INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^select.*for update$',10,1);
INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^select',30,1);
#检查
mysql> select * from mysql_query_rules;
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest | match_pattern        | negate_match_pattern | re_modifiers | flagOUT | replace_pattern | destination_hostgroup | cache_ttl | cache_empty_result | cache_timeout | reconnect | timeout | retries | delay | next_query_flagIN | mirror_flagOUT | mirror_hostgroup | error_msg | OK_msg | sticky_conn | multiplex | gtid_from_hostgroup | log | apply | attributes | comment |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| 1       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | NULL         | ^select.*for update$ | 0                    | CASELESS     | NULL    | NULL            | 10                    | NULL      | NULL               | NULL          | NULL      | NULL    | NULL    | NULL  | NULL              | NULL           | NULL             | NULL      | NULL   | NULL        | NULL      | NULL                | NULL | 1     |            | NULL    |
| 2       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | NULL         | ^select              | 0                    | CASELESS     | NULL    | NULL            | 30                    | NULL      | NULL               | NULL          | NULL      | NULL    | NULL    | NULL  | NULL              | NULL           | NULL             | NULL      | NULL   | NULL        | NULL      | NULL                | NULL | 1     |            | NULL    |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
2 rows in set (0.00 sec)


8.保存配置并载入内存生效


save mysql users to disk;
save mysql servers to disk;
save mysql query rules to disk;
save mysql variables to disk;
save admin variables to disk;
load mysql users to runtime;
load mysql servers to runtime;
load mysql query rules to runtime;
load mysql variables to runtime;
load admin variables to runtime;
#检查
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)


高可用路由验证


相关登陆账户


#proxysql监控登录账户
mysql -uadmin -padmin -h 127.0.0.1 -P 6032 -A
#proxysql测试登录账户
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -A


读写分离


#使用监控账户登录
#检查当前server状态,127.0.0.1:3301提供写库服务,127.0.0.1:3301、127.0.0.1:3301、127.0.0.1:3302、127.0.0.1:3303提供读库服务
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#proxysql测试登录账户,执行下面sql
create database d1;
create table d1.t1(c1 int, c2 varchar(10));
insert into d1.t1 values(1,"hello");
begin;insert into d1.t1 values(2,"world");select * from d1.t1;commit;
select * from d1.t1;
select * from d1.t1 for update;
#使用监控账户登录
#检查实际路由规则
mysql> select hostgroup,digest_text from stats_mysql_query_digest;
+-----------+------------------------------------------+
| hostgroup | digest_text                              |
+-----------+------------------------------------------+
| 10        | select * from d1.t1 for update           |
| 30        | select * from d1.t1                      |
| 10        | insert into d1.t1 values(?,?)            |
| 10        | commit                                   |
| 10        | create table d1.t1(c1 int,c2 varchar(?)) |
| 10        | create database d1                       |
| 10        | begin                                    |
| 10        | select @@version_comment limit ?         |
+-----------+------------------------------------------+
8 rows in set (0.01 sec)
#实际手动测试查看路由目的地,只读查询在读库的三个节点中随机选择,非只读查询只在写库中进行
$mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'select @@port, c1, c2 from d1.t1;'
Warning: Using a password on the command line interface can be insecure.
+--------+------+-------+
| @@port | c1   | c2    |
+--------+------+-------+
|   3301 |    1 | hello |
|   3301 |    2 | world |
+--------+------+-------+
$mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e \
'select @@port, c1, c2 from d1.t1;'
Warning: Using a password on the command line interface can be insecure.
+--------+------+-------+
| @@port | c1   | c2    |
+--------+------+-------+
|   3303 |    1 | hello |
|   3303 |    2 | world |
+--------+------+-------+
$mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e \
'select @@port, c1, c2 from d1.t1;'
Warning: Using a password on the command line interface can be insecure.
+--------+------+-------+
| @@port | c1   | c2    |
+--------+------+-------+
|   3302 |    1 | hello |
|   3302 |    2 | world |
+--------+------+-------+
$mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e \
'select @@port, c1, c2 from d1.t1 for update;'
Warning: Using a password on the command line interface can be insecure.
+--------+------+-------+
| @@port | c1   | c2    |
+--------+------+-------+
|   3301 |    1 | hello |
|   3301 |    2 | world |
+--------+------+-------+


主节点故障


手动将当前leader节点kill掉,观察集群可用性变化


#使用监控账户登录
#leader最开始是127.0.0.1:3301节点,kill leader前, 127.0.0.1:3301是写组10
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#执行检查sql, 请求会转发到leader3301上了
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'begin;select @@port,User from mysql.user for update;commit;'
Warning: Using a password on the command line interface can be insecure.
+--------+------------------+
| @@port | User             |
+--------+------------------+
|   3303 | admin2           |
|   3303 | proxysql_monitor |
|   3303 | mysql.infoschema |
|   3303 | mysql.session    |
|   3303 | mysql.sys        |
|   3303 | root             |
+--------+------------------+
#kill leader,127.0.0.1:3301节点立刻退化为离线组40,并随后状态更新为SHUNNED,而 127.0.0.1:3303节点进化为写组10
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)
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 |
+--------------+-----------+------+-----------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3303 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 3301 | 0         | SHUNNED | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#执行检查sql, 请求转发到新leader3303上
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'begin;select @@port,User from mysql.user for update;commit;'
Warning: Using a password on the command line interface can be insecure.
+--------+------------------+
| @@port | User             |
+--------+------------------+
|   3303 | admin2           |
|   3303 | proxysql_monitor |
|   3303 | mysql.infoschema |
|   3303 | mysql.session    |
|   3303 | mysql.sys        |
|   3303 | root             |
+--------+------------------+


前主节点恢复


手动将已经挂掉的前主恢复起来,观察集群状态变化


#使用监控账户登录
#127.0.0.1:3301是已经挂掉的前主, 127.0.0.1:3303是当选的新主
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 |
+--------------+-----------+------+-----------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3303 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE  | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 3301 | 0         | SHUNNED | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#执行检查sql, 请求转发到新leader3303上
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'begin;select @@port,User from mysql.user for update;commit;'
Warning: Using a password on the command line interface can be insecure.
+--------+------------------+
| @@port | User             |
+--------+------------------+
|   3303 | admin2           |
|   3303 | proxysql_monitor |
|   3303 | mysql.infoschema |
|   3303 | mysql.session    |
|   3303 | mysql.sys        |
|   3303 | root             |
+--------+------------------+
# 重启恢复前主节点127.0.0.1:3301,观察节点进化到只读组30,并且状态更新为ONLINE
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#执行写事务可以正确路由到leader
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'begin;select @@port,User from mysql.user for update;commit;'
Warning: Using a password on the command line interface can be insecure.
+--------+------------------+
| @@port | User             |
+--------+------------------+
|   3303 | admin2           |
|   3303 | proxysql_monitor |
|   3303 | mysql.infoschema |
|   3303 | mysql.session    |
|   3303 | mysql.sys        |
|   3303 | root             |
+--------+------------------+


在线切主


手动进行在线切主,观察集群状态变化


#使用测试账户登录,执行sql进行手动切主
#切主前,127.0.0.1:3303节点是leader
mysql> select * from information_schema.ALISQL_CLUSTER_GLOBAL;
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
| SERVER_ID | IP_PORT         | MATCH_INDEX | NEXT_INDEX | ROLE     | HAS_VOTED | FORCE_SYNC | ELECTION_WEIGHT | LEARNER_SOURCE | APPLIED_INDEX | PIPELINING | SEND_APPLIED |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
|         1 | 127.0.0.1:16800 |          24 |         25 | Follower | No        | No         |               5 |              0 |            24 | Yes        | No           |
|         2 | 127.0.0.1:16801 |          24 |         25 | Follower | Yes       | No         |               5 |              0 |            24 | Yes        | No           |
|         3 | 127.0.0.1:16802 |          24 |         0  | Leader   | Yes       | No         |               5 |              0 |            23 | No         | No           |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
3 rows in set (0.00 sec)
mysql> call dbms_consensus.change_leader("127.0.0.1:16801");
Query OK, 0 rows affected (0.00 sec)
mysql> select * from information_schema.ALISQL_CLUSTER_GLOBAL;
+-----------+---------+-------------+------------+------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
| SERVER_ID | IP_PORT | MATCH_INDEX | NEXT_INDEX | ROLE | HAS_VOTED | FORCE_SYNC | ELECTION_WEIGHT | LEARNER_SOURCE | APPLIED_INDEX | PIPELINING | SEND_APPLIED |
+-----------+---------+-------------+------------+------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
|         0 |         |           0 |          0 |      |           |            |               0 |              0 |             0 |            |              |
+-----------+---------+-------------+------------+------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
1 row in set (0.00 sec)
#手动将leader切到127.0.0.1:3302节点后
mysql> select * from information_schema.ALISQL_CLUSTER_GLOBAL;
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
| SERVER_ID | IP_PORT         | MATCH_INDEX | NEXT_INDEX | ROLE     | HAS_VOTED | FORCE_SYNC | ELECTION_WEIGHT | LEARNER_SOURCE | APPLIED_INDEX | PIPELINING | SEND_APPLIED |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
|         1 | 127.0.0.1:16800 |          25 |         26 | Follower | Yes       | No         |               5 |              0 |            25 | Yes        | No           |
|         2 | 127.0.0.1:16801 |          25 |          0 | Leader   | Yes       | No         |               5 |              0 |            24 | No         | No           |
|         3 | 127.0.0.1:16802 |          25 |         26 | Follower | Yes       | No         |               5 |              0 |            25 | Yes        | No           |
+-----------+-----------------+-------------+------------+----------+-----------+------------+-----------------+----------------+---------------+------------+--------------+
3 rows in set (0.01 sec)
#使用监控账户登录
#切主前,127.0.0.1:3303节点是leader
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#手动将leader切到127.0.0.1:3302节点后, proxysql及时响应,127.0.0.1:3302变成了写组
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#127.0.0.1:3303经历过短暂离线组40状态后变成读组30
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#执行写事务可以正确路由到新主
mysql -uadmin2 -p'123456' -h127.0.0.1 -P6033 -e 'begin;select @@port,User from mysql.user for update;commit;'
Warning: Using a password on the command line interface can be insecure.
+--------+------------------+
| @@port | User             |
+--------+------------------+
|   3302 | admin2           |
|   3302 | proxysql_monitor |
|   3302 | mysql.infoschema |
|   3302 | mysql.session    |
|   3302 | mysql.sys        |
|   3302 | root             |
+--------+------------------+


备节点故障恢复


#使用监控账户登录
#备节点故障前状态,此时127.0.0.1:3301为备库节点
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#将127.0.0.1:3301节点杀掉, proxysql很快更新其到离线组40
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)
#将127.0.0.1:3301节点恢复, proxysql很快更新其到读组30
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 |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3301 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3302 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 3303 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.01 sec)


总结

通过上述步骤,我们成功地将ProxySQL配置为PolarDB-X标准版的代理,不仅提升了数据库系统的可维护性和灵活性,也为开发者提供了一个更为便捷的接入方式。现在,无论是应对高并发访问,还是实现复杂的数据路由策略,PolarDB-X标准版 + ProxySQL的组合都能轻松应对,助力开发者高效构建云原生应用。




数据库PolarDB-X新人入门一站式页面,快速体验集中分布式一体化新特性!

https://www.aliyun.com/database/polardb-x-new

相关实践学习
跟我学:如何一键安装部署 PolarDB-X
《PolarDB-X 动手实践》系列第一期,体验如何一键安装部署 PolarDB-X。
相关文章
|
18天前
|
关系型数据库 分布式数据库 数据库
【PolarDB开源】PolarDB资源隔离技术:在多租户环境中的应用与优化
【5月更文挑战第29天】PolarDB,阿里云的云原生数据库,在多租户环境中通过逻辑(Schema/Partition隔离)和物理(分布式存储计算节点)隔离保障数据安全和资源独占。它支持动态资源分配,适应不同租户需求,处理大规模并发,提供租户管理及数据访问控制功能。通过优化资源分配算法、提升事务处理能力和强化监控告警,PolarDB确保性能和稳定性,满足多租户的高效数据库服务需求。
56 1
|
5天前
|
关系型数据库 数据管理 数据库
数据管理DMS产品使用合集之如何极速恢复RDS(关系型数据库服务)中的数据表
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
11天前
|
Ubuntu 关系型数据库 分布式数据库
开源PolarDB -X 部署安装
本文记录了在Ubuntu 20.04上部署阿里云分布式数据库PolarDB-X的步骤,包括环境准备、安装依赖、下载源码、编译安装、配置启动,并分享了遇到的配置错误、依赖冲突和日志不清等问题。作者建议官方改进文档细节、优化代码质量和建立开发者社区。安装历史记录显示了相关命令行操作。尽管过程有挑战,但作者期待产品体验的提升。
165 6
|
12天前
|
存储 弹性计算 关系型数据库
PolarDB 开源评测
摘要: 本文介绍了开源PolarDB-X的部署安装步骤,包括下载安装包、环境准备、配置参数、初始化数据库和启动服务。在安装过程中可能遇到的难题有依赖库缺失、配置错误、端口占用和权限问题。建议优化帮助文档、增加错误提示、自动检查端口和改进权限管理。安装命令示例包括wget下载、tar解压、配置参数和启动服务。 另外,还简述了开源PolarDB-PG在阿里云ECS+ESSD云盘共享存储的安装,涉及创建ECS实例、安装PostgreSQL、配置共享存储和部署PolarDB-PG。面临挑战包括网络配置、存储性能和数据同步。建议提供云环境部署指南、性能调优工具和数据同步监控功能。
|
12天前
|
关系型数据库 MySQL 分布式数据库
PolarDB 开源评测
阿里云PolarDB,一款分布式云原生数据库,以其高性能(交易性能6倍于开源DB,分析性能高达400倍)、强可扩展性(秒级弹性伸缩)、良好兼容性(100%适配MySQL/PostgreSQL,高度兼容Oracle)和易用性(丰富的监控管理功能,灵活备份恢复)脱颖而出。它是应对高并发业务和突发流量的理想选择,尤其适合寻求高性能、高可用和高扩展性的企业。
43 2
|
18天前
|
Cloud Native 关系型数据库 分布式数据库
【PolarDB开源】PolarDB开源项目未来展望:技术趋势与社区发展方向
【5月更文挑战第29天】PolarDB,阿里云的云原生分布式数据库,正聚焦于云原生、容器化、HTAP与实时分析、智能化运维等技术趋势,旨在提升资源利用率、扩展性及数据分析能力。未来,项目将深化全球开源社区合作,拓宽应用场景,构建开发者生态,以创新技术驱动数据库领域发展,目标成为领先的云数据库服务。
73 1
|
18天前
|
Kubernetes 关系型数据库 分布式数据库
【PolarDB开源】PolarDB在混合云环境下的部署策略与挑战
【5月更文挑战第29天】PolarDB,阿里云的高性能分布式数据库,应对混合云环境的部署策略包括:灵活的架构选择(如Kubernetes)、跨云平台数据实时同步及内置安全特性保障数据安全。然而,也面临复杂性、成本优化和运维难度等挑战。示例展示了使用Kubernetes StatefulSet部署PolarDB的基本配置。
223 3
|
18天前
|
人工智能 关系型数据库 分布式数据库
【PolarDB 开源】PolarDB 与 AI 融合:智能数据库管理与预测性维护
【5月更文挑战第28天】PolarDB结合AI,开创数据库管理新纪元,实现智能优化、资源预测与分配、预测性维护。通过AI算法提升查询效率,动态调整资源,提前发现故障,增强安全。示例代码显示如何用AI预测查询时间。面对挑战,持续学习改进,未来二者融合将为数据库管理带来更多创新与竞争力。
113 0
|
18天前
|
安全 关系型数据库 分布式数据库
【PolarDB 开源】PolarDB 在金融行业中的实践:高可用与安全合规解决方案
【5月更文挑战第28天】PolarDB,一款适用于金融行业的强大数据库,以其高可用性和安全合规性脱颖而出。通过多副本机制和自动故障转移确保业务连续性,结合严格的访问控制和数据加密技术保护信息安全。在实际应用中,如银行核心系统,PolarDB 负责处理海量交易数据,同时支持主从架构以备故障切换。此外,设置强密码策略和加密存储确保合规性,并通过监控预警及时解决问题。随着金融科技发展,PolarDB 将在云原生架构和人工智能等领域发挥更大作用,助力金融行业创新与进步。
101 0
|
18天前
|
存储 关系型数据库 分布式数据库
【PolarDB 开源】PolarDB 数据备份与恢复策略:确保数据安全的最后一道防线
【5月更文挑战第28天】PolarDB 开源数据安全的关键:备份与恢复策略。定期备份(全量+增量)降低数据丢失风险,制定策略考虑多种因素。确保备份存储安全,测试恢复流程有效性,结合监控系统及时处理问题。保留多版本备份,应对灾难情况有预案。培训人员,提高数据安全意识,打造稳固防线。
159 0

相关产品

  • 云原生分布式数据库 PolarDB-X
  • 云原生数据库 PolarDB