图片 2

ProxySQL读写分离测试(续)

Login
ProxySQL configure the hostgroups.

    

Configure the query
rules.

1. Installation

1 [root@zlm1 17:22:46 /var/lib]
2 #service proxysql start
3 Starting ProxySQL: ProxySQL is already running.
4 
5 [root@zlm1 17:23:16 /var/lib]
6 #ps aux|grep proxysql
7 root       666  0.0  0.5  58180  5180 ?        S    15:06   0:00 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
8 root       667  0.0  2.0 104152 21068 ?        Sl   15:06   0:02 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
9 root      5523  0.0  0.0 112640   960 pts/1    R+   15:55   0:00 grep --color=auto proxysql

Percona
websit:

Configure the mysql
servers.

  1 [root@zlm2 09:54:24 /var/lib/proxysql]
  2 #ls -l
  3 total 748
  4 -rw------- 1 root root 122880 Aug 10 09:39 proxysql.db
  5 -rw------- 1 root root 347160 Aug 10 09:47 proxysql.log
  6 -rw-r--r-- 1 root root      5 Aug 10 08:30 proxysql.pid
  7 -rw------- 1 root root 110592 Aug 10 09:46 proxysql_stats.db
  8 -rw-r--r-- 1 root root      2 Aug 10 08:29 reload
  9 
 10 [root@zlm2 09:54:32 /var/lib/proxysql]
 11 #tail proxysql.log
 12 2018-08-10 09:54:30 MySQL_Monitor.cpp:605:monitor_read_only_thread(): [ERROR] Timeout on read_only check for 192.168.1.101:3308 after 0ms. Unable to create a connection. If the server is overload, increase mysql-monitor_connect_timeout. Error: timeout on creating new connection: Access denied for user 'monitor'@'192.168.1.101' (using password: YES).
 13 2018-08-10 09:54:30 MySQL_Monitor.cpp:724:monitor_read_only_thread(): [ERROR] Server 192.168.1.101:3308 missed 3 read_only checks. Assuming read_only=1
 14 2018-08-10 09:54:32 MySQL_Monitor.cpp:605:monitor_read_only_thread(): [ERROR] Timeout on read_only check for 192.168.1.103:3308 after 1ms. Unable to create a connection. If the server is overload, increase mysql-monitor_connect_timeout. Error: timeout on creating new connection: Access denied for user 'monitor'@'192.168.1.101' (using password: YES).
 15 2018-08-10 09:54:32 MySQL_Monitor.cpp:724:monitor_read_only_thread(): [ERROR] Server 192.168.1.103:3308 missed 3 read_only checks. Assuming read_only=1
 16 2018-08-10 09:54:32 MySQL_Monitor.cpp:605:monitor_read_only_thread(): [ERROR] Timeout on read_only check for 192.168.1.101:3308 after 0ms. Unable to create a connection. If the server is overload, increase mysql-monitor_connect_timeout. Error: timeout on creating new connection: Access denied for user 'monitor'@'192.168.1.101' (using password: YES).
 17 2018-08-10 09:54:32 MySQL_Monitor.cpp:724:monitor_read_only_thread(): [ERROR] Server 192.168.1.101:3308 missed 3 read_only checks. Assuming read_only=1
 18 2018-08-10 09:54:33 MySQL_Monitor.cpp:605:monitor_read_only_thread(): [ERROR] Timeout on read_only check for 192.168.1.101:3308 after 0ms. Unable to create a connection. If the server is overload, increase mysql-monitor_connect_timeout. Error: timeout on creating new connection: Access denied for user 'monitor'@'192.168.1.101' (using password: YES).
 19 2018-08-10 09:54:33 MySQL_Monitor.cpp:724:monitor_read_only_thread(): [ERROR] Server 192.168.1.101:3308 missed 3 read_only checks. Assuming read_only=1
 20 2018-08-10 09:54:33 MySQL_Monitor.cpp:605:monitor_read_only_thread(): [ERROR] Timeout on read_only check for 192.168.1.103:3308 after 1ms. Unable to create a connection. If the server is overload, increase mysql-monitor_connect_timeout. Error: timeout on creating new connection: Access denied for user 'monitor'@'192.168.1.101' (using password: YES).
 21 2018-08-10 09:54:33 MySQL_Monitor.cpp:724:monitor_read_only_thread(): [ERROR] Server 192.168.1.103:3308 missed 3 read_only checks. Assuming read_only=1
 22 
 23 [root@zlm2 09:54:18 /data/mysql/mysql3308/data]
 24 #tail error.log 
 25 2018-08-10T09:54:09.757619+01:00 773 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 26 2018-08-10T09:54:11.248093+01:00 774 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 27 2018-08-10T09:54:12.748305+01:00 775 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 28 2018-08-10T09:54:14.253693+01:00 776 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 29 2018-08-10T09:54:15.753818+01:00 777 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 30 2018-08-10T09:54:17.274359+01:00 778 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 31 2018-08-10T09:54:18.754761+01:00 779 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 32 2018-08-10T09:54:19.177448+01:00 780 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 33 2018-08-10T09:54:19.386483+01:00 781 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 34 2018-08-10T09:54:20.270737+01:00 782 [Note] Access denied for user 'monitor'@'192.168.1.101' (using password: YES)
 35 
 36 //The user "monitor" has no privileges to connect in MySQL instance yet.
 37 
 38 //Add monitor user "monitor" in the MySQL instance and grant privileges.
 39 (root@localhost mysql3308.sock)[(none)]>grant all privileges on *.* to monitor@'%' identified by 'monitor';
 40 Query OK, 0 rows affected, 1 warning (0.02 sec)
 41 
 42 (root@localhost mysql3308.sock)[(none)]>select user,host from mysql.user;
 43 +---------------+-------------+
 44 | user          | host        |
 45 +---------------+-------------+
 46 | monitor       | %           |
 47 | repl          | 192.168.1.% |
 48 | zlm           | 192.168.1.% |
 49 | mysql.session | localhost   |
 50 | mysql.sys     | localhost   |
 51 | root          | localhost   |
 52 | sst           | localhost   |
 53 +---------------+-------------+
 54 7 rows in set (0.00 sec)
 55 
 56 [root@zlm2 10:03:06 /var/lib/proxysql]
 57 #tail -20 proxysql.log
 58 2018-08-10 10:00:20 [INFO] New mysql_replication_hostgroups table
 59 writer_hostgroup: 10 , reader_hostgroup: 20, Test of ProxySQL
 60 2018-08-10 10:00:20 [INFO] New mysql_group_replication_hostgroups table
 61 2018-08-10 10:00:20 [INFO] Dumping current MySQL Servers structures for hostgroup ALL
 62 HID: 10 , address: 192.168.1.103 , port: 3308 , weight: 1 , status: ONLINE , max_connections: 50 , max_replication_lag: 10 , use_ssl: 0 , max_latency_ms: 1000000000 , comment: 
 63 HID: 10 , address: 192.168.1.101 , port: 3308 , weight: 1 , status: ONLINE , max_connections: 50 , max_replication_lag: 10 , use_ssl: 0 , max_latency_ms: 1000000000 , comment: 
 64 HID: 20 , address: 191.168.1.102 , port: 3308 , weight: 1 , status: ONLINE , max_connections: 50 , max_replication_lag: 10 , use_ssl: 0 , max_latency_ms: 1000000000 , comment: 
 65 HID: 20 , address: 192.168.1.103 , port: 3308 , weight: 1 , status: ONLINE , max_connections: 50 , max_replication_lag: 10 , use_ssl: 0 , max_latency_ms: 1000000000 , comment: 
 66 HID: 20 , address: 192.168.1.101 , port: 3308 , weight: 1 , status: ONLINE , max_connections: 50 , max_replication_lag: 10 , use_ssl: 0 , max_latency_ms: 1000000000 , comment: 
 67 2018-08-10 10:00:20 [INFO] Dumping mysql_servers
 68 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+-----------------+
 69 | hostgroup_id | hostname      | port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | mem_pointer     |
 70 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+-----------------+
 71 | 10           | 192.168.1.103 | 3308 | 1      | 0      | 0           | 50              | 10                  | 0       | 1000           |         | 139639198273536 |
 72 | 20           | 192.168.1.101 | 3308 | 1      | 0      | 0           | 50              | 10                  | 0       | 1000           |         | 139639198867456 |
 73 | 20           | 192.168.1.103 | 3308 | 1      | 0      | 0           | 50              | 10                  | 0       | 1000           |         | 139639212485120 |
 74 | 20           | 191.168.1.102 | 3308 | 1      | 0      | 0           | 50              | 10                  | 0       | 1000           |         | 139639212484992 |
 75 | 10           | 192.168.1.101 | 3308 | 1      | 0      | 0           | 50              | 10                  | 0       | 1000           |         | 139639198412544 |
 76 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+-----------------+
 77 2018-08-10 10:00:29 MySQL_Monitor.cpp:1437:monitor_ping(): [ERROR] Server 191.168.1.102:3308 missed 3 heartbeats, shunning it and killing all the connections. Disabling other checks until the node comes back online.
 78 
 79 //Now,the monitor user "monitor" of ProxySQL can get the monitor information.
 80 
 81 //Continue to configure the product user which we need to connect in MySQL instance.
 82 (admin@127.0.0.1 6032)[main]>show create table mysql_users\G
 83 *************************** 1. row ***************************
 84        table: mysql_users
 85 Create Table: CREATE TABLE mysql_users (
 86     username VARCHAR NOT NULL,
 87     password VARCHAR,
 88     active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1,
 89     use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0,
 90     default_hostgroup INT NOT NULL DEFAULT 0,
 91     default_schema VARCHAR,
 92     schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0,
 93     transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1,
 94     fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0,
 95     backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1,
 96     frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1,
 97     max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000,
 98     PRIMARY KEY (username, backend),
 99     UNIQUE (username, frontend))
