#创建proxysql用户及监控用户
mysql> create user 'proxy'@'%' identified with mysql_native_password by 'Jianren@123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to proxy;
Query OK, 0 rows affected (0.00 sec)
mysql> create user 'monitor'@'%' identified with mysql_native_password by 'Jianren@123' ;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to monitor ;
Query OK, 0 rows affected (0.00 sec)
# 创建规则写入,更新操作使用主节点
Admin> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(1,1,'^select.*from update$',1,1);
Query OK, 1 row affected (0.00 sec)
#创建规则查询数据使用从节点
Admin> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(2,1,'^select',2,1);
Query OK, 1 row affected (0.00 sec)
#创建规则查询库使用从节点
Admin> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(3,1,'^show',2,1);
Query OK, 1 row affected (0.00 sec)
# 查看规则
Admin> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+-----------------------+-----------------------+-------+
| rule_id | active | match_digest | destination_hostgroup | apply |
+---------+--------+-----------------------+-----------------------+-------+
| 1 | 1 | ^select.*from update$ | 1 | 1 |
| 2 | 1 | ^select | 2 | 1 |
| 3 | 1 | ^show | 2 | 1 |
+---------+--------+-----------------------+-----------------------+-------+
3 rows in set (0.00 sec)
# 载入使用并持久化到磁盘
Admin> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
Admin> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
Admin> save mysql query rules to disk ;
Query OK, 0 rows affected (0.02 sec)
Admin> save admin variables to disk ;
Query OK, 49 rows affected (0.00 sec)
插入数据,查询数据
# 切换proxy用户
[root@localhost ~]# mysql -uproxy -pJianren@123 -P6033 -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> USE db1;
Database changed
mysql> CREATE TABLE test1 (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255));
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO test1 (id, name) VALUES(1, 'kk'),(2, 'nameqq');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查询记录验证
# 切换管理 查询
mysql -uadmin -padmin -P6032 -h127.0.0.1 --prompt='Admin>'
Admin> select * from stats_mysql_query_digest \G
规则生效