Mysql使用ProxySQL实现读写分离

ProxySQL简介:

准备工作

  • 本文所用环境:

    • 系统均为CentOS7.4,并且关闭防火墙和selinux
    • ProxySQL版本:proxysql-1.4.8-1-centos7.x86_64.rpm
    • Mysql版本:mariadb 5.5.56-2.el7
    • ProxySQL主机IP:192.168.100.2
    • Mysql主库IP:192.168.100.3
    • Mysql从库IP:192.168.100.4
  • 前提条件: Mysql主从已经配置好了同步

开始安装ProxySQL

  • 安装ProxySQL:
wget https://github.com/sysown/proxysql/releases/download/v1.4.8/proxysql-1.4.8-1-centos7.x86_64.rpm
yum install -y proxysql-1.4.8-1-centos7.x86_64.rpm

#proxysql需要依赖一些perl库,所以使用yum安装

#安装生成的文件:
[[email protected] ~]#rpm -ql proxysql
/etc/init.d/proxysql    #启动脚本

/etc/proxysql.cnf       #配置文件,仅在第一次(/var/lib/proxysql/proxysql.db文件不存在)启动时有效
                        #启动后可以在proxysql管理端中通过修改数据库的方式修改配置并生效(官方推荐方式。)
/usr/bin/proxysql       #主程序文件
/usr/share/proxysql/tools/proxysql_galera_checker.sh
/usr/share/proxysql/tools/proxysql_galera_writer.pl
  • 启动proxysql:
/etc/init.d/proxysql start

#proxysql客户端监听在6033端口上,管理端监听6032端口

[[email protected] ~]#/etc/init.d/proxysql start
Starting ProxySQL: DONE!

[[email protected] ~]#ss -tanl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port
LISTEN     0      128                          *:6032                                     *:*
LISTEN     0      128                          *:6033                                     *:*
    LISTEN     0      128                          *:6033                                   *:*
LISTEN     0      128                          *:6033                                     *:*
LISTEN     0      128                          *:6033                                     *:*
  • 连接proxysql管理端进行配置:
mysql -uadmin -padmin -h127.0.0.1 -P6032

#默认的管理端账号密码都是admin,登录进去之后可以修改变量进行修改账号密码

[[email protected] ~]# 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, 2017, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MySQL [(none)]>
  • 添加后端的mysql主机:
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,‘192.168.100.3‘,3306,1,‘Write Group‘);
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,‘192.168.100.4‘,3306,1,‘Read Group‘);

#使用insert语句添加主机到mysql_servers表中,其中:hostgroup_id 1 表示写组,2表示读组。

MySQL [(none)]> select * from mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+--
| hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | u
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+--
| 1            | 192.168.100.3 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0
| 2            | 192.168.100.4 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+--
2 rows in set (0.00 sec)
  • 添加可以访问后端主机的账号:
GRANT ALL ON *.* TO ‘proxysql‘@‘192.168.100.%‘ IDENTIFIED BY ‘123456‘;

#在后端mysql中添加可以增删改查的账号

insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values(‘proxysql‘,‘123456‘,1,1);

#在proxysql主机的mysql_users表中添加刚才创建的账号,proxysql客户端需要使用这个账号来访问数据库。
#default_hostgroup默认组设置为写组,也就是1
#当读写分离的路由规则不符合时,会访问默认组的数据库

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
              password: 123456
                active: 1
               use_ssl: 0
     default_hostgroup: 1
        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)
  • 添加健康监测的账号:
GRANT SELECT ON *.* TO ‘monitor‘@‘192.168.100.%‘ IDENTIFIED BY ‘monitor‘;

#在后端主机中添加可以访问数据库的账号,SELECT权限即可

set mysql-monitor_username=‘monitor‘
set mysql-monitor_password=‘monitor‘

#在proxysql管理端中修改变量设置健康检测的账号
  • 添加读写分离的路由规则:
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,‘^SELECT.*FOR UPDATE$‘,1,1);
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,‘^SELECT‘,1,1);

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

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)
  • 将刚才我们修改的数据加载至RUNTIME中(参考ProxySQL的多层配置结构):

load mysql users to runtime;
load mysql servers to runtime;
load mysql query rules to runtime;
load mysql variables to runtime;
load admin variables to runtime;

#load进runtime,是配置生效

save mysql users to disk;
save mysql servers to disk;
save mysql query rules to disk;
save mysql variables to disk;
save admin variables to disk;

#save到磁盘(/var/lib/proxysql/proxysql.db)中,永久保存配置

MySQL [(none)]> load mysql users 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)

测试读写分离

  • 连接proxysql客户端:

mysql -uproxysql -p123456 -h127.0.0.1 -P6033