100 1 row in set (0.00 sec)
101 
102 (admin@127.0.0.1 6032)[main]>insert into mysql_users(username,password,active,default_hostgroup,default_schema) values('zlm','zlmzlm',1,20,'zlm');
103 Query OK, 1 row affected (0.00 sec)
104 
105 (admin@127.0.0.1 6032)[main]>select * from mysql_users;
106 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
107 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
108 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
109 | zlm      | zlmzlm   | 1      | 0       | 20                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
110 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
111 1 row in set (0.00 sec)
112 
113 (admin@127.0.0.1 6032)[main]>load mysql users to runtime;save mysql users to disk;
114 Query OK, 0 rows affected (0.00 sec)
115 
116 Query OK, 0 rows affected (0.01 sec)
117 
118 (admin@127.0.0.1 6032)[main]>select * from runtime_mysql_users;
119 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
120 | username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
121 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
122 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 20                | zlm            | 0             | 1                      | 0            | 0       | 1        | 10000           |
123 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 20                | zlm            | 0             | 1                      | 0            | 1       | 0        | 10000           |
124 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
125 2 rows in set (0.00 sec)
126 
127 (admin@127.0.0.1 6032)[main]>select * from disk.mysql_users;
128 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
129 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
130 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
131 | zlm      | zlmzlm   | 1      | 0       | 20                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
132 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
133 1 row in set (0.00 sec)

 

 

 

https://github.com/sysown/proxysql/releases/

 

 

Configure
the mysql users.

 

 1 admin@127.0.0.1:6032 [(none)]>select * from mysql_users;
 2 Empty set (0.00 sec)
 3 
 4 admin@127.0.0.1:6032 [(none)]>insert into mysql_users(username,password,active,default_hostgroup,default_schema) values('zlm','zlmzlm',1,10,'zlm');
 5 Query OK, 1 row affected (0.00 sec)
 6 
 7 admin@127.0.0.1:6032 [(none)]>select * from mysql_users;
 8 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
 9 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
10 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
11 | zlm      | zlmzlm   | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
12 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
13 1 row in set (0.00 sec)
14 
15 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_users;
16 Empty set (0.00 sec)
17 
18 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_users;
19 Empty set (0.00 sec)
20 
21 admin@127.0.0.1:6032 [(none)]>load mysql users to runtime;save mysql users to disk;
22 Query OK, 0 rows affected (0.00 sec)
23 
24 Query OK, 0 rows affected (0.01 sec)
25 
26 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_users;
27 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
28 | username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
29 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
30 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 0       | 1        | 10000           |
31 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 0        | 10000           |
32 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
33 2 rows in set (0.00 sec)
34 
35 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_users;
36 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
37 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
38 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
39 | zlm      | zlmzlm   | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
40 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
41 1 row in set (0.00 sec)

 
 
ProxySQL is a low-weight proxy tool based on a SQLite
database.It provids hight performance espcially in high concurrent
environment what we can see below(compared with the MaxScale).

 

 

 1 admin@127.0.0.1:6032 [(none)]>select * from mysql_query_rules;
 2 Empty set (0.00 sec)
 3 
 4 admin@127.0.0.1:6032 [(none)]>insert into mysql_query_rules(active,username,match_pattern,schemaname,destination_hostgroup,apply) values(1,'zlm','^select','zlm',20,1);
 5 Query OK, 1 row affected (0.00 sec)
 6 
 7 admin@127.0.0.1:6032 [(none)]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from mysql_query_rules;
 8 +--------+----------+---------------+------------+-----------------------+-------+
 9 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
10 +--------+----------+---------------+------------+-----------------------+-------+
11 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
12 +--------+----------+---------------+------------+-----------------------+-------+
13 1 row in set (0.00 sec)
14 
15 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_query_rules;
16 Empty set (0.00 sec)
17 
18 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_query_rules;
19 Empty set (0.00 sec)
20 
21 admin@127.0.0.1:6032 [(none)]>load mysql query rules to runtime;save mysql query rules to disk;
22 Query OK, 0 rows affected (0.00 sec)
23 
24 admin@127.0.0.1:6032 [(none)]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from runtime_mysql_query_rules;
25 +--------+----------+---------------+------------+-----------------------+-------+
26 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
27 +--------+----------+---------------+------------+-----------------------+-------+
28 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
29 +--------+----------+---------------+------------+-----------------------+-------+
30 1 row in set (0.00 sec)
31 
32 admin@127.0.0.1:6032 [(none)]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from disk.mysql_query_rules;
33 +--------+----------+---------------+------------+-----------------------+-------+
34 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
35 +--------+----------+---------------+------------+-----------------------+-------+
36 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
37 +--------+----------+---------------+------------+-----------------------+-------+
38 1 row in set (0.00 sec)

 

 

Check
the default configuration file.

 

    As
we all know,it’s a common sense that separate reading and writing
operations can immensely increse the performance of MySQL
database.Especially the query operations by executing select statement
relevant with large tables.Therefore,we usually choose a proxy tool to
deal with it.There’re a lot of tools can be used nowadays such as
mycat(by Apache),dble(based on mycat by Action),atlas,dbproxy(based on
atlas of Qihoo360 by MeituanDianping),cetus(by NetEase) and so forth.I’m
not going to compare who’s the better tool to use.I’m just prefer to
having a test on another popular tool which is called
“ProxySQL”.

Start
ProxySQL.

 

 

 

Procedure

 

 1 [root@zlm1 17:27:11 ~]
 2 #mysql -uadmin -padmin -h127.0.0.1 -P6032
 3 mysql: [Warning] Using a password on the command line interface can be insecure.
 4 Welcome to the MySQL monitor.  Commands end with ; or \g.
 5 Your MySQL connection id is 1
 6 Server version: 5.5.30 (ProxySQL Admin Module)
 7 
 8 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
15 
16 admin@127.0.0.1:6032 [(none)]>select * from mysql_replication_hostgroups;
17 Empty set (0.00 sec)
18 
19 admin@127.0.0.1:6032 [(none)]>insert into mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup) values(10,20);
20 Query OK, 1 row affected (0.00 sec)
21 
22 admin@127.0.0.1:6032 [(none)]>select * from mysql_replication_hostgroups;
23 +------------------+------------------+---------+
24 | writer_hostgroup | reader_hostgroup | comment |
25 +------------------+------------------+---------+
26 | 10               | 20               |         |
27 +------------------+------------------+---------+
28 1 row in set (0.00 sec)
29 
30 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_replication_hostgroups;
31 Empty set (0.00 sec)
32 
33 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_replication_hostgroups;
34 Empty set (0.00 sec)
35 
36 admin@127.0.0.1:6032 [(none)]>load mysql servers to runtime;save mysql servers to disk;
37 Query OK, 0 rows affected (0.00 sec)
38 
39 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_replication_hostgroups;
40 +------------------+------------------+---------+
41 | writer_hostgroup | reader_hostgroup | comment |
42 +------------------+------------------+---------+
43 | 10               | 20               |         |
44 +------------------+------------------+---------+
45 1 row in set (0.00 sec)
46 
47 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_replication_hostgroups;
48 +------------------+------------------+---------+
49 | writer_hostgroup | reader_hostgroup | comment |
50 +------------------+------------------+---------+
51 | 10               | 20               |         |
52 +------------------+------------------+---------+
53 1 row in set (0.00 sec)

 

    I’ve implemented ProxySQL on PXC
