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;