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安裝

#安裝生成的文件:
[root@ProxySQL ~]#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端口

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

[root@ProxySQL ~]#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,登錄進去之後可以修改變量進行修改賬號密碼

[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, 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',2,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

[root@centos7 ~]#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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章