yesterday but got some errors when configured query rules.I’m gonna do
it again in my master-slave environment again.Let’s see the
procedure.

 

  1 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
  2 Empty set (0.00 sec)
  3 
  4 admin@127.0.0.1:6032 [(none)]>insert into mysql_servers(hostgroup_id,hostname,max_connections,max_replication_lag) values(10,'192.168.56.100',100,300);
  5 Query OK, 1 row affected (0.00 sec)
  6 
  7 admin@127.0.0.1:6032 [(none)]>insert into mysql_servers(hostgroup_id,hostname,max_connections,max_replication_lag) values(20,'192.168.56.101',100,300);
  8 Query OK, 1 row affected (0.00 sec)
  9 
 10 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
 11 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 12 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
 13 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 14 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 15 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 16 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 17 2 rows in set (0.00 sec)
 18 
 19 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_servers;
 20 Empty set (0.00 sec)
 21 
 22 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_servers;
 23 Empty set (0.00 sec)
 24 
 25 admin@127.0.0.1:6032 [(none)]>load mysql servers to runtime;save mysql servers to disk;
 26 Query OK, 0 rows affected (0.00 sec)
 27 
 28 Query OK, 0 rows affected (0.05 sec)
 29 
 30 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_servers;
 31 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 32 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
 33 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 34 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 35 | 20           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 36 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 37 2 rows in set (0.00 sec)
 38 
 39 //Why does the hostgroup_id in table "runtime_mysql_servers" still "20"?
 40 
 41 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_servers;
 42 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 43 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
 44 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 45 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 46 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 47 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 48 2 rows in set (0.00 sec)
 49 
 50 //The hostgroup_id in table "disk.mysql_servers" has taken effect.
 51 
 52 //Check the variables of "read_only" and "super_read_only".
 53 zlm@192.168.56.100:3306 [(none)]>show variables like '%read_only%';
 54 +-----------------------+-------+
 55 | Variable_name         | Value |
 56 +-----------------------+-------+
 57 | innodb_read_only      | OFF   |
 58 | read_only             | OFF   |
 59 | super_read_only       | OFF   |
 60 | transaction_read_only | OFF   |
 61 | tx_read_only          | OFF   |
 62 +-----------------------+-------+
 63 5 rows in set (0.00 sec)
 64 
 65 admin@127.0.0.1:6032 [(none)]>load mysql servers to runtime;
 66 Query OK, 0 rows affected (0.00 sec)
 67 
 68 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_servers;
 69 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 70 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
 71 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 72 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 73 | 20           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 74 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 75 2 rows in set (0.00 sec)
 76 
 77 //It's still "20".
 78 
 79 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
 80 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 81 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
 82 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 83 | 20           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 84 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
 85 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
 86 2 rows in set (0.00 sec)
 87 
 88 //The "hostgroup_id" in mysql_serves also turned to be "20".Why does it happen?It's the reason why my query rule did not take effect yesterday.
 89 
 90 admin@127.0.0.1:6032 [(none)]>SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
 91 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
 92 | hostname       | port | time_start_us    | connect_success_time_us | connect_error                                                 |
 93 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
 94 | 192.168.56.100 | 3306 | 1533999241153417 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
 95 | 192.168.56.101 | 3306 | 1533999240299607 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
 96 | 192.168.56.101 | 3306 | 1533999181251828 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
 97 | 192.168.56.100 | 3306 | 1533999180299465 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
 98 | 192.168.56.101 | 3306 | 1533999120959376 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
 99 | 192.168.56.100 | 3306 | 1533999120299350 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
100 | 192.168.56.100 | 3306 | 1533999061361175 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
101 | 192.168.56.101 | 3306 | 1533999060299292 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
102 | 192.168.56.100 | 3306 | 1533999001472876 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
103 | 192.168.56.101 | 3306 | 1533999000299091 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
104 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
105 10 rows in set (0.00 sec)
106 
107 //It seems the privileges was configurated abnormally.
108 
109 zlm@192.168.56.100:3306 [(none)]>select user,host from mysql.user;
110 +---------------+--------------+
111 | user          | host         |
112 +---------------+--------------+
113 | repl          | 192.168.56.% |
114 | zlm           | 192.168.56.% |
115 | bkuser        | localhost    |
116 | monitor       | localhost    |
117 | mysql.session | localhost    |
118 | mysql.sys     | localhost    |
119 | root          | localhost    |
120 +---------------+--------------+
121 7 rows in set (0.00 sec)
122 
123 zlm@192.168.56.100:3306 [(none)]>drop user monitor@localhost;
124 Query OK, 0 rows affected (0.00 sec)
125 
126 zlm@192.168.56.100:3306 [(none)]>grant all privileges on *.* to monitor@'%' identified by 'monitor';
127 ERROR 1045 (28000): Access denied for user 'zlm'@'192.168.56.%' (using password: YES)
128 zlm@192.168.56.100:3306 [(none)]>exit
129 Bye
130 
131 [root@zlm1 17:59:50 /data/backup]
132 #mysql -uroot -pPassw0rd -hlocalhost -S /tmp/mysql3306.sock
133 mysql: [Warning] Using a password on the command line interface can be insecure.
134 Welcome to the MySQL monitor.  Commands end with ; or \g.
135 Your MySQL connection id is 2758
136 Server version: 5.7.21-log MySQL Community Server (GPL)
137 
138 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
139 
140 Oracle is a registered trademark of Oracle Corporation and/or its
141 affiliates. Other names may be trademarks of their respective
142 owners.
143 
144 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
145 
146 root@localhost:mysql3306.sock [(none)]>grant all privileges on *.* to monitor@'%' identified by 'monitor';
147 Query OK, 0 rows affected, 1 warning (0.00 sec)
148 
149 root@localhost:mysql3306.sock [(none)]>select user,host from mysql.user;
150 +---------------+--------------+
151 | user          | host         |
152 +---------------+--------------+
153 | monitor       | %            |
154 | repl          | 192.168.56.% |
155 | zlm           | 192.168.56.% |
156 | bkuser        | localhost    |
157 | mysql.session | localhost    |
158 | mysql.sys     | localhost    |
159 | root          | localhost    |
160 +---------------+--------------+
161 7 rows in set (0.00 sec)
162 
163 admin@127.0.0.1:6032 [(none)]>SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
164 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
165 | hostname       | port | time_start_us    | connect_success_time_us | connect_error                                                 |
166 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
167 | 192.168.56.101 | 3306 | 1533999721321021 | 1040                    | NULL                                                          |
168 | 192.168.56.100 | 3306 | 1533999720303754 | 441                     | NULL                                                          |
169 | 192.168.56.100 | 3306 | 1533999661174268 | 370                     | NULL                                                          |
170 | 192.168.56.101 | 3306 | 1533999660302486 | 1468                    | NULL                                                          |
171 | 192.168.56.100 | 3306 | 1533999601377823 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
172 | 192.168.56.101 | 3306 | 1533999600302394 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
173 | 192.168.56.101 | 3306 | 1533999541205310 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
174 | 192.168.56.100 | 3306 | 1533999540302168 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
175 | 192.168.56.101 | 3306 | 1533999480925661 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
176 | 192.168.56.100 | 3306 | 1533999480302043 | 0                       | Access denied for user 'monitor'@'zlm1' (using password: YES) |
177 +----------------+------+------------------+-------------------------+---------------------------------------------------------------+
178 10 rows in set (0.00 sec)
179 
180 //The monitor became normal.
181 
182 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
183 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
184 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
185 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
186 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
187 | 20           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
188 | 10           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
189 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
190 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
191 4 rows in set (0.00 sec)
192 
193 admin@127.0.0.1:6032 [(none)]>delete from mysql_servers;
194 Query OK, 4 rows affected (0.00 sec)
195 
196 admin@127.0.0.1:6032 [(none)]>insert into mysql_servers(hostgroup_id,hostname,max_connections,max_replication_lag) values(10,'192.168.56.100',100,300),(20,'192.168.56.101',100,300);
197 Query OK, 2 rows affected (0.00 sec)
198 
199 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
200 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
201 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
202 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
203 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
204 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
205 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
206 2 rows in set (0.00 sec)
207 
208 admin@127.0.0.1:6032 [(none)]>load mysql servers to runtime;save mysql servers to disk;
209 Query OK, 0 rows affected (0.00 sec)
210 
211 Query OK, 0 rows affected (0.02 sec)
212 
213 admin@127.0.0.1:6032 [(none)]>select * from mysql_servers;
214 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
215 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
216 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
217 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
218 | 10           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
219 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
220 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
221 3 rows in set (0.00 sec)
222 
223 //Why there're three records in table "mysql_servers"?
224 
225 admin@127.0.0.1:6032 [(none)]>select * from runtime_mysql_servers;
226 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
227 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
228 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
229 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
230 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
231 | 10           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
232 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
233 3 rows in set (0.00 sec)
234 
235 admin@127.0.0.1:6032 [(none)]>select * from disk.mysql_servers;
236 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
237 | hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
238 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
239 | 10           | 192.168.56.100 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
240 | 20           | 192.168.56.101 | 3306 | ONLINE | 1      | 0           | 100             | 300                 | 0       | 0              |         |
241 +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
242 2 rows in set (0.00 sec)
243 
244 admin@127.0.0.1:6032 [(none)]>show variables like '%also%';
245 +-------------------------------------+-------+
246 | Variable_name                       | Value |
247 +-------------------------------------+-------+
248 | mysql-monitor_writer_is_also_reader | true  |
249 +-------------------------------------+-------+
250 1 row in set (0.00 sec)
251 
252 admin@127.0.0.1:6032 [(none)]>SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
253 +----------------+------+------------------+-------------------------+---------------+
254 | hostname       | port | time_start_us    | connect_success_time_us | connect_error |
255 +----------------+------+------------------+-------------------------+---------------+
256 | 192.168.56.100 | 3306 | 1534001701142271 | 357                     | NULL          |
257 | 192.168.56.101 | 3306 | 1534001700318759 | 1260                    | NULL          |
258 | 192.168.56.101 | 3306 | 1534001641047510 | 1215                    | NULL          |
259 | 192.168.56.100 | 3306 | 1534001640318616 | 435                     | NULL          |
260 | 192.168.56.101 | 3306 | 1534001581271612 | 1089                    | NULL          |
261 | 192.168.56.100 | 3306 | 1534001580317548 | 201                     | NULL          |
262 | 192.168.56.100 | 3306 | 1534001521182217 | 1198                    | NULL          |
263 | 192.168.56.101 | 3306 | 1534001520317373 | 922                     | NULL          |
264 | 192.168.56.101 | 3306 | 1534001461241620 | 1110                    | NULL          |
265 | 192.168.56.100 | 3306 | 1534001460316352 | 326                     | NULL          |
266 +----------------+------+------------------+-------------------------+---------------+
267 10 rows in set (0.00 sec)

