ProxySQL实现mysql读写分离

环境说明

IP角色应用系统平台
192.168.118.222读写分离解析主机proxysqlrhel7.4
192.168.118.112mastermariadbrhel7.4
192.168.118.100slavemariadbrhel7.4

关闭防火墙,关闭selinux

[root@server01 ~]# systemctl stop firewalld
[root@server01 ~]# setenforce 0
[root@server01 ~]# systemctl disable firewalld

安装mysql并配置主从
安装proxysql

#安装mariadb
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q   Local Address:Port     Peer Address:Port 
LISTEN      0      100          127.0.0.1:25                  *:*     
LISTEN      0      50                   *:3306                *:*     
LISTEN      0      128                  *:22                  *:*     
LISTEN      0      100                ::1:25                 :::*     
LISTEN      0      32                  :::21                 :::*     
LISTEN      0      128                 :::22                 :::*     

#配置proxysql的yum源
安装proxysql
[root@localhost ~]# cat  /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
[root@localhost ~]# yum -y install proxysql
[root@localhost ~]# systemctl start proxysql
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q   Local Address:Port     Peer Address:Port 
LISTEN      0      100          127.0.0.1:25                  *:*     
LISTEN      0      50                   *:3306                *:*     
LISTEN      0      128                  *:6032                *:*     
LISTEN      0      128                  *:6033                *:*     
LISTEN      0      128                  *:6033                *:*     
LISTEN      0      128                  *:6033                *:*     
LISTEN      0      128                  *:6033                *:*     
LISTEN      0      128                  *:22                  *:*     
LISTEN      0      100                ::1:25                 :::*     
LISTEN      0      32                  :::21                 :::*     
LISTEN      0      128                 :::22                 :::*     

在192.168.118.112(主库)上添加proxysql可以增删查改的账号

MariaDB [(none)]> grant all on *.* to 'proxysql'@'192.168.118.222' identified by 'pwproxysql';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

登录proxysql管理端

[root@localhost ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@localhost ~]# 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.

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

使用 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.118.112',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.118.100',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.118.112 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Write Group |
| 2            | 192.168.118.100 | 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;
Query OK, 0 rows affected (0.00 sec)

(admin@127.0.0.1:6032) [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.01 sec)

(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)]> elect * from mysql_users \G
ERROR 1045 (#2800): near "elect": syntax error
(admin@127.0.0.1:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
              username: proxysql
              password: pwproxysql
                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)

(admin@127.0.0.1:6032) [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

(admin@127.0.0.1:6032) [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.00 sec)

在mysql的 master 端添加属于proxysql的只读账号

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

(root@localhost:mysql.sock) [(none)]> GRANT SELECT ON *.* TO 'monitor'@'192.168.118.%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected (0.00 sec)

(root@localhost:mysql.sock) [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在proxysql主机端修改变量设置健康检测的账号

[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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.

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

(admin@127.0.0.1:6032) [(none)]> load mysql variables to runtime;Query OK, 0 rows affected (0.00 sec)

(admin@127.0.0.1:6032) [(none)]> save mysql variables to disk;
Query OK, 96 rows affected (0.00 sec)


添加读写分离的路由规则
需求:

将 select 查询语句全部路由至 hostgroup_id=2 的组(也就是读组)
但是 select * from tb for update 这样的语句是会修改数据的,所以需要单独定义,将它路由至 hostgroup_id=1 的组(也就是写组)
其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users 表中的 default_hostgroup)
(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;
Query OK, 0 rows affected (0.00 sec)

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

(admin@127.0.0.1:6032) [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)

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

验证读写分离
登录用户是刚才我们在 mysql_user 表中创建的用户,端口为6033

[root@localhost ~]# mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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.

(proxysql@127.0.0.1:6033) [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| xsb                |
+--------------------+
5 rows in set (0.00 sec)
尝试修改数据库和查询
(proxysql@127.0.0.1:6033) [(none)]> create database haha;
Query OK, 1 row affected (0.00 sec)

(proxysql@127.0.0.1:6033) [(none)]> select user,host from mysql.user;
+----------+-----------------+
| user     | host            |
+----------+-----------------+
| root     | 127.0.0.1       |
| proxysql | 192.168.118.222 |
| root     | ::1             |
|          | localhost       |
| root     | localhost       |
|          | server01        |
| root     | server01        |
+----------+-----------------+
7 rows in set (0.00 sec)

(proxysql@127.0.0.1:6033) [(none)]> \q
Bye

验证读写分离是否成功

[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
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.

(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 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1         | information_schema | proxysql | 0xEB4549A4A010C504 | create database haha             | 1          | 1535461379 | 1535461379 | 911      | 911      | 911      |
| 1         | information_schema | proxysql | 0x34B4BDE2EF8B8AE3 | create database wangqing         | 1          | 1535460570 | 1535460570 | 12612    | 12612    | 12612    |
| 1         | information_schema | proxysql | 0xBBFD963DCCF2A4F6 | show databasws                   | 1          | 1535460248 | 1535460248 | 10001248 | 10001248 | 10001248 |
| 2         | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1          | 1535461390 | 1535461390 | 1421     | 1421     | 1421     |
| 1         | information_schema | proxysql | 0x02033E45904D3DF0 | show databases                   | 6          | 1535460189 | 1535461347 | 30032967 | 2633     | 10001453 |
| 1         | information_schema | proxysql | 0xCE6BB7764BA98E04 | create database jinxinruntian    | 1          | 1535460553 | 1535460553 | 15011    | 15011    | 15011    |
| 1         | information_schema | proxysql | 0x594F2C744B698066 | select USER()                    | 4          | 1535460169 | 1535461340 | 0        | 0        | 0        |
| 1         | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 4          | 1535460169 | 1535461340 | 0        | 0        | 0        |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
8 rows in set (0.00 sec)
从上面的 hostgroup 和 digest_text 值来看,所有的写操作都被路由至1组,读操作都被路由至2组,其中1组为写组,2组为读组

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值