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;
相关推荐
天意pt14 小时前
Blog-SSR 系统操作手册(v1.0.0)
前端·vue.js·redis·mysql·docker·node.js·express
会跑的葫芦怪14 小时前
cursor 打开wsl Ubuntu项目
linux·运维·ubuntu
用户4672445449915 小时前
linux的网络配置
linux
Lueeee.15 小时前
字符设备驱动---自己实现点LED
linux
Full Stack Developme15 小时前
linux sudo su su - 三者区别
linux·运维·服务器
Byron Loong15 小时前
【系统】Linux内核和发行版的关系
linux·运维·服务器
ChenYY~15 小时前
双系统显卡冲突修复记录
ubuntu·显卡·黑屏·双系统·nvidia驱动
麦麦大数据16 小时前
J009 美食推荐可视化大数据系统vue+springboot
vue.js·spring boot·mysql·推荐算法·美食·可视化分析·沙箱支付
oMcLin16 小时前
如何在 Ubuntu 22.04 LTS 上部署并优化 OpenStack 云计算平台,实现多租户虚拟化与弹性伸缩?
ubuntu·云计算·openstack