*    The configuration of ProxySQL is a three-layer structure:*

  1 admin@127.0.0.1:6032 [(none)]>show create table stats.stats_mysql_query_digest\G
  2 *************************** 1. row ***************************
  3        table: stats_mysql_query_digest
  4 Create Table: CREATE TABLE stats_mysql_query_digest (
  5     hostgroup INT,
  6     schemaname VARCHAR NOT NULL,
  7     username VARCHAR NOT NULL,
  8     digest VARCHAR NOT NULL,
  9     digest_text VARCHAR NOT NULL,
 10     count_star INTEGER NOT NULL,
 11     first_seen INTEGER NOT NULL,
 12     last_seen INTEGER NOT NULL,
 13     sum_time INTEGER NOT NULL,
 14     min_time INTEGER NOT NULL,
 15     max_time INTEGER NOT NULL,
 16     PRIMARY KEY(hostgroup, schemaname, username, digest))
 17 1 row in set (0.00 sec)
 18 
 19 admin@127.0.0.1:6032 [(none)]>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest_reset;
 20 Empty set (0.00 sec)
 21 
 22 [root@zlm1 18:36:17 /data/backup]
 23 #mysql -uzlm -pzlmzlm -h192.168.56.100 -P6033
 24 mysql: [Warning] Using a password on the command line interface can be insecure.
 25 Welcome to the MySQL monitor.  Commands end with ; or \g.
 26 Your MySQL connection id is 6
 27 Server version: 5.5.30 (ProxySQL)
 28 
 29 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 30 
 31 Oracle is a registered trademark of Oracle Corporation and/or its
 32 affiliates. Other names may be trademarks of their respective
 33 owners.
 34 
 35 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 36 
 37 zlm@192.168.56.100:6033 [(none)]>show tables;
 38 +---------------+
 39 | Tables_in_zlm |
 40 +---------------+
 41 | test          |
 42 +---------------+
 43 1 row in set (0.00 sec)
 44 
 45 zlm@192.168.56.100:6033 [(none)]>select * from test;
 46 +------+------+
 47 | id   | name |
 48 +------+------+
 49 |    1 | aaa  |
 50 |    2 | bbb  |
 51 |    3 | ccc  |
 52 +------+------+
 53 3 rows in set (0.00 sec)
 54 
 55 zlm@192.168.56.100:6033 [(none)]>insert into test values(4,'eee');
 56 Query OK, 1 row affected (0.00 sec)
 57 
 58 zlm@192.168.56.100:6033 [(none)]>update test set name='ddd' where id=4;
 59 Query OK, 1 row affected (0.00 sec)
 60 Rows matched: 1  Changed: 1  Warnings: 0
 61 
 62 zlm@192.168.56.100:6033 [(none)]>select * from test;
 63 +------+------+
 64 | id   | name |
 65 +------+------+
 66 |    1 | aaa  |
 67 |    2 | bbb  |
 68 |    3 | ccc  |
 69 |    4 | ddd  |
 70 +------+------+
 71 4 rows in set (0.00 sec)
 72 
 73 admin@127.0.0.1:6032 [(none)]>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest;
 74 +-----------+------------+----------+-----------------------------------+------------+
 75 | hostgroup | schemaname | username | substr(digest_text,120,-120)      | count_star |
 76 +-----------+------------+----------+-----------------------------------+------------+
 77 | 10        | zlm        | zlm      | update test set name=? where id=? | 1          |
 78 | 20        | zlm        | zlm      | select * from test                | 2          |
 79 | 10        | zlm        | zlm      | show tables                       | 1          |
 80 | 10        | zlm        | zlm      | select USER()                     | 1          |
 81 | 10        | zlm        | zlm      | insert into test values(?,?)      | 1          |
 82 | 10        | zlm        | zlm      | select @@version_comment limit ?  | 1          |
 83 +-----------+------------+----------+-----------------------------------+------------+
 84 6 rows in set (0.00 sec)
 85 
 86 //What baffled me is that why the "select USER()" and "select @@version_comment limit ?" were not in the hostgroup "20"?
 87 
 88 zlm@192.168.56.100:6033 [(none)]>select @@hostname;
 89 +------------+
 90 | @@hostname |
 91 +------------+
 92 | zlm2       |
 93 +------------+
 94 1 row in set (0.00 sec)
 95 
 96 //The select operation has been executed on slave zlm2.
 97 
 98 admin@127.0.0.1:6032 [(none)]>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest;
 99 +-----------+------------+----------+-----------------------------------+------------+
100 | hostgroup | schemaname | username | substr(digest_text,120,-120)      | count_star |
101 +-----------+------------+----------+-----------------------------------+------------+
102 | 10        | zlm        | zlm      | update test set name=? where id=? | 1          |
103 | 20        | zlm        | zlm      | select * from test                | 2          |
104 | 10        | zlm        | zlm      | show tables                       | 1          |
105 | 10        | zlm        | zlm      | select USER()                     | 1          |
106 | 20        | zlm        | zlm      | select @@hostname                 | 1          |
107 | 10        | zlm        | zlm      | insert into test values(?,?)      | 1          |
108 | 10        | zlm        | zlm      | select @@version_comment limit ?  | 1          |
109 +-----------+------------+----------+-----------------------------------+------------+
110 7 rows in set (0.00 sec)
111 
112 //The new statement of "select @@hostname" was put into hostgroup "20" correctly.
 1 (admin@127.0.0.1 6032)[main]>show create table mysql_query_rules\G
 2 *************************** 1. row ***************************
 3        table: mysql_query_rules
 4 Create Table: CREATE TABLE mysql_query_rules (
 5     rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 6     active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0,
 7     username VARCHAR,
 8     schemaname VARCHAR,
 9     flagIN INT NOT NULL DEFAULT 0,
10     client_addr VARCHAR,
11     proxy_addr VARCHAR,
12     proxy_port INT,
13     digest VARCHAR,
14     match_digest VARCHAR,
15     match_pattern VARCHAR,
16     negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0,
17     re_modifiers VARCHAR DEFAULT 'CASELESS',
18     flagOUT INT,
19     replace_pattern VARCHAR,
20     destination_hostgroup INT DEFAULT NULL,
21     cache_ttl INT CHECK(cache_ttl > 0),
22     reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL,
23     timeout INT UNSIGNED,
24     retries INT CHECK (retries>=0 AND retries <=1000),
25     delay INT UNSIGNED,
26     next_query_flagIN INT UNSIGNED,
27     mirror_flagOUT INT UNSIGNED,
28     mirror_hostgroup INT UNSIGNED,
29     error_msg VARCHAR,
30     OK_msg VARCHAR,
31     sticky_conn INT CHECK (sticky_conn IN (0,1)),
32     multiplex INT CHECK (multiplex IN (0,1,2)),
33     log INT CHECK (log IN (0,1)),
34     apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0,
35     comment VARCHAR)
36 1 row in set (0.00 sec)
37 
38 (admin@127.0.0.1 6032)[main]>insert into mysql_query_rules(active,username,match_pattern,schemaname,destination_hostgroup,apply) values(1,'zlm','^select','zlm',20,1);
39 Query OK, 1 row affected (0.00 sec)
40 
41 (admin@127.0.0.1 6032)[main]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from mysql_query_rules;
42 +--------+----------+---------------+------------+-----------------------+-------+
43 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
44 +--------+----------+---------------+------------+-----------------------+-------+
45 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
46 +--------+----------+---------------+------------+-----------------------+-------+
47 1 row in set (0.00 sec)
48 
49 (admin@127.0.0.1 6032)[main]>load mysql query rules to runtime;save mysql query rules to disk;
50 Query OK, 0 rows affected (0.00 sec)
51 
52 Query OK, 0 rows affected (0.02 sec)
53 
54 (admin@127.0.0.1 6032)[main]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from runtime_mysql_query_rules;
55 +--------+----------+---------------+------------+-----------------------+-------+
56 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
57 +--------+----------+---------------+------------+-----------------------+-------+
58 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
59 +--------+----------+---------------+------------+-----------------------+-------+
60 1 row in set (0.00 sec)
61 
62 (admin@127.0.0.1 6032)[main]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from disk.mysql_query_rules;
63 +--------+----------+---------------+------------+-----------------------+-------+
64 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
65 +--------+----------+---------------+------------+-----------------------+-------+
66 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
67 +--------+----------+---------------+------------+-----------------------+-------+
68 1 row in set (0.00 sec)

 

 

 

