基于proxysql配置mysql读写分离

版权声明:未经本人允许严禁转载 https://blog.csdn.net/WanJiaBaoBao/article/details/84431638

环境说明

  • 已关闭防火墙,selinux;
  • 已用二进制安装mysql-5.7-22版本,mysql二进制安装详情请见mysql二进制安装
  • mysql主从已配置完成,配置详情可参照mysql主从配置
  • 所用服务器的IP地址、角色及安装内容如下表:
IP地址 角色 安装内容
192.168.91.128 proxysql中间件 proxysql,mariadb-server、mariadb
192.168.91.135 master(主) mysql-5.7.22
192.168.91.136 slave(从) mysql-5.7.22

原理图

在这里插入图片描述

配置步骤

  • 在192.168.91.128主机上,安装mariadb-server、mariadb、proxysql
[root@localhost ~]# yum -y install mariadb mariadb-server
......
Installed:
  mariadb.x86_64 1:5.5.60-1.el7_5            mariadb-server.x86_64 1:5.5.60-1.el7_5           

Dependency Installed:
  libaio.x86_64 0:0.3.109-13.el7                perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7  
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7   perl-DBD-MySQL.x86_64 0:4.023-6.el7           
  perl-DBI.x86_64 0:1.627-4.el7                 perl-Data-Dumper.x86_64 0:2.145-3.el7         
  perl-IO-Compress.noarch 0:2.061-2.el7         perl-Net-Daemon.noarch 0:0.48-5.el7           
  perl-PlRPC.noarch 0:0.2020-14.el7            

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                                                         
[root@localhost src]# wget https://github.com/sysown/proxysql/releases/download/v1.4.12/proxysql-1.4.12-1-centos7.x86_64.rpm
Saving to: ‘proxysql-1.4.12-1-centos7.x86_64.rpm’

100%[=====================================================>] 5,986,336   1.20MB/s   in 4.7s   

2018-11-24 20:58:28 (1.20 MB/s) - ‘proxysql-1.4.12-1-centos7.x86_64.rpm’ saved [5986336/5986336]
[root@localhost ~]# rpm -ivh proxysql-1.4.12-1-centos7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:proxysql-1.4.12-1                ################################# [100%]
  • 在192.168.91.128上,启动proxysql,查看端口,并登录proxysql管理端,账户为admin,密码为admin,主机地址为127.0.0.1,端口为6032(我直接将账户和密码写入配置文件进行登录)
[root@localhost ~]# systemctl start proxysql
[root@localhost ~]# ss -ntlp
State       Recv-Q Send-Q      Local Address:Port     Peer Address:Port 
LISTEN      0      128                     *:6032                *:*      users:(("proxysql",12358,23))
LISTEN      0      128                     *:6033                *:*      users:(("proxysql",12358,22))
LISTEN      0      128                     *:6033                *:*      users:(("proxysql",12358,21))
LISTEN      0      128                     *:6033                *:*      users:(("proxysql",12358,20))
LISTEN      0      128                     *:6033                *:*      users:(("proxysql",12358,19))
[root@localhost ~]# vim .my.cnf
[client]
user=admin
password='admin'
host='127.0.0.1'
port=6032
[root@localhost ~]# mysql
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)

修改mysql登录提示符(临时生效)
[root@localhost ~]# export MYSQL_PS1="(\u@\h:\p) [\d]>"
[root@localhost ~]# mysql
([email protected]:6032) [(none)]>

  • 在192.168.691.128上,Proxysql管理端添加后端连接mysql主从数据库的配置(注:执行load ···to runtime才能加载到RUNTIME中生效,执行save ··· to disk才能将配置持久化保存到磁盘
MySQL [(none)]> show tables from main;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| 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)

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.91.135',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.91.136',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
MySQL [(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.91.135 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Write Group |
| 2            | 192.168.91.136 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Read Group  |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
2 rows in set (0.00 sec)

注:下面语句中没有先切换到 main 库也执行成功了,因为 ProxySQL 内部使用的 SQLite3 数据库引擎,和 MySQL 的解析方式是不一样的。即使执行了 USE main 语句也是无任何效果的,但不会报错。
  • 在192.168.91.128上,将修改的配置加载到RUNTIME,并保存到disk
MySQL [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.01 sec)

注:使用 insert 语句添加 mysql 主机到 mysql_servers 表中,其中:hostgroup_id 1 表示写组,2表示读组
  • 在192.168.91.135主机master(主)上,创建用户并授权
mysql> create user 'proxysql'@'192.168.91.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on *.* to 'proxysql'@'192.168.91.%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 在 proxysql 主机的 mysql_users 表中添加刚才在 master 上创建的账号 proxysql,proxysql 客户端需要使用这个账号来访问数据库
MySQL [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent) values('proxysql','123456',1,1);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
              username: proxysql		##后端mysql实例的用户名
              password: 123456		##后端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)

MySQL [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.00 sec)

添加健康检测账号

  • 在192.168.91.135 master主机上,添加属于proxysql的只读账号
mysql> grant select on *.* to 'monitor'@'192.168.91.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  • 在192.168.91.128 proxysql主机上,修改变量设置健康检测的账号
MySQL [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> set mysql-monitor_password='123456';
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.01 sec)

添加读写分离的路由规则

  • 需求:

    • 将 select 查询语句全部路由至 hostgroup_id=2 的组(也就是读组);
    • 但是 select * from tb for update 这样的语句是会修改数据的,所以需要单独定义,将它路由至 hostgroup_id=1 的组(也就是写组);
    • 其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users 表中的 default_hostgroup)。
  • 在192.168.91.128主机上进行配置

MySQL [(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)

MySQL [(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)

MySQL [(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)

MySQL [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.00 sec)

验证读写分离

  • 在192.168.91.128主机上,登录到proxysql客户端,账号为proxysql(mysql_users表中创建的),密码为123456,主机地址为127.0.0.1,端口为6033
[root@localhost ~]# mysql -uproxysql -p123456 -h 127.0.0.1 -P 6033
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
  • 修改数据库和查询
MySQL [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> create database haha;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> select user,host from mysql.user;
+---------------+--------------+
| user          | host         |
+---------------+--------------+
| monitor       | 192.168.91.% |
| proxysql      | 192.168.91.% |
| repl          | 192.168.91.% |
| mysql.session | localhost    |
| mysql.sys     | localhost    |
| root          | localhost    |
+---------------+--------------+
6 rows in set (0.01 sec)
  • 验证读写分离是否成功
[root@localhost ~]# mysql
MySQL [(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 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1         | information_schema | proxysql | 0xEB4549A4A010C504 | create database haha             | 1          | 1543077133 | 1543077133 | 2955     | 2955     | 2955     |
| 1         | information_schema | proxysql | 0x30327C1D25D51434 | create database test             | 1          | 1543077129 | 1543077129 | 3646     | 3646     | 3646     |
| 2         | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1          | 1543077152 | 1543077152 | 3506     | 3506     | 3506     |
| 1         | information_schema | proxysql | 0x02033E45904D3DF0 | show databases                   | 1          | 1543077082 | 1543077082 | 5068     | 5068     | 5068     |
| 1         | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 2          | 1543076519 | 1543076981 | 0        | 0        | 0        |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
5 rows in set (0.00 sec)

注:从上面的 hostgroup 和 digest_text 值来看,所有的写操作都被路由至1组,读操作都被路由至2组,其中1组为写组,2组为读组!由此可见,读写分离成功!!!

猜你喜欢

转载自blog.csdn.net/WanJiaBaoBao/article/details/84431638
今日推荐