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;
相关推荐
小杨爱科普34 分钟前
制造业上自动化改造,改造部分的专利到底归谁
运维·经验分享·自动化
陈希1231 小时前
Linux 服务器性能排查手册(2026):CPU、内存、磁盘IO、网络四大瓶颈定位
linux·服务器·网络
在多学一点1 小时前
MYSQL第二次作业
android·数据库·mysql
yeccheng2 小时前
MySQL 索引超详解:从底层 B+ 树到面试高频考点
java·开发语言·jvm·mysql·面试
骑着蜗牛撵大象3272 小时前
MySQL 8.0 迁移后 SQL 变慢:执行计划里 3 类被忽略的成本差异
android·数据库·sql·mysql·优化器·执行计划
方便面不加香菜2 小时前
Docker Container --容器及容器的操作案例详解
运维·docker·容器
测试界的小码农4 小时前
【Linux 命令实战(03):文件创建、复制、移动与删除,掌握 touch、mkdir、cp、mv、rm】
linux·服务器·php
byte轻骑兵5 小时前
实战 1:修改 BlueZ 源码,添加自定义开机日志
linux·aiot·bluez·电脑蓝牙·嵌入式蓝牙
叼烟扛炮5 小时前
Linux 学习笔记(五十):网络层——IP 协议、子网划分与 NAT 魔法
linux·笔记·学习
叼烟扛炮5 小时前
Linux 学习笔记(六十):单线程的逆袭——epoll 高并发服务器实战
linux·服务器·网络·笔记·学习·算法