Configure
the hostgroup id.

Test whether proxy can seperate writing
and reading operations.

  • LOAD
    MYSQL object FROM MEMORY or LOAD MYSQL object TO RUNTIME 
  • SAVE
    MYSQL object TO MEMORY or SAVE MYSQL object FROM RUNTIME 
  • LOAD
    MYSQL object TO MEMORY or LOAD MYSQL object FROM DISK 
  • SAVE
    MYSQL object FROM MEMORY or SAVE MYSQL object TO DISK 
  • LOAD
    MYSQL object FROM CONFIG

 

Login MySQL Instance with product user
‘zlm’.

Preface

Download and install ProxySQL in rpm mode
on node zlm2.

 

The
comparison with other popular middleware tools.

Start ProxySQL and check tables of
it.

 

 

 

  1 //Connect with port 6033 to use the query rule of ProxySQL.
  2 [root@zlm2 11:13:16 /data/mysql/mysql3308/data]
  3 #mysql -uzlm -pzlmzlm -h192.168.1.101 -P6033
  4 mysql: [Warning] Using a password on the command line interface can be insecure.
  5 Welcome to the MySQL monitor.  Commands end with ; or \g.
  6 Your MySQL connection id is 4
  7 Server version: 5.5.30 (ProxySQL)
  8 
  9 Copyright (c) 2009-2018 Percona LLC and/or its affiliates
 10 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 11 
 12 Oracle is a registered trademark of Oracle Corporation and/or its
 13 affiliates. Other names may be trademarks of their respective
 14 owners.
 15 
 16 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 17 
 18 (zlm@192.168.1.101 6033)[(none)]>show tables; //The default database automatically became zlm although I have't specify which database to use.
 19 +---------------+
 20 | Tables_in_zlm |
 21 +---------------+
 22 | t1            |
 23 | t2            |
 24 +---------------+
 25 2 rows in set (0.00 sec)
 26 
 27 (zlm@192.168.1.101 6033)[(none)]>select * from t1;
 28 +----+------------+
 29 | id | name       |
 30 +----+------------+
 31 |  1 | MySQL      |
 32 |  2 | Oracle     |
 33 |  3 | PostgreSQL |
 34 |  4 | Redis      |
 35 +----+------------+
 36 4 rows in set (0.00 sec)
 37 
 38 (zlm@192.168.1.101 6033)[(none)]>insert into t1 values(5,'MongoDB');
 39 Query OK, 1 row affected (0.01 sec)
 40 
 41 (zlm@192.168.1.101 6033)[(none)]>select * from t1;
 42 +----+------------+
 43 | id | name       |
 44 +----+------------+
 45 |  1 | MySQL      |
 46 |  2 | Oracle     |
 47 |  3 | PostgreSQL |
 48 |  4 | Redis      |
 49 |  5 | MongoDB    |
 50 +----+------------+
 51 5 rows in set (0.00 sec)
 52 
 53 //Check the stats we've catched in "stats_mysql_query_digest" table of "stats" database of ProxySQL.
 54 (admin@127.0.0.1 6032)[main]>use stats;
 55 Reading table information for completion of table and column names
 56 You can turn off this feature to get a quicker startup with -A
 57 
 58 Database changed
 59 (admin@127.0.0.1 6032)[stats]>show tables; //Notice,if you don't specify "from xxx",it will show tables of "main" database.
 60 +--------------------------------------------+
 61 | tables                                     |
 62 +--------------------------------------------+
 63 | global_variables                           |
 64 | mysql_collations                           |
 65 | mysql_group_replication_hostgroups         |
 66 | mysql_query_rules                          |
 67 | mysql_query_rules_fast_routing             |
 68 | mysql_replication_hostgroups               |
 69 | mysql_servers                              |
 70 | mysql_users                                |
 71 | proxysql_servers                           |
 72 | runtime_checksums_values                   |
 73 | runtime_global_variables                   |
 74 | runtime_mysql_group_replication_hostgroups |
 75 | runtime_mysql_query_rules                  |
 76 | runtime_mysql_query_rules_fast_routing     |
 77 | runtime_mysql_replication_hostgroups       |
 78 | runtime_mysql_servers                      |
 79 | runtime_mysql_users                        |
 80 | runtime_proxysql_servers                   |
 81 | runtime_scheduler                          |
 82 | scheduler                                  |
 83 +--------------------------------------------+
 84 20 rows in set (0.00 sec)
 85 
 86 (admin@127.0.0.1 6032)[stats]>show tables from stats; //Speicify the target database to be "stats".
 87 +--------------------------------------+
 88 | tables                               |
 89 +--------------------------------------+
 90 | global_variables                     |
 91 | stats_memory_metrics                 |
 92 | stats_mysql_commands_counters        |
 93 | stats_mysql_connection_pool          |
 94 | stats_mysql_connection_pool_reset    |
 95 | stats_mysql_global                   |
 96 | stats_mysql_prepared_statements_info |
 97 | stats_mysql_processlist              |
 98 | stats_mysql_query_digest             |
 99 | stats_mysql_query_digest_reset       |