#登录用户是刚才我们在mysql_user表中创建的用户,端口为6033

[[email protected] ~]#mysql -uproxysql -p123456 -h127.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)]>
  • 尝试修改数据库和查询:

create database bigboss;
create database weijinyun;
select user,host from mysql.user;

#创建两个数据库和查个表。

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.100.% |
| proxysql    | 192.168.100.% |
| repliaction | 192.168.100.% |
| root        | ::1           |
|             | centos7       |
| root        | centos7       |
|             | localhost     |
| root        | localhost     |
+-------------+---------------+
9 rows in set (0.01 sec)
  • 验证读写分离是否成功:

#proxysql有个类似审计的功能,可以查看各类SQL的执行情况。在proxysql管理端执行:
select * from stats_mysql_query_digest;

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

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 | 0xA6212D89D814BAC5 | create database weijinyun        | 1          | 1523658457 | 1523658457 | 1244     | 1244     | 1244     |
| 2         | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1          | 1523658520 | 1523658520 | 12538    | 12538    | 12538    |
| 1         | information_schema | proxysql | 0x02033E45904D3DF0 | show databases                   | 5          | 1523658103 | 1523658486 | 24852    | 1263     | 17592    |
| 1         | information_schema | proxysql | 0xA175FD2982EC6396 | create database bigboss          | 1          | 1523658437 | 1523658437 | 1833     | 1833     | 1833     |
| 1         | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 3          | 1523658098 | 1523658473 | 0        | 0        | 0     |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
6 rows in set (0.00 sec)

原文地址:http://blog.51cto.com/bigboss/2103290

时间: 2024-08-13 21:16:23

Mysql使用ProxySQL实现读写分离的相关文章

MHA+ProxySQL实现读写分离高可用

最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.com/sysown/proxysql/wiki 本文主要介绍的是MHA+ProxySQL读写分离以及高可用,ProxySQL的细节请参考文档,目前已经有人写发非常详细了,文章最后会给出链接.当然和Group Replication,PXC搭配那是更完美的.关于MHA的配置参考我前面的文章,本文就不再

mysq通过ProxySQL实现读写分离

mysq通过ProxySQL实现读写分离 常见的读写分离应用 Oracle:mysql-proxy qihoo:Atlas 美团:dbproxy 网易:cetus amoeba 阿里巴巴:cobar 基于amoeba研发 Mycat:基于cobar实现 ProxySQL ProxySQL:MySQL中间件 版本: 官方版 percona版:percona公司基于官方版本用C++语言开发,性能更优 特点:具有中间件所需的绝大多数功能,包括: 多种方式的读/写分离 定制基于用户.基于schema.基

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番配置直接可以实现,而读写分离则需要一些软件的支持,基本上读写分离的实现有两种: Amoeba(变形虫):是由前阿里员工实现的一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.但是由于没人维护了,而且据说作者也不再回答开发者的问题,所以不予考虑. Mysql-Proxy:是一

MySQL主从同步、读写分离配置步骤

现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1 1.登录Master服务器,修改my.cnf,添加如下内容: server-id = 1 //数据库ID号,

Mysql高级集群-读写分离Amoeba

一.环境介绍Master-IP:10.0.0.201Slave- IP:10.0.0.202Amobea-IP:10.0.0.203 二.安装JDK# mkdir /Amoeba# tar -xvf jdk-7u40-linux-x64.tar.gz -C /Amoeba/# vim /etc/profileJAVA_HOME=/Amoeba/jdk1.7.0_40export JAVA_HOME PATH=$JAVA_HOME/bin:$PATHexport PATH CLASSPATH=.:

MySQL Proxy 实现MySQLDB 读写分离

一.简述 MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载平衡,故障.查询分析,查询过滤和修改等等. MySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负 载平衡.对于应用来说,MySQL Proxy是完全透明的,应用

mysql基于amoeba配置读写分离

                     Mysql高级集群-读写分离Amoeba                          mysql在配置好主从复制之后,已经达到双机热备和容灾的效果.此博客是建立在主从复制的前提上 ,mysql基于amoeba的配置读写分离在我看来:就是为了达到数据库高可用性,安全性以及高并发,达到 负载均衡的效果.说简单点,我个人觉得意思就是让主服务器轻松点,不易挂掉.还有就是充分利用从服务器.  本人水平有限,望各位大神多多指点指点.我非常乐意听取意见. 此版本

搭建mysql的主从复制和读写分离

搭建mysql的主从复制和读写分离   +--------+                          (write)        +--------+                    | client |                 +---------------------+| master |     +--------+|                   |                        +--------+|           |      

MySQL主从同步、读写分离配置步骤、问题解决笔记

根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用.       为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master)           192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i6