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;
相关推荐
城北徐宫2 小时前
MySQL基础与库、表操作
数据库·mysql
Johny_Zhao2 小时前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
FlightYe4 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
by209995 小时前
Linux 文件描述符与重定向
linux·服务器
橙橙笔记5 小时前
Linux基础与命令的学习
linux·运维·服务器·shell
无奈笑天下5 小时前
银河麒麟桌面操作系统【命令行配置文件与图片搜索功能】操作方法
linux·运维·服务器
飞花漫卷,情随岁迁6 小时前
网络数据包转发与内核机制:小白概念科普
linux·运维·网络·数据库·mysql·oracle·智能路由器
FungLeo6 小时前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
.道阻且长.7 小时前
Linux基础IO:理解文件以及回顾C文件接口
linux·运维·服务器·c语言·开发语言·职场和发展
学以智用9 小时前
CentOS 目录穿梭
linux