100 | stats_mysql_query_rules              |
101 | stats_mysql_users                    |
102 | stats_proxysql_servers_checksums     |
103 | stats_proxysql_servers_metrics       |
104 | stats_proxysql_servers_status        |
105 +--------------------------------------+
106 15 rows in set (0.00 sec)
107 
108 (admin@127.0.0.1 6032)[stats]>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest;
109 +-----------+------------+----------+----------------------------------+------------+
110 | hostgroup | schemaname | username | substr(digest_text,120,-120)     | count_star |
111 +-----------+------------+----------+----------------------------------+------------+
112 | 20        | zlm        | zlm      | select * from t1                 | 2          |
113 | 20        | zlm        | zlm      | insert into t1 values(?,?)       | 1          |
114 | 20        | zlm        | zlm      | select USER()                    | 2          |
115 | 20        | zlm        | zlm      | show tables                      | 1          |
116 | 20        | zlm        | zlm      | select @@version_comment limit ? | 2          |
117 +-----------+------------+----------+----------------------------------+------------+
118 5 rows in set (0.00 sec)
119 
120 //It's due to I've set "default_hostgroup" with "20".Actually it should be set with "10" of writer_hostgroup.
121 
122 (admin@127.0.0.1 6032)[stats]>select * from mysql_users;
123 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
124 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
125 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
126 | zlm      | zlmzlm   | 1      | 0       | 20                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
127 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
128 1 row in set (0.00 sec)
129 
130 (admin@127.0.0.1 6032)[stats]>update mysql_users set default_hostgroup=10;
131 Query OK, 1 row affected (0.00 sec)
132 
133 (admin@127.0.0.1 6032)[stats]>select * from mysql_users;
134 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
135 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
136 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
137 | zlm      | zlmzlm   | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
138 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
139 1 row in set (0.00 sec)
140 
141 (admin@127.0.0.1 6032)[stats]>load mysql users to runtime;save mysql users to disk;
142 Query OK, 0 rows affected (0.00 sec)
143 
144 Query OK, 0 rows affected (0.05 sec)
145 
146 (admin@127.0.0.1 6032)[stats]>select  * from runtime_mysql_users;
147 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
148 | username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
149 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
150 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 0       | 1        | 10000           |
151 | zlm      | *512FB3FAA6F522E351929CAC70AD8EBB0F5901B6 | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 0        | 10000           |
152 +----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
153 2 rows in set (0.00 sec)
154 
155 (admin@127.0.0.1 6032)[stats]>select  * from disk.mysql_users;
156 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
157 | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
158 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
159 | zlm      | zlmzlm   | 1      | 0       | 10                | zlm            | 0             | 1                      | 0            | 1       | 1        | 10000           |
160 +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
161 1 row in set (0.00 sec)
162 
163 //Clear the statistic data of table "stats_mysql_query_digest".
164 (admin@127.0.0.1 6032)[stats]>select * from stats_mysql_query_digest_reset;
165 +-----------+------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
166 | hostgroup | schemaname | username | digest             | digest_text                      | count_star | first_seen | last_seen  | sum_time | min_time | max_time |
167 +-----------+------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
168 | 20        | zlm        | zlm      | 0x3765930C7143F468 | select * from t1                 | 2          | 1533892415 | 1533892545 | 828      | 204      | 624      |
169 | 20        | zlm        | zlm      | 0x3CE4C46484576DFD | insert into t1 values(?,?)       | 1          | 1533892538 | 1533892538 | 13359    | 13359    | 13359    |
170 | 20        | zlm        | zlm      | 0x594F2C744B698066 | select USER()                    | 2          | 1533892377 | 1533892397 | 0        | 0        | 0        |
171 | 20        | zlm        | zlm      | 0x99531AEFF718C501 | show tables                      | 1          | 1533892400 | 1533892400 | 2607     | 2607     | 2607     |
172 | 20        | zlm        | zlm      | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 2          | 1533892377 | 1533892397 | 0        | 0        | 0        |
173 +-----------+------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
174 5 rows in set (0.01 sec)
175 
176 (admin@127.0.0.1 6032)[stats]>select * from stats_mysql_query_digest;
177 Empty set (0.00 sec)
178 
179 //Do the dml operations again.
180 (zlm@192.168.1.101 6033)[(none)]>show tables;
181 +---------------+
182 | Tables_in_zlm |
183 +---------------+
184 | t1            |
185 | t2            |
186 +---------------+
187 2 rows in set (0.00 sec)
188 
189 (zlm@192.168.1.101 6033)[(none)]>select * from t2;
190 +----+------------+
191 | id | name       |
192 +----+------------+
193 |  1 | MySQL      |
194 |  2 | Oracle     |
195 |  3 | SQL Server |
196 |  4 | Redis      |
197 |  5 | MongoDB    |
198 |  6 | PostgreSQL |
199 +----+------------+
200 6 rows in set (0.01 sec)
201 
202 (zlm@192.168.1.101 6033)[(none)]>insert into t2 values(7,'Hadoop');
203 Query OK, 1 row affected (0.01 sec)
204 
205 //Check the diagnostic data in table "stats_mysql_query_digest" again.
206 (admin@127.0.0.1 6032)[stats]>select active,username,match_pattern,schemaname,destination_hostgroup,apply from mysql_query_rules;
207 +--------+----------+---------------+------------+-----------------------+-------+
208 | active | username | match_pattern | schemaname | destination_hostgroup | apply |
209 +--------+----------+---------------+------------+-----------------------+-------+
210 | 1      | zlm      | ^select       | zlm        | 20                    | 1     |
211 +--------+----------+---------------+------------+-----------------------+-------+
212 1 row in set (0.00 sec)
213 
214 (admin@127.0.0.1 6032)[stats]>select hostgroup,schemaname,username,substr(digest_text,120,-120),count_star from stats_mysql_query_digest;
215 +-----------+------------+----------+------------------------------+------------+
216 | hostgroup | schemaname | username | substr(digest_text,120,-120) | count_star |
217 +-----------+------------+----------+------------------------------+------------+
218 | 20        | zlm        | zlm      | insert into t2 values(?,?)   | 1          |
219 | 20        | zlm        | zlm      | select * from t2             | 1          |
220 | 20        | zlm        | zlm      | show tables                  | 1          |
221 +-----------+------------+----------+------------------------------+------------+
222 3 rows in set (0.00 sec)
223 
224 //It still doesn't take effect.What's wrong with it?

http://www.proxysql.com/compare

Configure the MySQL servers in different
hostgroup.

图片 1

 

 

2. Configure the separation of reading and
writing.

Github websit:

 

 

Procedure

Official website:

Preface

  1 [root@zlm2 08:05:09 ~]
  2 #rpm -ql proxysql
  3 /etc/init.d/proxysql
  4 /etc/proxysql.cnf //This is the configuration file of ProxySQL.
  5 /usr/bin/proxysql
  6 /usr/share/proxysql/tools/proxysql_galera_checker.sh //This is the script to check the status of hostgroups.
  7 /usr/share/proxysql/tools/proxysql_galera_writer.pl
  8 
  9 [root@zlm2 08:08:13 ~]
 10 #cat /etc/proxysql.cnf
 11 #file proxysql.cfg
 12 
 13 ########################################################################################
 14 # This config file is parsed using libconfig , and its grammar is described in:        
 15 # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar 
 16 # Grammar is also copied at the end of this file                                       
 17 ########################################################################################
 18 
 19 ########################################################################################
 20 # IMPORTANT INFORMATION REGARDING THIS CONFIGURATION FILE:                             
 21 ########################################################################################
 22 # On startup, ProxySQL reads its config file (if present) to determine its datadir. 
 23 # What happens next depends on if the database file (disk) is present in the defined
 24 # datadir (i.e. "/var/lib/proxysql/proxysql.db").
 25 #
 26 # If the database file is found, ProxySQL initializes its in-memory configuration from 
 27 # the persisted on-disk database. So, disk configuration gets loaded into memory and 
 28 # then propagated towards the runtime configuration. 
 29 #
 30 # If the database file is not found and a config file exists, the config file is parsed 
 31 # and its content is loaded into the in-memory database, to then be both saved on-disk 
 32 # database and loaded at runtime.
 33 #
 34 # IMPORTANT: If a database file is found, the config file is NOT parsed. In this case
 35 #            ProxySQL initializes its in-memory configuration from the persisted on-disk
 36 #            database ONLY. In other words, the configuration found in the proxysql.cnf
 37 #            file is only used to initial the on-disk database read on the first startup.
 38 #
 39 # In order to FORCE a re-initialise of the on-disk database from the configuration file 
 40 # the ProxySQL service should be started with "service proxysql initial".
 41 #
 42 ########################################################################################
 43 
 44 datadir="/var/lib/proxysql" //This is the position of SQLite database of ProxySQL.
 45 
 46 admin_variables=
 47 {
 48     admin_credentials="admin:admin" //The default user/password is admin/admin.
 49 #    mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
 50     mysql_ifaces="0.0.0.0:6032" //The default admin port is 6032.
 51 #    refresh_interval=2000
 52 #    debug=true
 53 }
 54 
 55 mysql_variables=
 56 {
 57     threads=4
 58     max_connections=2048
 59     default_query_delay=0
 60     default_query_timeout=36000000
 61     have_compress=true
 62     poll_timeout=2000
 63 #    interfaces="0.0.0.0:6033;/tmp/proxysql.sock"
 64     interfaces="0.0.0.0:6033" //The default service port is 6033.
 65     default_schema="information_schema"
 66     stacksize=1048576
 67     server_version="5.5.30"
 68     connect_timeout_server=3000
 69 # make sure to configure monitor username and password
 70 # https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_password
 71     monitor_username="monitor"
 72     monitor_password="monitor"
 73     monitor_history=600000
 74     monitor_connect_interval=60000
 75     monitor_ping_interval=10000
 76     monitor_read_only_interval=1500
 77     monitor_read_only_timeout=500
 78     ping_interval_server_msec=120000
 79     ping_timeout_server=500
 80     commands_stats=true
 81     sessions_sort=true
 82     connect_retries_on_failure=10
 83 }
 84 
 85 
 86 # defines all the MySQL servers
 87 mysql_servers =
 88 (
 89 #    {
 90 #        address = "127.0.0.1" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
 91 #        port = 3306           # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
 92 #        hostgroup = 0            # no default, required
 93 #        status = "ONLINE"     # default: ONLINE
 94 #        weight = 1            # default: 1
 95 #        compression = 0       # default: 0
 96 #   max_replication_lag = 10  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
 97 #    },
 98 #    {
 99 #        address = "/var/lib/mysql/mysql.sock"
100 #        port = 0
101 #        hostgroup = 0
102 #    },
103 #    {
104 #        address="127.0.0.1"
105 #        port=21891
106 #        hostgroup=0
107 #        max_connections=200
108 #    },
109 #    { address="127.0.0.2" , port=3306 , hostgroup=0, max_connections=5 },
110 #    { address="127.0.0.1" , port=21892 , hostgroup=1 },
111 #    { address="127.0.0.1" , port=21893 , hostgroup=1 }
112 #    { address="127.0.0.2" , port=3306 , hostgroup=1 },
113 #    { address="127.0.0.3" , port=3306 , hostgroup=1 },
114 #    { address="127.0.0.4" , port=3306 , hostgroup=1 },
115 #    { address="/var/lib/mysql/mysql.sock" , port=0 , hostgroup=1 }
116 )
117 
118 
119 # defines all the MySQL users
120 mysql_users:
121 (
122 #    {
123 #        username = "username" # no default , required
124 #        password = "password" # default: ''
125 #        default_hostgroup = 0 # default: 0
126 #        active = 1            # default: 1
127 #    },
128 #    {
129 #        username = "root"
130 #        password = ""
131 #        default_hostgroup = 0
132 #        max_connections=1000
133 #        default_schema="test"
134 #        active = 1
135 #    },
136 #    { username = "user1" , password = "password" , default_hostgroup = 0 , active = 0 }
137 )
138 
139 
140 
141 #defines MySQL Query Rules
142 mysql_query_rules:
143 (
144 #    {
145 #        rule_id=1
146 #        active=1
147 #        match_pattern="^SELECT .* FOR UPDATE$"
148 #        destination_hostgroup=0
149 #        apply=1
150 #    },
151 #    {
152 #        rule_id=2
153 #        active=1
154 #        match_pattern="^SELECT"
155 #        destination_hostgroup=1
156 #        apply=1
157 #    }
158 )
159 
160 scheduler=
161 (
162 #  {
163 #    id=1
164 #    active=0
165 #    interval_ms=10000
166 #    filename="/var/lib/proxysql/proxysql_galera_checker.sh"
167 #    arg1="0"
168 #    arg2="0"
169 #    arg3="0"
170 #    arg4="1"
171 #    arg5="/var/lib/proxysql/proxysql_galera_checker.log"
172 #  }
173 )
174 
175 
176 mysql_replication_hostgroups=
177 (
178 #        {
179 #                writer_hostgroup=30
180 #                reader_hostgroup=40
181 #                comment="test repl 1"
182 #       },
183 #       {
184 #                writer_hostgroup=50
185 #                reader_hostgroup=60
186 #                comment="test repl 2"
187 #        }
188 )
189 
190 
191 
192 
193 # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
194 #
195 # Below is the BNF grammar for configuration files. Comments and include directives are not part of the grammar, so they are not included here. 
196 #
197 # configuration = setting-list | empty
198 #
199 # setting-list = setting | setting-list setting
200 #     
201 # setting = name (":" | "=") value (";" | "," | empty)
202 #     
203 # value = scalar-value | array | list | group
204 #     
205 # value-list = value | value-list "," value
206 #     
207 # scalar-value = boolean | integer | integer64 | hex | hex64 | float
208 #                | string
209 #     
210 # scalar-value-list = scalar-value | scalar-value-list "," scalar-value
211 #     
212 # array = "[" (scalar-value-list | empty) "]"
213 #     
214 # list = "(" (value-list | empty) ")"
215 #     
216 # group = "{" (setting-list | empty) "}"
217 #     
218 # empty =

