mysql 实现读写分离的方式有以下几种:
ProxySQL 是一款可以实际用于生产环境的 MySQL 中间件,它有官方版和 percona 版两种。percona版是在官方版的基础上修改的,添加了几个比较实用的工具。生产环境建议用官方版。
ProxySQL 是用 C++ 语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,包括:
//配置yum源
[root@xiaoxiong ~]# cat <
ProxySQL 的 admin 管理接口是一个使用 MySQL 协议的接口,所以,可以直接使用 mysql 客户端、navicat 等工具去连接这个管理接口,其默认的用户名和密码均为 admin
例如,使用 mysql 客户端去连接 ProxySQL 的管理接口:
[root@xiaoxiong ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(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)
由于 ProxySQL 的配置全部保存在几个自带的库中,所以通过管理接口,可以非常方便地通过发送一些SQL命令去修改 ProxySQL 的配置。 ProxySQL 会解析通过该接口发送的某些对ProxySQL 有效的特定命令,并将其合理转换后发送给内嵌的 SQLite3 数据库引擎去运行
ProxySQL 的配置几乎都是通过管理接口来操作的,通过 Admin 管理接口,可以在线修改几乎所有的配置并使其生效。只有两个变量的配置是必须重启 ProxySQL 才能生效的,它们是:
mysql-threads 和 mysql-stacksize
admin-admin_credentials 变量控制的是admin管理接口的管理员账户。默认的管理员账户和密码为admin:admin,但是这个默认的用户只能在本地使用。如果想要远程连接到ProxySQL,例如用windows上的navicat连接Linux上的ProxySQL管理接口,必须自定义一个管理员账户。
添加管理员帐户
MySQL [(none)]> select @@admin-admin_credentials; 查看当前用户名和密码
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin |
+---------------------------+
1 row in set (0.00 sec)
设置管理员帐号myadmin,密码123456
MySQL [(none)]> set admin-admin_credentials='admin:admin;myadmin:123456';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> select @@admin-admin_credentials;
+----------------------------+
| @@admin-admin_credentials |
+----------------------------+
| admin:admin;myadmin:123456 |
+----------------------------+
1 row in set (0.00 sec)
MySQL [(none)]> load admin variables to runtime; 使修改立即生效
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> save admin variables to disk; 使修改永久保存到磁盘
Query OK, 31 rows affected (0.01 sec)
修改后,就可以使用该用户名和密码连接管理接口
[root@xiaoxiong ~]# mysql -umyadmin -p123456 -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
所有的配置操作都是在修改main库中对应的表
MySQL [(none)]> select * from global_variables where variable_name='admin-admin_credentials';
+-------------------------+----------------------------+
| variable_name | variable_value |
+-------------------------+----------------------------+
| admin-admin_credentials | admin:admin;myadmin:123456 |
+-------------------------+----------------------------+
1 row in set (0.00 sec)
必须要区分admin管理接口的用户名和mysql_users中的用户名
admin管理接口的用户必须不能存在于mysql_users中,这是出于安全的考虑,防止通过admin管理接口用户猜出mysql_users中的用户
admin-stats_credentials 变量控制admin管理接口的普通用户,这个变量中的用户没有超级管理员权限,只能查看monitor库和main库中关于统计的数据,其它库都是不可见的,且没有任何写权限
默认的普通用户名和密码均为 stats ,与admin一样,它默认也只能用于本地登录,若想让人远程查看则要添加查看的专有用户
MySQL [(none)]> select @@admin-stats_credentials;
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats |
+---------------------------+
1 row in set (0.00 sec)
添加专有的查看用户
MySQL [(none)]> set admin-stats_credentials='stats:stats;mystats:123456';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> select @@admin-stats_credentials;
+----------------------------+
| @@admin-stats_credentials |
+----------------------------+
| stats:stats;mystats:123456 |
+----------------------------+
1 row in set (0.00 sec)
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.00 sec)
同样,这个变量中的用户必须不能存在于mysql_users表中
使用mystats用户远程连接查看
[root@xiaoxiong ~]# mysql -umystats -p123456 -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show tables from main;
+--------------------------------------+
| tables |
+--------------------------------------+
| global_variables |
| stats_memory_metrics |
| stats_mysql_commands_counters |
| stats_mysql_connection_pool |
| stats_mysql_connection_pool_reset |
| stats_mysql_global |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist |
| stats_mysql_query_digest |
| stats_mysql_query_digest_reset |
| stats_mysql_query_rules |
| stats_mysql_users |
| stats_proxysql_servers_checksums |
| stats_proxysql_servers_metrics |
| stats_proxysql_servers_status |
+--------------------------------------+
15 rows in set (0.00 sec)
admin-mysql_ifaces 变量指定admin接口的监听地址,格式为冒号分隔的hostname:port列表。默认监听在 0.0.0.0:6032
注意,允许使用UNIX的domain socket进行监听,这样本主机内的应用程序就可以直接被处理。
例如:
MySQL [(none)]> set admin-mysql_ifaces='0.0.0.0:6032;/tmp/proxysql_admin.sock';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.00 sec)
使用ProxySQL的Admin管理接口连上ProxySQL,可查看ProxySQL拥有的库
[root@xiaoxiong ~]# mysql -umyadmin -p123456 -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(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)
其中:
ProxySQL 内部使用的是 SQLite3 数据库,无论是内存数据库还是磁盘数据库,都是通过SQLite3引 擎进行解析、操作的。它和 MySQL 的语法可能稍有不同,但ProxySQL会对不兼容的语法自动进行调整,最大程度上保证MySQL语句的有效率。
上面描述main库的时候,只是说了内存数据库需要持久化到disk库才能永久保存配置。但实际上,修改了main库中的配置后,并不会立即生效,它还需要load到runtime的数据结构中才生效,只有在runtime数据结构中的配置才是对ProxySQL当前有效的配置
ProxySQL 的配置系统非常强大,它能在线修改几乎所有配置(仅有的两个需要重启才能生效的变量为 mysql-threads 和 mysql-stacksize ),并在线生效、持久化保存。这得益于它采用的多层配置系统。
多层配置系统结构如下:
+-------------------------+
| RUNTIME |
+-------------------------+
/|\ |
| |
[1] | [2] |
| \|/
+-------------------------+
| MEMORY |
+-------------------------+ _
/|\ | |\
| | \
[3] | [4] | \ [5]
| \|/ \
+-------------------------+ +---------------+
| DISK | | CONFIG FILE |
+-------------------------+ +---------------+
最底层的是 disk 库和 config file 。这里需要注意,这里的 config file 就是传统的配置文件,默认为 /etc/proxysql.cnf , ProxySQL 启动时,主要是从 disk 库中读取配置加载到内存并最终加载到 runtime 生效,只有极少的几个特定配置内容是从 config file 中加载的,除非是第一次初始化 ProxySQL 运行环境(或者disk库为空)。
中间层的是 memory ,表示的是内存数据库,其实就是 main 库。通过管理接口修改的所有配置,都保存在内存数据库(main)中。当 ProxySQL 重启或者崩溃时,这个内存数据库中的数据会丢失,所以需要 save 到 disk 库中。
最上层的是 runtime ,它是 ProxySQL 有关线程运行时读取的数据结构。换句话说,该数据结构中的配置都是已生效的配置。所以,修改了 main 库中的配置后,必须 load 到 runtime 数据结构中才能使其生效。
在上面的多层配置系统图中,标注了[1]、[2]、[3]、[4]、[5]的序号。每个序号都有两个操作方向from/to,其实只是所站角度不同而已。以下是各序号对应的操作:
[1] :将内存数据库中的配置加载到RUNTIME数据结构中
LOAD XXX FROM MEMORY
LOAD XXX TO RUNTIME
[2] :将RUNTIME数据结构中的配置持久化到内存数据库中
SAVE XXX FROM RUNTIME
SAVE XXX TO MEMORY
[3] :将磁盘数据库中的配置加载到内存数据库中
LOAD XXX FROM DISK
LOAD XXX TO MEMORY
[4] :将内存数据库中的配置持久化到磁盘数据库中
SAVE XXX FROM MEMORY
SAVE XXX TO DISK
[5] :从传统配置文件中读取配置加载到内存数据库中
LOAD XXX FROM CONFIG
DISK/MEMORY/RUNTIME/CONFIG 可以缩写,只要能识别即可。例如MEMORY可以缩写为MEM,runtime可以缩写为run
另外,上面的XXX是什么?这表示要加载/保存的是哪类配置。目前的ProxySQL支持以下几种:
mysql users
mysql servers
mysql variables
mysql query rules
admin variables
scheduler
proxysql_servers:目前ProxySQL集群功能还处于实验阶段,所以该类配置不应该去使用
这些从main库或disk库中就可以查看到
MySQL [(none)]> show tables from disk;
+------------------------------------+
| tables |
+------------------------------------+
| global_variables | # (1)
| mysql_collations | # (N)
| mysql_group_replication_hostgroups | # (2)
| mysql_query_rules | # (3)
| mysql_query_rules_fast_routing | # (4)
| mysql_replication_hostgroups | # (5)
| mysql_servers | # (6)
| mysql_users | # (7)
| proxysql_servers | # (8)
| scheduler | # (9)
+------------------------------------+
10 rows in set (0.00 sec)
上面的结果中我给这些表都标注了一些序号,其所对应的表的内容有以下讲究:
如果 ProxySQL 是刚安装的,或者磁盘数据库文件为空(甚至不存在),或者启动 ProxySQL 时使用了选项 --initial,这几种情况启动 ProxySQL 时,都会从传统配置文件 config file 中读取配置加载到内存数据库,并自动 load 到 runtime 数据结构、save到磁盘数据库,这是初始化 ProxySQL 运行环境的过程。
如果不是第一次启动 ProxySQL ,由于已经存在磁盘数据库文件,这时 ProxySQL 会从磁盘数据库中读取几乎所有的配置(即使传统配置文件中配置了某项,也不会去解析),但有3项是必须从传统配置文件中读取,它们分别是:
数据库中间件最基本的功能就是实现读写分离, ProxySQL 当然也支持。而且 ProxySQL 支持的路由规则非常灵活,不仅可以实现最简单的读写分离,还可以将读/写都分散到多个不同的组,以及实现分库 sharding (分表sharding的规则比较难写,但也能实现)。
本文只描述通过规则制定的语句级读写分离,不讨论通过 ip/port, client, username, schemaname 实现的读写分离。
下面描述了ProxySQL能实现的常见读写分离类型
这种模式的读写分离,严格区分后端的master和slave节点,且slave节点必须设置选项read_only=1
在ProxySQL上,分两个组,一个写组HG=10,一个读组HG=20。同时在ProxySQL上开启monitor模块的read_only监控功能,让ProxySQL根据监控到的read_only值来自动调整节点放在HG=10(master会放进这个组)还是HG=20(slave会放进这个组)
这种模式的读写分离是最简单的,只需在mysql_users表中设置用户的默认路由组为写组HG=10,并在mysql_query_rules中加上两条简单的规则(一个select for update,一个select)即可
这种读写分离模式,在环境较小时能满足绝大多数需求。但是需求复杂、环境较大时,这种模式就太过死板,因为一切都是monitor模块控制的
前面那种读写分离模式,是通过 monitor 模块监控 read_only 来调整的,所以每一个后端集群必须只能分为一个写组,一个读组。
但如果想要区分不同的 select ,并将不同的 select 路由到不同的节点上。例如有些查询语句的开销非常大,想让它们独占一个节点/组,其它查询共享一个节点/组,怎么实现?
例如,下面这种模式
看上去非常简单。但是却能适应各种需求。例如,后端做了分库,对某库的查询要路由到特定的主机组
至于各个主机组是同一个主从集群(下图左边),还是互相独立的主从集群环境(下图右边),要看具体的需求,不过这种读写分离模式都能应付
在实现这种模式时,前提是不能开启monitor模块的read_only监控功能,也不要设置mysql_replication_hostgroup 表
例如,下面的配置实现的是上图左边的结构:写请求路由给HG=10,对test1库的select语句路由给HG=20,其它select路由给HG=30
mysql_servers:
+--------------+----------+------+--------+--------+
| hostgroup_id | hostname | port | status | weight |
+--------------+----------+------+--------+--------+
| 10 | host1 | 3306 | ONLINE | 1 |
| 20 | host2 | 3306 | ONLINE | 1 |
| 30 | host3 | 3306 | ONLINE | 1 |
+--------------+----------+------+--------+--------+
mysql_users:
+----------+-------------------+
| username | default_hostgroup |
+----------+-------------------+
| root | 10 |
+----------+-------------------+
mysql_query_rules:
+---------+-----------------------+----------------------+
| rule_id | destination_hostgroup | match_digest |
+---------+-----------------------+----------------------+
| 1 | 10 | ^SELECT.*FOR UPDATE$ |
| 2 | 20 | ^SELECT.*test1\..* |
| 3 | 30 | ^SELECT |
+---------+-----------------------+----------------------+
查看表结构的方式:
PRAGMA table_info("表名");
环境说明:
IP | 角色 | 应用 | 系统平台 |
---|---|---|---|
192.168.28.128 | 读写分离解析主机 | proxysql | rhel7.4 |
192.168.28.129 | master | mysql5.7 | rhel7.4 |
192.168.28.130 | slave | mysql5.7 | rhel7.4 |
192.168.28.132 | slave | mysql5.7 | rhel7.4 |
准备工作:
配置proxysql的yum源
[root@proxysql ~]# cat <
mysql> grant all on *.* to 'proxysql'@'192.168.28.128' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
[root@proxysql ~]# yum -y install mariadb
[root@proxysql ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
([email protected]: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)
数据库说明:
([email protected]: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_servers | # 存储MySQL实例的信息
| mysql_users | # 存储MySQL用户
| proxysql_servers | # 存储ProxySQL的信息,用于ProxySQL Cluster同步
| 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 10 表示写组,20表示读组,30表示读组。
([email protected]:6032) [(none)]> PRAGMA table_info('mysql_servers');
+-----+---------------------+--------------+---------+------------+----+
| cid | name | type | notnull | dflt_value | pk |
+-----+---------------------+--------------+---------+------------+----+
| 0 | hostgroup_id | INT | 1 | 0 | 1 |
| 1 | hostname | VARCHAR | 1 | NULL | 2 |
| 2 | port | INT | 1 | 3306 | 3 |
| 3 | status | VARCHAR | 1 | 'ONLINE' | 0 |
| 4 | weight | INT | 1 | 1 | 0 |
| 5 | compression | INT | 1 | 0 | 0 |
| 6 | max_connections | INT | 1 | 1000 | 0 |
| 7 | max_replication_lag | INT | 1 | 0 | 0 |
| 8 | use_ssl | INT | 1 | 0 | 0 |
| 9 | max_latency_ms | INT UNSIGNED | 1 | 0 | 0 |
| 10 | comment | VARCHAR | 1 | '' | 0 |
+-----+---------------------+--------------+---------+------------+----+
11 rows in set (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(10,'192.168.28.129',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(20,'192.168.28.130',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(30,'192.168.28.132',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_servers\G
*************************** 1. row ***************************
hostgroup_id: 10
hostname: 192.168.28.129
port: 3306
status: ONLINE
weight: 1
compression: 0
max_connections: 1000
max_replication_lag: 0
use_ssl: 0
max_latency_ms: 0
comment: Write Group
*************************** 2. row ***************************
hostgroup_id: 20
hostname: 192.168.28.130
port: 3306
status: ONLINE
weight: 1
compression: 0
max_connections: 1000
max_replication_lag: 0
use_ssl: 0
max_latency_ms: 0
comment: Read Group
*************************** 3. row ***************************
hostgroup_id: 30
hostname: 192.168.28.132
port: 3306
status: ONLINE
weight: 1
compression: 0
max_connections: 1000
max_replication_lag: 0
use_ssl: 0
max_latency_ms: 0
comment: Read Group
3 rows in set (0.00 sec)
修改后,需要加载到RUNTIME,并保存到disk
([email protected]:6032) [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.01 sec)
在 proxysql 主机的 mysql_users 表中添加刚才在 master 上创建的账号 proxysql,proxysql 客户端需要使用这个账号来访问数据库
default_hostgroup 默认组设置为写组,也就是1;
当读写分离的路由规则不符合时,会访问默认组的数据库;
([email protected]:6032) [(none)]> PRAGMA table_info('mysql_users');
+-----+------------------------+---------+---------+------------+----+
| cid | name | type | notnull | dflt_value | pk |
+-----+------------------------+---------+---------+------------+----+
| 0 | username | VARCHAR | 1 | NULL | 1 |
| 1 | password | VARCHAR | 0 | NULL | 0 |
| 2 | active | INT | 1 | 1 | 0 |
| 3 | use_ssl | INT | 1 | 0 | 0 |
| 4 | default_hostgroup | INT | 1 | 0 | 0 |
| 5 | default_schema | VARCHAR | 0 | NULL | 0 |
| 6 | schema_locked | INT | 1 | 0 | 0 |
| 7 | transaction_persistent | INT | 1 | 1 | 0 |
| 8 | fast_forward | INT | 1 | 0 | 0 |
| 9 | backend | INT | 1 | 1 | 2 |
| 10 | frontend | INT | 1 | 1 | 0 |
| 11 | max_connections | INT | 1 | 10000 | 0 |
+-----+------------------------+---------+---------+------------+----+
12 rows in set (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',10,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
username: proxysql
password: 123456
active: 1
use_ssl: 0
default_hostgroup: 10
default_schema: NULL
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
1 row in set (0.00 sec)
([email protected]:6032) [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.01 sec)
在mysql的 master 端添加属于proxysql的只读账号
mysql> GRANT SELECT ON *.* TO 'monitor'@'192.168.28.%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在proxysql主机端修改变量设置健康检测的账号
([email protected]:6032) [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.00 sec)
需求:
([email protected]:6032) [(none)]> PRAGMA table_info('mysql_query_rules');
+-----+-----------------------+--------------+---------+------------+----+
| cid | name | type | notnull | dflt_value | pk |
+-----+-----------------------+--------------+---------+------------+----+
| 0 | rule_id | INTEGER | 1 | NULL | 1 |
| 1 | active | INT | 1 | 0 | 0 |
| 2 | username | VARCHAR | 0 | NULL | 0 |
| 3 | schemaname | VARCHAR | 0 | NULL | 0 |
| 4 | flagIN | INT | 1 | 0 | 0 |
| 5 | client_addr | VARCHAR | 0 | NULL | 0 |
| 6 | proxy_addr | VARCHAR | 0 | NULL | 0 |
| 7 | proxy_port | INT | 0 | NULL | 0 |
| 8 | digest | VARCHAR | 0 | NULL | 0 |
| 9 | match_digest | VARCHAR | 0 | NULL | 0 |
| 10 | match_pattern | VARCHAR | 0 | NULL | 0 |
| 11 | negate_match_pattern | INT | 1 | 0 | 0 |
| 12 | re_modifiers | VARCHAR | 0 | 'CASELESS' | 0 |
| 13 | flagOUT | INT | 0 | NULL | 0 |
| 14 | replace_pattern | VARCHAR | 0 | NULL | 0 |
| 15 | destination_hostgroup | INT | 0 | NULL | 0 |
| 16 | cache_ttl | INT | 0 | NULL | 0 |
| 17 | reconnect | INT | 0 | NULL | 0 |
| 18 | timeout | INT UNSIGNED | 0 | NULL | 0 |
| 19 | retries | INT | 0 | NULL | 0 |
| 20 | delay | INT UNSIGNED | 0 | NULL | 0 |
| 21 | next_query_flagIN | INT UNSIGNED | 0 | NULL | 0 |
| 22 | mirror_flagOUT | INT UNSIGNED | 0 | NULL | 0 |
| 23 | mirror_hostgroup | INT UNSIGNED | 0 | NULL | 0 |
| 24 | error_msg | VARCHAR | 0 | NULL | 0 |
| 25 | OK_msg | VARCHAR | 0 | NULL | 0 |
| 26 | sticky_conn | INT | 0 | NULL | 0 |
| 27 | multiplex | INT | 0 | NULL | 0 |
| 28 | log | INT | 0 | NULL | 0 |
| 29 | apply | INT | 1 | 0 | 0 |
| 30 | comment | VARCHAR | 0 | NULL | 0 |
+-----+-----------------------+--------------+---------+------------+----+
31 rows in set (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',10,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',20,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(3,1,'^SHOW',30,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.01 sec)
登录用户是刚才我们在 mysql_user 表中创建的用户,端口为6033
[root@proxysql ~]# mysql -uproxysql -p123456 -h127.0.0.1 -P6033
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.30 (ProxySQL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
([email protected]:6033) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| daxiong |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
创建2个数据库并查询一下表
([email protected]:6033) [(none)]> create database xiaoxiong;
Query OK, 1 row affected (0.01 sec)
([email protected]:6033) [(none)]> create database dadaguia;
Query OK, 1 row affected (0.01 sec)
([email protected]:6033) [(none)]> select user,host from mysql.user;
+---------------+----------------+
| user | host |
+---------------+----------------+
| monitor | 192.168.28.% |
| proxysql | 192.168.28.128 |
| repl1 | 192.168.28.132 |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+----------------+
6 rows in set (0.00 sec)
([email protected]:6033) [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
proxysql有个类似审计的功能,可以查看各类SQL的执行情况,其需要在proxysql管理端执行
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
([email protected]:6032) [(none)]> select * from stats_mysql_query_digest\G
*************************** 1. row ***************************
hostgroup: 10
schemaname: information_schema
username: proxysql
digest: 0xAF8078E6F28843D6
digest_text: creat database xiaoxiong
count_star: 1
first_seen: 1568105984
last_seen: 1568105984
sum_time: 3660
min_time: 3660
max_time: 3660
*************************** 2. row ***************************
hostgroup: 10
schemaname: information_schema
username: proxysql
digest: 0x898F1FBBEB8689EB
digest_text: create database dadaguia
count_star: 1
first_seen: 1568106010
last_seen: 1568106010
sum_time: 4364
min_time: 4364
max_time: 4364
*************************** 3. row ***************************
hostgroup: 10
schemaname: information_schema
username: proxysql
digest: 0xD590CAE0035D20AD
digest_text: user mysql
count_star: 1
first_seen: 1568106075
last_seen: 1568106075
sum_time: 564
min_time: 564
max_time: 564
*************************** 4. row ***************************
hostgroup: 20
schemaname: information_schema
username: proxysql
digest: 0x620B328FE9D6D71A
digest_text: SELECT DATABASE()
count_star: 1
first_seen: 1568106079
last_seen: 1568106079
sum_time: 3025
min_time: 3025
max_time: 3025
*************************** 5. row ***************************
hostgroup: 20
schemaname: information_schema
username: proxysql
digest: 0x0F02B330C823D739
digest_text: select user,host from mysql.user
count_star: 1
first_seen: 1568106026
last_seen: 1568106026
sum_time: 7178
min_time: 7178
max_time: 7178
*************************** 6. row ***************************
hostgroup: 30
schemaname: information_schema
username: proxysql
digest: 0x99531AEFF718C501
digest_text: show tables
count_star: 1
first_seen: 1568106041
last_seen: 1568106041
sum_time: 1260
min_time: 1260
max_time: 1260
12 rows in set (0.00 sec)
从上面的 hostgroup 和 digest_text 值来看,所有的写操作都被路由至10组,读操作都被路由至20组,30组,其中10组为写组,20,30组为读组!
由此可见,读写分离成功!!!