ubuntu24 安装 proxsql 实现数据库代理

Ubuntu 24 系统优化的完整 ProxySQL 安装和配置方案,整合了最佳实践和关键配置细节:

版本搭配:

proxy-3.0

mysql-8.0.36

ProxySQL 安装(Ubuntu 24 专属方案)

添加官方仓库并安装

添加密钥(使用 gpg 替代已弃用的 apt-key)

复制代码
curl -s https://repo.proxysql.com/ProxySQL/repo_pub_key | sudo gpg --dearmor -o /usr/share/keyrings/proxysql.gpg

添加适配 Ubuntu 24 的仓库源

复制代码
echo "deb [signed-by=/usr/share/keyrings/proxysql.gpg] https://repo.proxysql.com/ProxySQL/proxysql-3.0.x/ubuntu $(lsb_release -sc) ./" | sudo tee /etc/apt/sources.list.d/proxysql.list

安装 ProxySQL

复制代码
sudo apt update 
sudo apt install proxysql -y

启动服务

复制代码
sudo systemctl start proxysql 
sudo systemctl enable proxysql

验证安装

复制代码
proxysql --version
sudo systemctl status proxysql

MySQL 8 配置(后端数据库)

修改 MySQL 监听地址

复制代码
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld] 
bind-address = 0.0.0.0 # 允许远程连接

创建专用用户(在 MySQL 执行)

复制代码
-- 监控用户(ProxySQL 专用) 
CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor'; 
GRANT USAGE ON . TO 'monitor'@'%';

-- 访问用户 
CREATE USER 'app_user'@'%' IDENTIFIED BY 'AppPass456!'; 
GRANT SELECT, INSERT, UPDATE, DELETE ON your_db.* TO 'app_user'@'%'; 
FLUSH PRIVILEGES;

重启 MySQL

复制代码
sudo systemctl restart mysql

ProxySQL 核心配置

登录管理界面

复制代码
mysql -u admin -p -h 127.0.0.1 -P 6032

# 初始密码:admin/admin

配置后端服务器

复制代码
root@mzi:~# mysql -u admin -p -h 127.0.0.1 -P 6032
Enter password: 
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, 2024, 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.

mysql> INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (10, '127.0.0.1', 3306);
Query OK, 1 row affected (0.00 sec)

mysql> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
Query OK, 1 row affected (0.00 sec)

mysql> UPDATE global_variables SET  variable_value='monitor' WHERE variable_name='mysql-monitor_password';
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO mysql_users(username, password, default_hostgroup, active) VALUES ('', 'app_pass', 10, 1);
Query OK, 1 row affected (0.00 sec)

mysql> UPDATE global_variables SET variable_value='0.0.0.0:6033' WHERE variable_name='mysql-interfaces';
Query OK, 1 row affected (0.01 sec)

mysql> LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)

mysql> LOAD MYSQL USERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

mysql> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

mysql> SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.57 sec)

mysql> SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.18 sec)

mysql> SAVE MYSQL VARIABLES TO DISK;
Query OK, 171 rows affected (0.04 sec)

连接测试

通过 ProxySQL 连接

复制代码
mysql -u app_user -p -h 127.0.0.1 -P 6033 -e "SHOW DATABASES;"

直接查看路由状态

复制代码
mysql -u admin -p -h 127.0.0.1 -P 6032 -e "SELECT * FROM stats_mysql_connection_pool"

读写分离配置示例(扩展)

复制代码
-- 添加读服务器(hostgroup 20) 
INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (20, 'slave-ip', 3306);

-- 配置路由规则 
INSERT INTO mysql_query_rules(rule_id, active, match_pattern, destination_hostgroup, apply) VALUES (1, 1, '^SELECT', 20, 1), -- 读操作 (2, 1, '.*', 10, 1); -- 其他操作到写组

LOAD MYSQL QUERY RULES TO RUNTIME; 
SAVE MYSQL QUERY RULES TO DISK;
相关推荐
MMME~1 小时前
Ansible模块速查指南:高效定位与实战技巧
大数据·运维·数据库
zhengfei6111 小时前
AutoPentestX – Linux 自动化渗透测试和漏洞报告工具
linux·运维·自动化
我材不敲代码1 小时前
在Linux系统上安装MySQL
linux·运维·服务器
obboda1 小时前
CICD 部署与使用
运维
yuezhilangniao1 小时前
阿里云服务器Alibaba Cloud Linux 3 安装Python3.11简明指南
linux·运维·python3.11
程序 代码狂人1 小时前
CentOS7初始化配置操作
linux·运维·开发语言·php
芋圆奶绿,要半t1 小时前
ubuntu20/2204修改系统时间的命令
ubuntu
lcx_defender1 小时前
【Docker】Docker部署运行nacos
运维·docker·容器
历程里程碑1 小时前
Linux15 进程二
linux·运维·服务器·开发语言·数据结构·c++·笔记
H Journey2 小时前
Linux su 命令核心用法总结
java·linux·服务器·su