3. Test dml operations

 

  1 [root@zlm2 08:30:12 ~]
  2 #service proxysql start
  3 Starting ProxySQL: 2018-08-10 08:30:38 [INFO] Using config file /etc/proxysql.cnf
  4 DONE!
  5 
  6 [root@zlm2 08:30:38 ~]
  7 #ps aux|grep proxysql
  8 root      4307  0.0  0.5  58688  5184 ?        S    08:30   0:00 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
  9 root      4308  0.3  1.8 102612 19020 ?        Sl   08:30   0:00 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
 10 root      4335  0.0  0.0 112640   960 pts/0    R+   08:30   0:00 grep --color=auto proxysql
 11 
 12 [root@zlm2 08:32:20 ~]
 13 #cd /var/lib/proxysql
 14 
 15 [root@zlm2 08:33:55 /var/lib/proxysql]
 16 #ls -l
 17 total 244
 18 -rw------- 1 root root 122880 Aug 10 08:16 proxysql.db
 19 -rw------- 1 root root   7320 Aug 10 08:30 proxysql.log
 20 -rw-r--r-- 1 root root      5 Aug 10 08:30 proxysql.pid
 21 -rw------- 1 root root 110592 Aug 10 08:33 proxysql_stats.db
 22 -rw-r--r-- 1 root root      2 Aug 10 08:29 reload
 23 
 24 [root@zlm2 08:33:57 /var/lib/proxysql]
 25 #mysql -uadmin -padmin -h127.0.0.1 -P6032
 26 mysql: [Warning] Using a password on the command line interface can be insecure.
 27 Welcome to the MySQL monitor.  Commands end with ; or \g.
 28 Your MySQL connection id is 1
 29 Server version: 5.5.30 (ProxySQL Admin Module)
 30 
 31 Copyright (c) 2009-2018 Percona LLC and/or its affiliates
 32 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 33 
 34 Oracle is a registered trademark of Oracle Corporation and/or its
 35 affiliates. Other names may be trademarks of their respective
 36 owners.
 37 
 38 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 39 
 40 (admin@127.0.0.1 6032)[(none)]>show databases;
 41 +-----+---------------+-------------------------------------+
 42 | seq | name          | file                                |
 43 +-----+---------------+-------------------------------------+
 44 | 0   | main          |                                     |
 45 | 2   | disk          | /var/lib/proxysql/proxysql.db       |
 46 | 3   | stats         |                                     |
 47 | 4   | monitor       |                                     |
 48 | 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
 49 +-----+---------------+-------------------------------------+
 50 5 rows in set (0.00 sec)
 51 
 52 //There's five databases in all in PorxySQL database.In the previous version,there's no stats_history database;
 53 
 54 (admin@127.0.0.1 6032)[(none)]>show tables from main;
 55 +--------------------------------------------+
 56 | tables                                     |
 57 +--------------------------------------------+
 58 | global_variables                           | //It contains all the variables of ProxySQL.
 59 | mysql_collations                           |
 60 | mysql_group_replication_hostgroups         |
 61 | mysql_query_rules                          | //It contains all the query rules we'd like to use.
 62 | mysql_query_rules_fast_routing             |
 63 | mysql_replication_hostgroups               |
 64 | mysql_servers                              | //It contains the information of all servers relevant with MySQL.
 65 | mysql_users                                | //It contains the information of all users relevant with MySQL.
 66 | proxysql_servers                           | //It contains the information of all servers relevant with ProxySQL.
 67 | runtime_checksums_values                   |
 68 | runtime_global_variables                   |
 69 | runtime_mysql_group_replication_hostgroups |
 70 | runtime_mysql_query_rules                  |
 71 | runtime_mysql_query_rules_fast_routing     |
 72 | runtime_mysql_replication_hostgroups       |
 73 | runtime_mysql_servers                      |
 74 | runtime_mysql_users                        |
 75 | runtime_proxysql_servers                   |
 76 | runtime_scheduler                          |
 77 | scheduler                                  |
 78 +--------------------------------------------+
 79 20 rows in set (0.00 sec)
 80 
 81 //All the "runtime" prefixed tables in "main" database contain the current configuration in runtime mode.The other tables can be modified in memory mode.
 82 
 83 (admin@127.0.0.1 6032)[(none)]>show tables from disk;
 84 +------------------------------------+
 85 | tables                             |
 86 +------------------------------------+
 87 | global_variables                   |
 88 | mysql_collations                   |
 89 | mysql_group_replication_hostgroups |
 90 | mysql_query_rules                  |
 91 | mysql_query_rules_fast_routing     |
 92 | mysql_replication_hostgroups       |
 93 | mysql_servers                      |
 94 | mysql_users                        |
 95 | proxysql_servers                   |
 96 | scheduler                          |
 97 +------------------------------------+
 98 10 rows in set (0.00 sec)
 99 
100 //All the tables contains the relevant data which we have saved in disk by "SAVE MYSQL xxx TO DISK;" command.
101 //All the tables have the the same structure as those in "main" database.
102 
103 (admin@127.0.0.1 6032)[(none)]>show tables from monitor;
104 +------------------------------------+
105 | tables                             |
106 +------------------------------------+
107 | mysql_server_connect_log           |
108 | mysql_server_group_replication_log |
109 | mysql_server_ping_log              |
110 | mysql_server_read_only_log         |
111 | mysql_server_replication_lag_log   |
112 +------------------------------------+
113 5 rows in set (0.00 sec)
114 
115 (admin@127.0.0.1 6032)[(none)]>show tables from stats;
116 +--------------------------------------+
117 | tables                               |
118 +--------------------------------------+
119 | global_variables                     |
120 | stats_memory_metrics                 |
121 | stats_mysql_commands_counters        |
122 | stats_mysql_connection_pool          |
123 | stats_mysql_connection_pool_reset    |
124 | stats_mysql_global                   |
125 | stats_mysql_prepared_statements_info |
126 | stats_mysql_processlist              |
127 | stats_mysql_query_digest             |
128 | stats_mysql_query_digest_reset       |
129 | stats_mysql_query_rules              |
130 | stats_mysql_users                    |
131 | stats_proxysql_servers_checksums     |
132 | stats_proxysql_servers_metrics       |
133 | stats_proxysql_servers_status        |
134 +--------------------------------------+
135 15 rows in set (0.00 sec)
 1 (admin@127.0.0.1 6032)[main]>show create table mysql_replication_hostgroups\G
 2 *************************** 1. row ***************************
 3        table: mysql_replication_hostgroups
 4 Create Table: CREATE TABLE mysql_replication_hostgroups (
 5     writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
 6     reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>0),
 7     comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))
 8 1 row in set (0.00 sec)
 9 
