ProxySQL讀寫分離

中間件ProxySQL讀寫分離試驗

主機 IP 作用
Master 192.168.37.7 主服務器
Slave 192.168.37.17 從服務器
ProxySQL 192.168.37.27 中間件服務器
Clinet 192.168.37.37 客戶主機

前期準備:
Master、Slave(從服務器my.cnf文件中必須要加上read_only,因爲ProxSQL通過此語句判斷主從服務器)先完成主從複製。ProxySQL安裝Mariadb,Clinet最少要安裝MySQL客戶端

ProxySQL設置

  • 設置ProxySQL yum源並安裝,實驗時版本爲1.4
    [root@poxysql ~]#vim /etc/yum.repos.d/proxysql.repo
    [proxysql]
    name=ProxySQLyum
    baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
    gpgcheck=1
    gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
    [root@poxysql ~]#yum install proxysql
  • ProxySQL文件
    [root@poxysql ~]#rpm -ql proxysql 
    /etc/init.d/proxysql
    /etc/proxysql.cnf
    /usr/bin/proxysql
    /usr/share/proxysql/tools/proxysql_galera_checker.sh
    /usr/share/proxysql/tools/proxysql_galera_writer.pl
  • 啓動ProxySQL

    [root@poxysql ~]#service proxysql start
    Starting ProxySQL: 2019-05-10 14:15:18 [INFO] Using config file /etc/proxysql.cnf
    DONE!
  • 登錄服務,ProxySQL默認用戶爲admin,密碼爲admin,管理端口爲6032,客戶使用端口爲6033

    [root@poxysql ~]#mysql -uadmin -padmin -P6032 -h127.0.0.1
    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)]> 
  • 設置讀寫分離的主機的地址
    在mysql_servers表中添加記錄
    MySQL [(none)]>insert into mysql_servers(hostgrup_id,hostname,prot)values(10,'192.168.37.7',3306);
    Query OK, 1 row affected (0.01sec)
    MySQL [(none)]>insert into mysql_servers(hostgrup_id,hostname,prot)values(10,'192.168.37.17',3306);
    Query OK, 1 row affected (0.01sec)
    MySQL [(none)]>  load mysql servers to runtime;   #加載到內存,使設置生效
    Query OK, 0 row affected (0.00 sec)
    MySQL [(none)]>  save mysql servers to disk;      #保存到硬盤中
    Query OK, 0 row affected (0.00 sec)
  • ProxySQL配置監控,添加一個訪問Master和Slave的用戶:monitor密碼:centos
    Masetr主機(因爲已經設置主從複製,所有Slave不用手動設置用戶,通過主從自動複製)
    MariaDB [(none)]>grant replication client on *.* to monitor@'192.168.37.%' identified by 'centos';
  • ProxySQL主機
    MySQL[(none)]>  set mysql-monitor_username='monitor';
    Query OK, 1 row affected (0.00 sec)
    MySQL [(none)]> set mysql-monitor_password='centos';
    Query OK, 1 row affected (0.00 sec)
    MySQL [(none)]> load mysql variables to runtime;   #加載到內存,使設置生效
    Query OK, 0 row affected (0.00 sec)
    MySQL [(none)]> save mysql variables to disk;      #保存到硬盤中
    Query OK, 0 row affected (0.00 sec)
  • 查看監控連接是否正常:如果connect_error的結果爲NULL則表示正常
MySQL [(none)]> select * from mysql_server_connect_log;
+---------------+------+------------------+-------------------------+---------------+
| hostname      | port | time_start_us    | connect_success_time_us | connect_error |
+---------------+------+------------------+-------------------------+---------------+
| 192.168.37.7  | 3306 | 1557470898315003 | 1898                    | NULL          |
| 192.168.37.17 | 3306 | 1557470899044242 | 1950                    | NULL          |
                .
                .
                .

| 192.168.37.7  | 3306 | 1557471439197688 | 4458                    | NULL          |
+---------------+------+------------------+-------------------------+---------------+
20 rows in set (0.01 sec)

查看監控心跳信息 (對ping指標的監控):
MySQL> select from mysql_server_ping_log;
查看read_only和replication_lag的監控日誌
MySQL> select
from mysql_server_read_only_log;
MySQL> select * from mysql_server_replication_lag_log;

  • 設置分組信息(讀寫分離的讀主機和寫主機)
    MySQL [(none)]> insert into mysql_replication_hostgroups values(10,20,"test");  #在分組表mysql_replication_hostgroups中添10(寫組)組和20(讀組)組,test爲描述信息  
    Query 0K, 1 row affacted (0.00sec)
    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)
  • 設置讀寫分離規則
    Master主機上設置客戶使用用戶
    MariaDB [(none)]> grant all on *.* to sqluser@'192.168.37.%' identified by 'centos';
  • ProxySQL中添加用戶
    MySQL[(none)]> insert into mysql_users(username,password,default_hostgroup)
    values('sqluser','centos',10);
    MySQL[(none)]> load mysql users to runtime;   #加載,使設置生效
    MySQL[(none)]> save mysql users to disk;      #保存到硬盤
  • 插入路由規則:將select語句分離到20的讀組,select語句中有一個特殊語句SELECT...FOR UPDATE它會申請寫鎖,應路由到10的寫組
    MySQL [(none)]> insert into mysql_query_rules
    (rule_id,active,match_digest,destination_hostgroup,apply)VALUES
    (1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);  #路由規則,讀語句到20組,其他到10組
    MySQL [(none)]> load mysql query rules to runtime;  #加載,使設置生效
    MySQL [(none)]> save mysql query rules to disk;     #保存到硬盤

    測試

    在client中執行命令查讀和寫的主機ID判斷是否完成讀寫分離
    在還沒有設置規則時;所有的訪問都發送到master機器上

    [root@Centos7 ~]#mysql -usqluser -pmagedu -P6033 -h192.168.37.27
    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, 2018, Oracle, MariaDB Corporation Ab and others.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    MySQL [(none)]> select @@server_id;
    +-------------+
    | @@server_id |
    +-------------+
    |           7 |   #主服務器(Master)ID 
    +-------------+
    1 row in set (0.01 sec)

    規設置成功後,select 語句被髮送到slave上執行,其他語句發到master上執行

    MySQL [(none)]> select @@server_id;
    +-------------+
    | @@server_id |
    +-------------+
    |          17 |   #從服務器(slave)ID 
    +-------------+
    1 row in set (0.00 sec)

查詢調度記錄

MySQL [(none)]> SELECT hostgroup hg,sum_time, count_star, digest_text
    -> FROM stats_mysql_query_digest ORDER BY sum_time DESC;
+----+----------+------------+----------------------------------+
| hg | sum_time | count_star | digest_text                      |
+----+----------+------------+----------------------------------+
| 20 | 32464    | 5          | select @@server_id               |
| 10 | 15435    | 12         | select @@server_id               |
| 10 | 9941     | 1          | show @@server_id                 |
| 10 | 0        | 1          | select @@version_comment limit ? |
+----+----------+------------+----------------------------------+
4 rows in set (0.00 sec)

MySQL [(none)]> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章