十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎。
官方文档:https://github.com/sysown/proxysql/wiki/
下载地址:https://github.com/sysown/proxysql/releases/
创新互联公司专业为企业提供丰县网站建设、丰县做网站、丰县网站设计、丰县网站制作等企业网站建设、网页设计与制作、丰县企业网站模板建站服务,十余年丰县做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
# 配ProxySQL源
[root@ProxySQL ~]# cat <
github
官网
[root@ProxySQL ~]# egrep -v "^#|^$" /etc/proxysql.cnf
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin" # 定义连接管理端口的用户名和密码
mysql_ifaces="0.0.0.0:6032" # 定义管理端口6032;用来连接proxysql的管理数据库,修改proxysql服务的设置以及路由策略
}
mysql_variables=
{
threads=4 # 定义每个转发端口开启多少个线程
max_connections=2048
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:6033" # 定义转发端口6033;用来连接后端的mysql实例,起到代理转发的作用;
default_schema="information_schema"
stacksize=1048576
server_version="5.7.22" # 设置后端mysql实例的版本号,仅起到comment的作用
connect_timeout_server=3000
monitor_username="monitor"
monitor_password="monitor"
monitor_history=600000
monitor_connect_interval=60000
monitor_ping_interval=10000
monitor_read_only_interval=1500
monitor_read_only_timeout=500
ping_interval_server_msec=120000
ping_timeout_server=500
commands_stats=true
sessions_sort=true
connect_retries_on_failure=10
}
mysql_servers =
(
)
mysql_users:
(
)
mysql_query_rules:
(
)
scheduler=
(
)
mysql_replication_hostgroups=
(
)
[root@ProxySQL ~]# sed -i 's#5.5.30#5.7.22#g' /etc/proxysql.cnf # 把5.5.30改为自己的版本
[root@ProxySQL ~]# chkconfig proxysql on # 添加到开机自启动,默认已添加
[root@ProxySQL ~]# chkconfig --list |grep proxysql # 查看是否开机自启动
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
proxysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
默认情况下,rpm安装的ProxySQL只提供了SysV风格的服务脚本/etc/init.d/proxysql。
所以,可通过该脚本管理ProxySQL的启动、停止等功能。
[root@ProxySQL ~]# /etc/init.d/proxysql --help
Usage: ProxySQL {start|stop|status|reload|restart|initial}
# 启动
[root@ProxySQL ~]# service proxysql start
Starting ProxySQL: DONE!
# 查看
[root@tcloud-113 ~]# service proxysql status
ProxySQL is running (30422).
# 启动后会监听两个端口,默认为6032和6033。6032端口是ProxySQL的管理端口,6033是ProxySQL对外提供服务的端口。
[root@ProxySQL ~]# ss -lntup |grep proxysql
tcp LISTEN 0 128 *:6032 *:* users:(("proxysql",1322,20))
tcp LISTEN 0 128 *:6033 *:* users:(("proxysql",1322,19))
tcp LISTEN 0 128 *:6033 *:* users:(("proxysql",1322,18))
tcp LISTEN 0 128 *:6033 *:* users:(("proxysql",1322,17))
tcp LISTEN 0 128 *:6033 *:* users:(("proxysql",1322,16))
# 可以看到转发端口的6033开启了4个线程,线程数由全局变量"threads"控制,受cpu物理核心数的影响(每个端口下的线程数<=cpu物理核心数)
如果想要通过systemd管理ProxySQL,可在/usr/lib/systemd/system/proxysql.service中写入如下内容:
[root@ProxySQL ~]# vim /usr/lib/systemd/system/proxysql.service
[Unit]
Description=High Performance Advanced Proxy for MySQL
After=network.target
[Service]
Type=simple
User=mysql
Group=mysql
PermissionsStartOnly=true
LimitNOFILE=102400
LimitCORE=1073741824
ExecStartPre=/bin/mkdir -p /var/lib/proxysql
ExecStartPre=/bin/chown mysql:mysql -R /var/lib/proxysql /etc/proxysql.cnf
ExecStart=/usr/bin/proxysql -f
Restart=always
[root@ProxySQL ~]#
一般来说,ProxySQL很少停止或重启,因为绝大多数配置都可以在线修改。
例如:
user:proxysql;
password:pwproxysql
mysql> GRANT ALL ON *.* TO 'proxysql'@'192.168.1.%' IDENTIFIED BY 'pwproxysql';
[root@ProxySQL ~]# yum install mysql -y # 安装mysql客户端命令;依赖:mysql-libs
[root@ProxySQL ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@ProxySQL ~]# mysql -uadmin -padmin -h227.0.0.1 -P6032 # 默认的用户名密码都是 admin。
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, 2013, Oracle and/or its affiliates. All rights reserved.
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.
(admin@127.0.0.1:6032) [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.00 sec)
库说明:
(admin@127.0.0.1:6032) [(none)]> show tables from main;
+--------------------------------------------+
| tables |
+--------------------------------------------+
| global_variables | # ProxySQL的基本配置参数,类似与MySQL
| mysql_collations | # 配置对MySQL字符集的支持
| mysql_group_replication_hostgroups | # MGR相关的表,用于实例的读写组自动分配
| mysql_query_rules | # 路由表
| mysql_query_rules_fast_routing | # 主从复制相关的表,用于实例的读写组自动分配
| mysql_replication_hostgroups | # 存储MySQL实例的信息
| mysql_servers | # 现阶段存储MySQL用户,当然以后有前后端账号分离的设想
| mysql_users | # 存储ProxySQL的信息,用于ProxySQL Cluster同步
| proxysql_servers | # 运行环境的存储校验值
| runtime_checksums_values | #
| runtime_global_variables | #
| runtime_mysql_group_replication_hostgroups | #
| runtime_mysql_query_rules | #
| runtime_mysql_query_rules_fast_routing | #
| runtime_mysql_replication_hostgroups | # 与上面对应,但是运行环境正在使用的配置
| runtime_mysql_servers | #
| runtime_mysql_users | #
| runtime_proxysql_servers | #
| runtime_scheduler | #
| scheduler | # 定时任务表
+--------------------------------------------+
20 rows in set (0.00 sec)
runtime_开头的是运行时的配置,这些是不能修改的。要修改ProxySQL的配置,需要修改了非runtime_表,修改后必须执行LOAD ... TO RUNTIME才能加载到RUNTIME生效,执行save ... to disk才能将配置持久化保存到磁盘。
下面语句中没有先切换到main库也执行成功了,因为ProxySQL内部使用的SQLite3数据库引擎,和MySQL的解析方式是不一样的。即使执行了USE main语句也是无任何效果的,但不会报错。
使用insert语句添加mysql主机到mysql_servers表中,其中:hostgroup_id 1 表示写组,2表示读组。
(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.1.102',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.1.103',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> select * from mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 1 | 192.168.1.102 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |
| 2 | 192.168.1.103 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
2 rows in set (0.00 sec)
修改后,加载到RUNTIME,并保存到disk。
(admin@127.0.0.1:6032) [(none)]> load mysql servers to runtime;
(admin@127.0.0.1:6032) [(none)]> save mysql servers to disk;
在proxysql主机的mysql_users表中添加刚才创建的账号,proxysql客户端需要使用这个账号来访问数据库。
default_hostgroup默认组设置为写组,也就是1;
当读写分离的路由规则不符合时,会访问默认组的数据库;
(admin@127.0.0.1:6032) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','pwproxysql',1,1);
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
username: proxysql # 后端mysql实例的用户名
password: pwproxysql # 后端mysql实例的密码
active: 1 # active=1表示用户生效,0表示不生效
use_ssl: 0
default_hostgroup: 1 # 用户默认登录到哪个hostgroup_id下的实例
default_schema: NULL # 用户默认登录后端mysql实例时连接的数据库,这个地方为NULL的话,则由全局变量mysql-default_schema决定,默认是information_schema
schema_locked: 0
transaction_persistent: 1 # 如果设置为1,连接上ProxySQL的会话后,如果在一个hostgroup上开启了事务,那么后续的sql都继续维持在这个hostgroup上,不伦是否会匹配上其它路由规则,直到事务结束。虽然默认是0
fast_forward: 0 # 忽略查询重写/缓存层,直接把这个用户的请求透传到后端DB。相当于只用它的连接池功能,一般不用,路由规则 .* 就行了
backend: 1
frontend: 1
max_connections: 10000 # #该用户允许的最大连接数
1 row in set (0.00 sec)
修改后,加载到RUNTIME,并保存到disk。
(admin@127.0.0.1:6032) [(none)]> load mysql users to runtime;
(admin@127.0.0.1:6032) [(none)]> save mysql users to disk;
首先在后端master节点上创建一个用于监控的用户名(只需在master上创建即可,因为会复制到slave上),这个用户名只需具有USAGE权限即可。如果还需要监控复制结构中slave是否严重延迟于master(先混个眼熟:这个俗语叫做"拖后腿",术语叫做"replication lag"),则还需具备replication client权限。这里直接赋予这个权限。
mysql> GRANT replication client ON *.* TO 'monitor'@'192.168.1.%' IDENTIFIED BY 'monitor';
(admin@127.0.0.1:6032) [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec)
以上设置实际上是在修改global_variables表,它和下面两个语句是等价的:
(admin@127.0.0.1:6032) [(none)]> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_password';
Query OK, 1 row affected (0.00 sec)
修改后,加载到RUNTIME,并保存到disk。
(admin@127.0.0.1:6032) [(none)]> load mysql variables to runtime;
(admin@127.0.0.1:6032) [(none)]> save mysql variables to disk;
(admin@127.0.0.1:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1);
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1);
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^SELECT.*FOR UPDATE$ | 1 | 1 |
| 2 | 1 | ^SELECT | 2 | 1 |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> load mysql query rules to runtime;
(admin@127.0.0.1:6032) [(none)]> load admin variables to runtime;
(admin@127.0.0.1:6032) [(none)]> save mysql query rules to disk;
(admin@127.0.0.1:6032) [(none)]> save admin variables to disk;
登录用户是刚才我们在mysql_user表中创建的用户,端口为6033
[root@centos7 ~]#mysql -uproxysql -ppwproxysql -h227.0.0.1 -P6033
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MySQL [(none)]>
创建两个数据库和查个表。
MySQL [(none)]> create database bigboss;
Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> create database weijinyun;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bigboss |
| mysql |
| performance_schema |
| test |
| weijinyun |
+--------------------+
6 rows in set (0.01 sec)
MySQL [(none)]> select user,host from mysql.user;
+-------------+---------------+
| user | host |
+-------------+---------------+
| root | 127.0.0.1 |
| monitor | 192.168.1.% |
| proxysql | 192.168.1.% |
| repliaction | 192.168.1.% |
| root | ::1 |
| | centos7 |
| root | centos7 |
| | localhost |
| root | localhost |
+-------------+---------------+
9 rows in set (0.01 sec)
(admin@127.0.0.1:6032) [(none)]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time| max_time |
+-----------+--------------------+----------+--------------------+----------------------------------------+------------+------------+------------+----------+----------+----------+
| 2 | information_schema | proxysql | 0x3EA85877510AC608 | select * from stats_mysql_query_digest | 2 | 1527233735 | 1527233782 | 4092 | 792| 3300 |
| 1 | information_schema | proxysql | 0x594F2C744B698066 | select USER() | 1 | 1527233378 | 1527233378 | 0 | 0| 0 |
| 1 | information_schema | proxysql | 0x02033E45904D3DF0 | show databases | 2 | 1527233202 | 1527233495 | 5950 | 1974| 3976 |
| 1 | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 2 | 1527233196 | 1527233378 | 0 | 0| 0 |
+-----------+--------------------+----------+--------------------+----------------------------------------+------------+------------+------------+----------+----------+----------+
4 rows in set (0.00 sec)
(admin@127.0.0.1:6032) [(none)]>
读写分离成功!!!
https://blog.51cto.com/bigboss/2103290
http://www.cnblogs.com/f-ck-need-u/p/7586194.html#middleware
http://seanlook.com/2017/04/10/mysql-proxysql-install-config/