标签:dml rds 频率 它的 使用说明 权限 root null 参与
在美团点评DBProxy读写分离使用说明文章中已经说明了使用目的,本文介绍ProxySQL的使用方法以及和DBProxy的性能差异。具体的介绍可以看官网的相关说明,并且这个中间件也是percona推的一款中间件。其特性和其他读写分离的中间件差距不大,具体的会在文中介绍。本文大致简单的介绍在使用过程中的一些说明,也可以看官方的wiki获得使用帮助。
Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 Codename: trusty
percona站点:
github/官网:
1)编译安装:
1:下载 wget https://github.com/sysown/proxysql/archive/v1.3.6.tar.gz 2:安装依赖包 apt-get install automake bzip2 cmake make g++ gcc git openssl debconf-utils 3:解压、编译 make make install ... Adding system startup for /etc/init.d/proxysql ... /etc/rc0.d/K20proxysql -> ../init.d/proxysql /etc/rc1.d/K20proxysql -> ../init.d/proxysql /etc/rc6.d/K20proxysql -> ../init.d/proxysql /etc/rc2.d/S20proxysql -> ../init.d/proxysql /etc/rc3.d/S20proxysql -> ../init.d/proxysql /etc/rc4.d/S20proxysql -> ../init.d/proxysql /etc/rc5.d/S20proxysql -> ../init.d/proxysql 4:修改非root启动 添加用户: useradd proxysql 修改相关文件权限: cd /var/lib/ chown -R proxysql.proxysql proxysql/ cd /etc/ chown -R proxysql.proxysql proxysql.cnf 修改启动命令 vi /etc/init.d/proxysql ->75行 proxysql $OPTS 改成 sudo -u proxysql /usr/bin/proxysql $OPTS 5:启动&关闭 /etc/init.d/proxysql start /etc/init.d/proxysql stop
2)deb包安装:
1:下载 wget https://github.com/sysown/proxysql/releases/download/v1.3.6/proxysql_1.3.6-dbg-ubuntu14_amd64.deb 2:安装 dpkg -i proxysql_1.3.5-1.1.trusty_amd64.deb 3:非root用户启动,参考编译安装部分。
通过启动文件 /etc/init.d/proxysql里可以知道ProxySQL安装好的数据目录在 /var/lib/proxysql/,配置文件目录在 /etc/proxysql.cnf。
root@proxysql:/var/lib/proxysql# pwd /var/lib/proxysql root@proxysql:/var/lib/proxysql# ls -lh total 48K -rw------- 1 proxysql proxysql 33K 5月 9 11:52 proxysql.db -rw------- 1 proxysql proxysql 7.4K 5月 9 11:56 proxysql.log root@proxysql:~# ls -lh /etc/proxysql.cnf -rw------- 1 proxysql proxysql 4.2K 5月 9 11:45 /etc/proxysql.cnf
启动之后,可以看到这些文件:proxysql.db是SQLITE的数据文件,proxysql配置,如后端数据库的账号、密码、路由等存储在这个数据库里面,proxysql.log是日志文件。proxysql.pid这个pid文件。proxysql.cnf是ProxySQL的一些静态配置项,比如一些启动选项,sqlite的数据目录等等。配置文件只在第一次启动的时候读取进行初始化,后面只读取db文件。
root@proxysql:~# ps -ef | grep proxysql proxysql 35740 1 0 12:20 ? 00:00:00 /usr/bin/proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql proxysql 35741 35740 0 12:20 ? 00:00:00 /usr/bin/proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
和MySQL的很相似,我们启动一个进程,然后fork出一个子进程,父进程负责监控子进程运行状况如果挂了则拉起来,子进程负责执行真正的任务。
和DBProxy一样,ProxySQL也是有管理接口和客户端接口,通过配置文件/etc/proxysql.cnf可以看到管理和客户端接口的信息:
admin_variables= { admin_credentials="admin:admin" mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" # refresh_interval=2000 # debug=true } mysql_variables= { ...
... interfaces="0.0.0.0:6033;/tmp/proxysql.sock" default_schema="information_schema" ...
... }
看到管理接口的端口是6032,账号密码是admin(可以动态修改)只能通过本地连接,客户端接口的端口是6033,账号密码通过管理接口去设置。
通过管理接口登录,需要先装好mysql客户端,并且设置prompt
[client]
prompt = \\u@\\h : \\d \\r:\\m:\\s>
通过管理接口进入ProxySQL
root@proxysql:~# mysql -uadmin -padmin -h127.0.0.1 -P6032 ... ... admin@127.0.0.1 : (none) 12:43:08>show databases; +-----+---------+-------------------------------+ | seq | name | file | +-----+---------+-------------------------------+ | 0 | main | | | 2 | disk | /var/lib/proxysql/proxysql.db | | 3 | stats | | | 4 | monitor | | +-----+---------+-------------------------------+ 4 rows in set (0.00 sec)
main
内存配置数据库,表里存放后端db实例、用户验证、路由规则等信息。表名以 runtime_
开头的表示proxysql当前运行的配置内容,不能通过dml语句修改,只能修改对应的不以 runtime_ 开头的(在内存)里的表,然后 LOAD
使其生效, SAVE
使其存到硬盘以供下次重启加载。disk
是持久化到硬盘的配置,sqlite数据文件。stats
是proxysql运行抓取的统计信息,包括到后端各命令的执行次数、流量、processlist、查询种类汇总/执行时间等等。monitor
库存储 monitor 模块收集的信息,主要是对后端db的健康/延迟检查。1)main库:
admin@127.0.0.1 : (none) 02:01:54>show tables from main; +--------------------------------------+ | tables | +--------------------------------------+ | global_variables | | mysql_collations | | mysql_query_rules | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | runtime_global_variables | | runtime_mysql_query_rules | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_scheduler | | scheduler | +--------------------------------------+ 13 rows in set (0.00 sec)
global_variables
设置变量,包括监听的端口、管理账号等,详细可参考 https://github.com/sysown/proxysql/wiki/Global-variables。通过set来设置,如:
set mysql-monitor_ping_interval =10001;
mysql_collations:相关字符集和校验规则。
mysql_query_rules:定义查询路由规则。
CREATE TABLE mysql_query_rules ( rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0, username VARCHAR, schemaname VARCHAR, flagIN INT NOT NULL DEFAULT 0, client_addr VARCHAR, proxy_addr VARCHAR, proxy_port INT, digest VARCHAR, match_digest VARCHAR, match_pattern VARCHAR, negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0, flagOUT INT, replace_pattern VARCHAR, destination_hostgroup INT DEFAULT NULL, cache_ttl INT CHECK(cache_ttl > 0), reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL, timeout INT UNSIGNED, retries INT CHECK (retries>=0 AND retries <=1000), delay INT UNSIGNED, mirror_flagOUT INT UNSIGNED, mirror_hostgroup INT UNSIGNED, error_msg VARCHAR, log INT CHECK (log IN (0,1)), apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0, comment VARCHAR)
mysql_replication_hostgroups:监视指定主机组中所有服务器的read_only值,并且根据read_only的值将服务器分配给写入器或读取器主机组
CREATE TABLE mysql_replication_hostgroups ( writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY, reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>0), comment VARCHAR, UNIQUE (reader_hostgroup))
定义 hostgroup 的主从关系。ProxySQL monitor 模块会监控 HG 后端所有servers 的 read_only
变量,如果发现从库的 read_only 变为0、主库变为1,则认为角色互换了,自动改写 mysql_servers 表里面 hostgroup 关系,达到自动 Failover 效果。
mysql_servers:设置后端MySQL的表
CREATE TABLE mysql_servers ( hostgroup_id INT NOT NULL DEFAULT 0, hostname VARCHAR NOT NULL, port INT NOT NULL DEFAULT 3306, status VARCHAR CHECK (UPPER(status) IN (‘ONLINE‘,‘SHUNNED‘,‘OFFLINE_SOFT‘, ‘OFFLINE_HARD‘)) NOT NULL DEFAULT ‘ONLINE‘, weight INT CHECK (weight >= 0) NOT NULL DEFAULT 1, compression INT CHECK (compression >=0 AND compression <= 102400) NOT NULL DEFAULT 0, max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000, max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0, use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0, max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0, comment VARCHAR NOT NULL DEFAULT ‘‘, PRIMARY KEY (hostgroup_id, hostname, port) )
mysql_users:配置后端数据库的账号和监控的账号
CREATE TABLE mysql_users ( username VARCHAR NOT NULL, password VARCHAR, active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1, use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0, default_hostgroup INT NOT NULL DEFAULT 0, default_schema VARCHAR, schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0, transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 0, fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0, backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1, frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1, max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000, PRIMARY KEY (username, backend), UNIQUE (username, frontend))
scheduler:调度器是一个类似于cron的实现,集成在ProxySQL中,具有毫秒的粒度。通过脚本检测来设置ProxySQL。
CREATE TABLE scheduler ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1, interval_ms INTEGER CHECK (interval_ms>=100 AND interval_ms<=100000000) NOT NULL, filename VARCHAR NOT NULL, arg1 VARCHAR, arg2 VARCHAR, arg3 VARCHAR, arg4 VARCHAR, arg5 VARCHAR, comment VARCHAR NOT NULL DEFAULT ‘‘)
关于main库下的表的说明可以看这篇文章的说明:http://seanlook.com/2017/04/10/mysql-proxysql-install-config/
2)disk库:
show tables from disk; +------------------------------+ | tables | +------------------------------+ | global_variables | | mysql_collations | | mysql_query_rules | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | scheduler | +------------------------------+ 7 rows in set (0.00 sec)
和上面介绍的main库一样。
3)stats库:
stats_mysql_commands_counters:统计各种SQL类型的执行次数和时间,通过参数mysql-commands_stats控制开关,默认是ture。
CREATE TABLE stats_mysql_commands_counters ( Command VARCHAR NOT NULL PRIMARY KEY, Total_Time_us INT NOT NULL, Total_cnt INT NOT NULL, cnt_100us INT NOT NULL, cnt_500us INT NOT NULL, cnt_1ms INT NOT NULL, cnt_5ms INT NOT NULL, cnt_10ms INT NOT NULL, cnt_50ms INT NOT NULL, cnt_100ms INT NOT NULL, cnt_500ms INT NOT NULL, cnt_1s INT NOT NULL, cnt_5s INT NOT NULL, cnt_10s INT NOT NULL, cnt_INFs)
stats_mysql_connection_pool:连接后端MySQL的连接信息。
CREATE TABLE stats_mysql_connection_pool ( hostgroup VARCHAR, srv_host VARCHAR, srv_port VARCHAR, status VARCHAR, ConnUsed INT, ConnFree INT, ConnOK INT, ConnERR INT, Queries INT, Bytes_data_sent INT, Bytes_data_recv INT, Latency_us INT)
stats_mysql_global:代表与MySQL相关的代理级别的全局统计,如:
stats_mysql_processlist:类似MySQL的show processlist的命令,查看各线程的状态。
CREATE TABLE stats_mysql_processlist ( ThreadID INT NOT NULL, SessionID INTEGER PRIMARY KEY, user VARCHAR, db VARCHAR, cli_host VARCHAR, cli_port VARCHAR, hostgroup VARCHAR, l_srv_host VARCHAR, l_srv_port VARCHAR, srv_host VARCHAR, srv_port VARCHAR, command VARCHAR, time_ms INT NOT NULL, info VARCHAR)
stats_mysql_query_digest:表示SQL的执行次数、时间消耗等。通过变量mysql-query_digests控制开关,默认是开。
stats_mysql_query_digest Create Table: CREATE TABLE stats_mysql_query_digest ( hostgroup INT, schemaname VARCHAR NOT NULL, username VARCHAR NOT NULL, digest VARCHAR NOT NULL, digest_text VARCHAR NOT NULL, count_star INTEGER NOT NULL, first_seen INTEGER NOT NULL, last_seen INTEGER NOT NULL, sum_time INTEGER NOT NULL, min_time INTEGER NOT NULL, max_time INTEGER NOT NULL, PRIMARY KEY(hostgroup, schemaname, username, digest))
stats_mysql_query_digest_reset和stats_mysql_query_digest结构一样,stats_mysql_query_digest_reset可以清空stats_mysql_query_digest的数据,如执行:
select * from stats_mysql_query_digest_reset; show create table stats.stats_mysql_query_digest_reset;
执行上面的任意一条就可以清空stats_mysql_query_digest表。
stats_mysql_query_rules:路由命中次数统计。
CREATE TABLE stats_mysql_query_rules ( rule_id INTEGER PRIMARY KEY, hits INT NOT NULL)
4)monitor库: 对后端MySQL的健康检查。由变量mysql-monitor_enabled来确定是否开启Monitor模块。
mysql-monitor_connect_interval来控制其检测
的时间间隔,由参数mysql-monitor_connect_timeout控制连接是否超时(默认200毫秒)。
mysql_server_ping/mysql_server_ping_log:使用mysql_ping API ping后端MySQL服务器检查它们是否可用,该表用来存放ping的日志。由变量mysql-monitor_ping_interval控制ping的时间间隔,默认值:10000(毫秒,相当于10秒)。ProxySQL有一个完备的配置系统,持配置修改之后的在线保存、应用,不需要重启之后生效。整个配置系统分三层设计。配置系统分为三层的目的有三个:
(1).自动更新
(2).尽可能的不重启proxysql就可以修改配置
(3).方便回滚错误配置
整个配置系统分为三层,如下所示:
+-------------------------+ | RUNTIME | +-------------------------+ /|\ | | | [1] | [2] | | \|/ +-------------------------+ | MEMORY | +-------------------------+ _ /|\ | | | | [3] | [4] | \ [5] | \|/ +-------------------------+ +-------------------------+ | DISK | | CONFIG FILE | +-------------------------+ +-------------------------+
RUNTIME 代表的是ProxySQL当前生效的正在使用的配置,包括 global_variables, mysql_servers, mysql_users, mysql_query_rules。无法直接修改这里的配置,必须要从下一层load进来。也就是说RUNTIME这个顶级层,就是proxysql运行过程中实际采用的那一份配置,这一份配置就是要影响实际生产的,所以将你的配置加进RUNTIME层时需要三思而行。
MEMORY 用户可以将MySQL客户端连接到此接口(admin接口),并查询不同的表和数据库是在mysql命令行修改的 main 里头配置,可以认为是SQLite数据库在内存的镜像。也就是说MEMORY这个中间层,上面接着生产配置项RUNTIME,下面接着持久化层DISK,CONFIG FILE。MEMORY也是我们修改proxysql的唯一正常入口。一般的,我们修改一个配置,先改Memory,确认无误后再接入生产(RUNTIME),和持久化到磁盘(DISK)。也就是说memeory里面的配置随便改,不影响生产,也不影响磁盘中保存的数据。通过此接口可以修改mysql_servers、mysql_users、mysql_query_rules、global_variables等。
DISK / CONFIG FILE 持久存储的那份配置,一般在$(DATADIR)/proxysql.db
,在重启的时候会从硬盘里加载。 /etc/proxysql.cnf
文件只在第一次初始化的时候用到,完了后,如果要修改监听端口,还是需要在管理命令行里修改,再 save 到硬盘。 也就是说DISK和CONFIG FILE这一层是持久化层,我们做的任何配置更改,如果不持久化下来,重启后,配置都将丢失。
说明:proxysql的每一个配置项在三层中都存在,但是这三层是独立的,也就是说,proxysql可以同时拥有三份配置,每层都是独立的,可能三份都不一样,可能三份都一样。
当proxysql启动时,首先读取配置文件CONFIG FILE(/etc/proxysql.cnf),然后从该配置文件中获取datadir,datadir中存储的是sqlite的数据目录。如果该目录存在,且sqlite数据文件存在,那么正常启动,将sqlite中的配置项读进内存,并且加载进RUNTIME,用于初始化proxysql的运行。如果datadir目录下没有sqlite的数据文件,proxysql就会使用config file中的配置来初始化proxysql,并且将这些配置保存至数据库。
修改配置:需要修改配置时,直接操作的是 MEMORAY(main)
以下命令可用于加载或保存 users(mysql_users)
: (序号对应上图)
[1]: LOAD MYSQL USERS TO RUNTIME / LOAD MYSQL USERS FROM MEMORY --将修改后的配置(在memory层)用到实际生产 [2]: SAVE MYSQL USERS TO MEMORY / SAVE MYSQL USERS FROM RUNTIME --将生产配置拉一份到memory中 [3]: LOAD MYSQL USERS TO MEMORY / LOAD MYSQL USERS FROM DISK --将磁盘中持久化的配置拉一份到memory中来 [4]: SAVE MYSQL USERS TO DISK / SAVE MYSQL USERS FROM MEMORY --将memoery中的配置保存到磁盘中去 [5]: LOAD MYSQL USERS FROM CONFIG --将配置文件中的配置加载到memeory中
以下命令加载或保存servers(mysql_servers)
:
[1]: LOAD MYSQL SERVERS TO RUNTIME --让修改的配置生效 [2]: SAVE MYSQL SERVERS TO MEMORY [3]: LOAD MYSQL SERVERS TO MEMORY [4]: SAVE MYSQL SERVERS TO DISK --将修改的配置持久化 [5]: LOAD MYSQL SERVERS FROM CONFIG
以下命令加载或保存query rules(mysql_query_rules)
:
[1]: load mysql query rules to run [2]: save mysql query rules to mem [3]: load mysql query rules to mem [4]: save mysql query rules to disk [5]: load mysql query rules from config
以下命令加载或保存 mysql variables(global_variables)
:
[1]: load mysql variables to runtime [2]: save mysql variables to memory [3]: load mysql variables to memory [4]: save mysql variables to disk [5]: load mysql variables from config
以下命令加载或保存admin variables(select * from global_variables where variable_name like ‘admin-%‘)
:
[1]: load admin variables to runtime [2]: save admin variables to memory [3]: load admin variables to memory [4]: save admin variables to disk [5]: load admin variables from config
到此,PrxoySQL的说明已经大致介绍完,更多的信息可以看官方文档介绍和官方wiki说明,后面接着开始测试ProxySQL的相关功能。
1)读写分离
环境:
APP:192.168.200.25、192.168.200.64 DB: M:192.168.200.202 S:192.168.200.132 ProxySQL:192.168.200.24
① 配置后端MySQL。登入ProxySQL,把MySQL主从的信息添加进去。将主库master也就是做写入的节点放到HG 100中,salve节点做读放到HG 1000。
admin@127.0.0.1 : (none) 12:37:05>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(100,‘192.168.200.202‘,3306,1,1000,10,‘test proxysql‘); Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:37:13>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,‘192.168.200.132‘,3306,1,1000,10,‘test proxysql‘); Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:37:19>select * from mysql_servers; +--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------+ | hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------+ | 100 | 192.168.200.202 | 3306 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | test proxysql | | 1000 | 192.168.200.132 | 3306 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | test proxysql | +--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------+ 2 rows in set (0.00 sec)
② 配置后端MySQL用户。这个用户需要先在后端MySQL(202,132)里真实存在,一个是监控账号、一个是程序账号:
-- 监控 dba@192.168.200.202 : 12:38:21>GRANT USAGE ON *.* TO ‘proxysql‘@‘192.168.200.24‘ IDENTIFIED BY ‘proxysql‘; Query OK, 0 rows affected (0.00 sec) -- 程序 dba@192.168.200.202 : 12:38:22>GRANT SELECT, INSERT, UPDATE, DELETE ON `sbtest`.* TO ‘sbuser‘@‘192.168.200.24‘ identified by ‘sbuser‘; Query OK, 0 rows affected (0.00 sec)
在后端MySQL里添加完之后再配置ProxySQL:这里需要注意,default_hostgroup需要和上面的对应。
admin@127.0.0.1 : (none) 12:39:52>insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values(‘sbuser‘,‘sbuser‘,1,100,1); Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:41:07>select * from mysql_users; +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ | sbuser | sbuser | 1 | 0 | 100 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 | +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ 1 row in set (0.00 sec)
上面看到是一个明文密码,也可以用password函数加密后的密码进行代替。
③ 设置健康监测账号:
admin@127.0.0.1 : (none) 12:42:38>UPDATE global_variables SET variable_value=‘proxysql‘ WHERE variable_name=‘mysql-monitor_username‘; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:42:44>UPDATE global_variables SET variable_value=‘proxysql‘ WHERE variable_name=‘mysql-monitor_password‘; Query OK, 1 row affected (0.00 sec) 或则还行下面的: admin@127.0.0.1 : (none) 12:44:24>set mysql-monitor_username=‘proxysql‘; Query OK, 1 row affected (0.01 sec) admin@127.0.0.1 : (none) 12:45:14>set mysql-monitor_password=‘proxysql‘; Query OK, 1 row affected (0.01 sec)
其他变量的修改,按照需要可以自行调整。
admin@127.0.0.1 : (none) 12:45:22>set mysql-default_charset = ‘utf8mb4‘; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:47:38>set mysql-query_retries_on_failure=0; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:48:00>set mysql-eventslog_filename=‘queries.log‘; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:48:20>set mysql-ping_timeout_server=1500; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:48:55>set mysql-monitor_connect_timeout=1000; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:48:55>set mysql-default_max_latency_ms=2000; Query OK, 1 row affected (0.00 sec) admin@127.0.0.1 : (none) 12:48:56>set mysql-server_version=‘5.6.33‘; Query OK, 1 row affected (0.00 sec)
④ 加载配置和变量:因为修改了servers、users和variables,所以加载的时候要执行:
-- 应用到线上 admin@127.0.0.1 : (none) 12:51:28>load mysql servers to runtime; Query OK, 0 rows affected (0.00 sec) admin@127.0.0.1 : (none) 12:56:27>load mysql users to runtime; Query OK, 0 rows affected (0.00 sec) admin@127.0.0.1 : (none) 12:56:31>load mysql variables to runtime; Query OK, 0 rows affected (0.01 sec) -- 持久化 admin@127.0.0.1 : (none) 12:56:37>save mysql servers to disk; Query OK, 0 rows affected (0.01 sec) admin@127.0.0.1 : (none) 12:56:45>save mysql users to disk; Query OK, 0 rows affected (0.00 sec) admin@127.0.0.1 : (none) 12:56:50>save mysql variables to disk; Query OK, 72 rows affected (0.00 sec)
要是是用明文密码设置mysql_users,在这里可以用save命令来转换成了hash值的密码:
admin@127.0.0.1 : (none) 04:25:23>save mysql users to mem; Query OK, 0 rows affected (0.00 sec) admin@127.0.0.1 : (none) 04:25:33>select username,password from mysql_users; +----------+-------------------------------------------+ | username | password | +----------+-------------------------------------------+ | sbuser | *CA96E56547F43610DDE9EB7B12B4EF4C51CDDFFC | +----------+-------------------------------------------+ 1 row in set (0.00 sec)
⑤ 连接数据库,通过proxysql的客户端接口访问(6033):
~$ mysql -usbuser -psbuser -h192.168.200.24 -P6033 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.33 (ProxySQL) ... sbuser@192.168.200.24 : (none) 04:41:34>show databases; +--------------------+ | Database | +--------------------+ | information_schema | | sbtest | +--------------------+ 2 rows in set (0.00 sec)
写入测试数据:
sbuser@192.168.200.24 : sbtest 04:42:35>insert into sb values(1),(2),(3); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 sbuser@192.168.200.24 : sbtest 04:44:12>select * from sb; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec) sbuser@192.168.200.24 : sbtest 04:44:14>begin; Query OK, 0 rows affected (0.00 sec) sbuser@192.168.200.24 : sbtest 04:44:17>insert into sb values(11),(22),(33); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 sbuser@192.168.200.24 : sbtest 04:44:26>commit; Query OK, 0 rows affected (0.00 sec) sbuser@192.168.200.24 : sbtest 04:44:29>select * from sb; +------+ | id | +------+ | 1 | | 2 | | 3 | | 11 | | 22 | | 33 | +------+ 6 rows in set (0.00 sec)
通过proxysql接口正常操作数据,从管理接口看看ProxySQL的统计信息:
-- 查看各类命令的执行情况 admin@127.0.0.1 : (none) 04:52:59>select Command,Total_Time_us,Total_cnt from stats_mysql_commands_counters where Total_cnt >0; +---------+---------------+-----------+ | Command | Total_Time_us | Total_cnt | +---------+---------------+-----------+ | BEGIN | 314 | 1 | | COMMIT | 493 | 1 | | INSERT | 5021 | 2 | | SELECT | 2224 | 9 | | SHOW | 4577 | 5 | +---------+---------------+-----------+
-- 查看各类SQL的执行情况 admin@127.0.0.1 : (none) 04:54:47>select * from stats_mysql_query_digest;
通过上面看到,ProxySQL已经正常启动,但是在stats_mysql_query_digest的hostgroup中发现,读和写全部都是走100这个Master的HG,没有用到从库。主要原因就是ProxySQL的核心mysql_query_rules路由表没有配置。proxysql是通过自定义sql路由规则就可以实现读写分离。
⑥ 定义路由规则,如:除select * from tb for update的select全部发送到slave,其他的的语句发送到master。
-- 发送到M admin@127.0.0.1 : (none) 04:58:11>INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,‘^SELECT.*FOR UPDATE$‘,100,1); Query OK, 1 row affected (0.00 sec) -- 发送到S admin@127.0.0.1 : (none) 05:08:17>INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,‘^SELECT‘,1000,1); Query OK, 1 row affected (0.00 sec) -- 加载 admin@127.0.0.1 : (none) 05:09:37>load mysql query rules to runtime; Query OK, 0 rows affected (0.00 sec) admin@127.0.0.1 : (none) 05:09:57>save mysql query rules to disk; Query OK, 0 rows affected (0.00 sec)
说明:active表示是否启用这个sql路由项,match_pattern就是我们正则匹配项,destination_hostgroup表示我们要将该类sql转发到哪些mysql上面去,apply为1表示该正则匹配后,将不再接受其他匹配,直接转发。具体的信息看上面的表介绍说明。路由规则添加完成,现在来测试下读写分离,先清空stats_mysql_query_digest统计表:
admin@127.0.0.1 : (none) 05:16:21>select * from stats_mysql_query_digest_reset;
写入测试数据:
sbuser@192.168.200.24 : sbtest 10:20:27>insert into sb values(1),(2),(3); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 sbuser@192.168.200.24 : sbtest 10:20:29>select * from sb; ... sbuser@192.168.200.24 : sbtest 10:20:34>start transaction; Query OK, 0 rows affected (0.01 sec) sbuser@192.168.200.24 : sbtest 10:20:41>insert into sb values(11),(22),(33); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 sbuser@192.168.200.24 : sbtest 10:20:46>commit; Query OK, 0 rows affected (0.00 sec) sbuser@192.168.200.24 : sbtest 10:20:51>select * from sb; ... sbuser@192.168.200.24 : sbtest 10:20:56>select * from sb for update; .. sbuser@192.168.200.24 : sbtest 10:21:11>insert into sb values(111),(222),(333); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 sbuser@192.168.200.24 : sbtest 10:21:20>select * from sb; +------+ | id | +------+ | 1 | | 2 | | 3 | | 11 | | 22 | | 33 | | 1 | | 2 | | 3 | | 11 | | 22 | | 33 | | 111 | | 222 | | 333 | +------+ 15 rows in set (0.01 sec)
查看统计信息:
select * from stats_mysql_query_digest;
看到读写分离已经成功。为了更直观一点,使用sysbench测试读写分离是否正常:
./bin/sysbench --test=./share/sysbench/oltp_read_write.lua --mysql-host=192.168.200.24 --mysql-port=6033 --mysql-user=sbuser --mysql-password=sbuser --mysql-db=sbtest --report-interval=10 --max-requests=0 --time=300 --threads=4 --tables=1 --table-size=500000 --skip-trx=on --db-ps-mode=disable --mysql-ignore-errors=1062 prepare/run/clean
为了更好的验证,可以在sysbench运行期间,可以到后端的从MySQL上查看是否有查询进来。执行完之后来看下统计表:
admin@127.0.0.1 : (none) 10:31:52>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest; +-----------+------------+----------+--------------------------------------------------------------------+------------+ | hostgroup | schemaname | username | substr(digest_text,120,-120) | count_star | +-----------+------------+----------+--------------------------------------------------------------------+------------+ | 100 | sbtest | sbuser | show processlist | 15 | | 100 | sbtest | sbuser | INSERT INTO sbtest1 (id, k, c, pad) VALUES (?, ?, ?, ?) | 123932 | | 100 | sbtest | sbuser | DELETE FROM sbtest1 WHERE id=? | 123932 | | 100 | sbtest | sbuser | UPDATE sbtest1 SET c=? WHERE id=? | 123932 | | 100 | sbtest | sbuser | UPDATE sbtest1 SET k=k+? WHERE id=? | 123932 | | 1000 | sbtest | sbuser | SELECT SUM(k) FROM sbtest1 WHERE id BETWEEN ? AND ? | 123932 | | 100 | sbtest | sbuser | show tables | 1 | | 100 | sbtest | sbuser | select * from sb for update | 1 | | 100 | sbtest | sbuser | start transaction | 1 | | 1000 | sbtest | sbuser | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ? ORDER BY c | 123932 | | 1000 | sbtest | sbuser | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ? | 123932 | | 1000 | sbtest | sbuser | SELECT c FROM sbtest1 WHERE id=? | 1239320 | | 1000 | sbtest | sbuser | select * from sb | 3 | | 1000 | sbtest | sbuser | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ? ORDER BY c | 123932 | | 100 | sbtest | sbuser | drop table sbtest1 | 1 | | 100 | sbtest | sbuser | commit | 1 | | 100 | sbtest | sbuser | insert into sb values(?),(?),(?) | 3 | +-----------+------------+----------+--------------------------------------------------------------------+------------+
从上面的结果可知,路由规则已经生效,select语句均到从库上执行了。
本文初步介绍了ProxySQL的安装、相关表说明和读写分离。 后续还有ProxySQL其本身的特性说明:延迟读取主库、重写查询以及和DBProxy对比等,由于篇幅原因,发给下一篇文章里介绍。
https://github.com/sysown/proxysql/wiki
ProxySQL之安装及配置详解 强烈推荐
ProxySQL之读写分离与分库路由演示 强烈推荐
标签:dml rds 频率 它的 使用说明 权限 root null 参与
原文地址:http://www.cnblogs.com/zhoujinyi/p/6829983.html