10 (admin@127.0.0.1 6032)[main]>insert into mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup,comment) values(10,20,'Test of ProxySQL');
11 Query OK, 1 row affected (0.00 sec)
12 
13 (admin@127.0.0.1 6032)[main]>select * from mysql_replication_hostgroups;
14 +------------------+------------------+------------------+
15 | writer_hostgroup | reader_hostgroup | comment          |
16 +------------------+------------------+------------------+
17 | 10               | 20               | Test of ProxySQL |
18 +------------------+------------------+------------------+
19 1 row in set (0.00 sec)
20 
21 (admin@127.0.0.1 6032)[main]>select * from main.runtime_mysql_replication_hostgroups;
22 Empty set (0.00 sec)
23 
24 (admin@127.0.0.1 6032)[main]>select * from disk.mysql_replication_hostgroups;
25 Empty set (0.00 sec)
26 
27 (admin@127.0.0.1 6032)[main]>load mysql servers to runtime;save mysql servers to disk;
28 Query OK, 0 rows affected (0.00 sec)
29 
30 Query OK, 0 rows affected (0.04 sec)
31 
32 (admin@127.0.0.1 6032)[main]>select * from main.runtime_mysql_replication_hostgroups;
33 +------------------+------------------+------------------+
34 | writer_hostgroup | reader_hostgroup | comment          |
35 +------------------+------------------+------------------+
36 | 10               | 20               | Test of ProxySQL |
37 +------------------+------------------+------------------+
38 1 row in set (0.00 sec)
39 
40 (admin@127.0.0.1 6032)[main]>select * from disk.mysql_replication_hostgroups;
41 +------------------+------------------+------------------+
42 | writer_hostgroup | reader_hostgroup | comment          |
43 +------------------+------------------+------------------+
44 | 10               | 20               | Test of ProxySQL |
45 +------------------+------------------+------------------+
46 1 row in set (0.00 sec)

Configure the mysql query
rules.

 

 

 

 

  *  We
usually conifuge the parameter in layer of memory,and then load them
into layer of runtime to make it take effect.In the end,we should save
them to disk for durability storage.
It also provides some simple syntax
to transfer configurations between those layers as below:*

图片 2

http://www.percona.com/downloads/proxysql

 

http://www.proxysql.com/

 

Configure the monitor user and product
user.

 1 (admin@127.0.0.1 6032)[main]>show create table mysql_servers\G
 2 *************************** 1. row ***************************
 3        table: mysql_servers
 4 Create Table: CREATE TABLE mysql_servers (
 5     hostgroup_id INT CHECK (hostgroup_id>=0) NOT NULL DEFAULT 0,
 6     hostname VARCHAR NOT NULL,
 7     port INT NOT NULL DEFAULT 3306,
 8     status VARCHAR CHECK (UPPER(status) IN ('ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD')) NOT NULL DEFAULT 'ONLINE',
 9     weight INT CHECK (weight >= 0) NOT NULL DEFAULT 1,
10     compression INT CHECK (compression >=0 AND compression <= 102400) NOT NULL DEFAULT 0,
11     max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000,
12     max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0,
13     use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0,
14     max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0,
15     comment VARCHAR NOT NULL DEFAULT '',
16     PRIMARY KEY (hostgroup_id, hostname, port) )
17 1 row in set (0.00 sec)
18 
19 (admin@127.0.0.1 6032)[main]>insert into mysql_servers(hostgrop_id,hostname,port,max_connections,max_replication_lag,max_latency_ms) values(10,'192.168.1.101',3308,50,10,1000),(20,'192.168.1.102',3308,50,10,1000),(20,'192.168.1.103',3308,50,10,1000);
20 Query OK, 3 row affected (0.01 sec)
21 
22 (admin@127.0.0.1 6032)[main]>select * from mysql_servers;
23 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
24 | hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
25 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
26 | 10           | 192.168.1.101 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
27 | 20           | 191.168.1.102 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
28 | 20           | 192.168.1.103 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
29 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
30 3 rows in set (0.00 sec)
31 
32 (admin@127.0.0.1 6032)[main]>select * from runtime_mysql_servers;
33 Empty set (0.00 sec)
34 
35 (admin@127.0.0.1 6032)[main]>select * from disk.mysql_servers;
36 Empty set (0.00 sec)
37 
38 (admin@127.0.0.1 6032)[main]>load mysql servers to runtime;save mysql servers to disk;
39 Query OK, 0 rows affected (0.01 sec)
40 
41 Query OK, 0 rows affected (0.03 sec)
42 
43 (admin@127.0.0.1 6032)[main]>select * from runtime_mysql_servers;
44 +--------------+---------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
45 | hostgroup_id | hostname      | port | status  | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
46 +--------------+---------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
47 | 20           | 191.168.1.102 | 3308 | SHUNNED | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
48 | 20           | 192.168.1.101 | 3308 | ONLINE  | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
49 | 20           | 192.168.1.103 | 3308 | ONLINE  | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
50 +--------------+---------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
51 3 rows in set (0.00 sec)
52 
53 (admin@127.0.0.1 6032)[main]>select * from disk.mysql_servers;
54 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
55 | hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
56 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
57 | 10           | 192.168.1.101 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
58 | 20           | 191.168.1.102 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
59 | 20           | 192.168.1.103 | 3308 | ONLINE | 1      | 0           | 50              | 10                  | 0       | 1000           |         |
60 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
61 3 rows in set (0.00 sec)
 1 [root@zlm2 08:00:34 ~]
 2 #wget https://github.com/sysown/proxysql/releases/download/v1.4.10/proxysql-1.4.10-1-centos7.x86_64.rpm
 3 --2018-08-10 08:01:20--  https://github.com/sysown/proxysql/releases/download/v1.4.10/proxysql-1.4.10-1-centos7.x86_64.rpm
 4 Resolving github.com (github.com)... 13.229.188.59, 52.74.223.119, 13.250.177.223
 5 Connecting to github.com (github.com)|13.229.188.59|:443... connected.
 6 HTTP request sent, awaiting response... 302 Found
 7 Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/27358084/305d1618-9a5b-11e8-9b04-df8e3393a6b4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180810%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180810T060119Z&X-Amz-Expires=300&X-Amz-Signature=f4f676653d4cd4f34f4b75b2a4e7dfeaee8bf9d8e35dc91938f41961123a9c9f&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dproxysql-1.4.10-1-centos7.x86_64.rpm&response-content-type=application%2Foctet-stream [following]
 8 --2018-08-10 08:01:29--  https://github-production-release-asset-2e65be.s3.amazonaws.com/27358084/305d1618-9a5b-11e8-9b04-df8e3393a6b4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180810%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180810T060119Z&X-Amz-Expires=300&X-Amz-Signature=f4f676653d4cd4f34f4b75b2a4e7dfeaee8bf9d8e35dc91938f41961123a9c9f&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dproxysql-1.4.10-1-centos7.x86_64.rpm&response-content-type=application%2Foctet-stream
 9 Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 54.231.32.91
10 Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|54.231.32.91|:443... connected.
11 HTTP request sent, awaiting response... 200 OK
12 Length: 5982016 (5.7M) [application/octet-stream]
13 Saving to: ‘proxysql-1.4.10-1-centos7.x86_64.rpm’
14 
15 100%[===========================================================================================================>] 5,982,016   1.12MB/s   in 12s    
16 
17 2018-08-10 08:02:14 (472 KB/s) - ‘proxysql-1.4.10-1-centos7.x86_64.rpm’ saved [5982016/5982016]
18 
19 
20 [root@zlm2 08:02:14 ~]
21 #ls -l|grep proxysql
22 -rw-r--r--   1 root root    5982016 Aug  7 15:03 proxysql-1.4.10-1-centos7.x86_64.rpm
23 
24 [root@zlm2 08:04:48 ~]
25 #yum -y localinstall proxysql-1.4.10-1-centos7.x86_64.rpm
26 Loaded plugins: fastestmirror
27 Examining proxysql-1.4.10-1-centos7.x86_64.rpm: proxysql-1.4.10-1.x86_64
28 Marking proxysql-1.4.10-1-centos7.x86_64.rpm to be installed
29 Resolving Dependencies
30 --> Running transaction check
31 ---> Package proxysql.x86_64 0:1.4.10-1 will be installed
32 --> Finished Dependency Resolution
33 
34 Dependencies Resolved
35 
36 =====================================================================================================================================================
37  Package                      Arch                       Version                         Repository                                             Size
38 =====================================================================================================================================================
39 Installing:
40  proxysql                     x86_64                     1.4.10-1                        /proxysql-1.4.10-1-centos7.x86_64                      22 M
41 
42 Transaction Summary
43 =====================================================================================================================================================
44 Install  1 Package
45 
46 Total size: 22 M
47 Installed size: 22 M
48 Downloading packages:
49 Running transaction check
50 Running transaction test
51 Transaction test succeeded
52 Running transaction
53   Installing : proxysql-1.4.10-1.x86_64                                                                                                          1/1 
54   Verifying  : proxysql-1.4.10-1.x86_64                                                                                                          1/1 
55 
56 Installed:
57   proxysql.x86_64 0:1.4.10-1                                                                                                                         
58 
59 Complete!

Introduce