ip | 主机 | 服务 |
---|---|---|
192.168.69.134 | 读写分离解析主机 | proxysql |
192.168.69.130 | mysql-master | mysql |
192.168.69.132 | mysql-master2 | mysql |
192.168.69.5 | mysql-slave | mysql |
192.168.69.6 | mysql-slave2 | mysql |
[root@proxysql ~]# setenforce 0
[root@proxysql ~]# systemctl stop firewalld
[root@mysql-master ~]# yum -y install mariadb mariadb-server
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# systemctl enable mariadb
[root@mysql-master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@proxysql ~]# cat <
[root@mysql-master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
MariaDB [(none)]> grant all on *.* to 'proxysql'@'192.168.69.134' identified by 'pwproxysql';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
[root@proxysql ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
([email protected]:6032) [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.00 sec)
([email protected]:6032) [(none)]> show tables from main;
+--------------------------------------------+
| tables |
+--------------------------------------------+
| global_variables |
| mysql_collations |
| mysql_group_replication_hostgroups |
| mysql_query_rules |
| mysql_query_rules_fast_routing |
| mysql_replication_hostgroups |
| mysql_servers |
| mysql_users |
| proxysql_servers |
| runtime_checksums_values |
| runtime_global_variables |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_query_rules |
| runtime_mysql_query_rules_fast_routing |
| runtime_mysql_replication_hostgroups |
| runtime_mysql_servers |
| runtime_mysql_users |
| runtime_proxysql_servers |
| runtime_scheduler |
| scheduler |
+--------------------------------------------+
20 rows in set (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.69.130',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.69.132',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.69.5',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.69.6',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 1 | 192.168.69.130 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |
| 1 | 192.168.69.132 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |
| 2 | 192.168.69.5 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
| 2 | 192.168.69.6 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
4 rows in set (0.00 sec)
######修改后,需要加载到RUNTIME,并保存到disk######
([email protected]:6032) [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.03 sec)
([email protected]:6032) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','pwproxysql',1,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************
username: proxysql
password: pwproxysql
active: 1
use_ssl: 0
default_hostgroup: 1
default_schema: NULL
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 1
([email protected]:6032) [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.00 sec)
[root@mysql-master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
MariaDB [(none)]> GRANT SELECT ON *.* TO 'monitor'@'192.168.69.%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
([email protected]:6032) [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.01 sec)
添加读写分离的路由规则
需求:
将 select 查询语句全部路由至 hostgroup_id=2 的组(也就是读组)
但是 select * from tb for update 这样的语句是会修改数据的,所以需要单独定义,将它路由至 hostgroup_id=1 的组(也就是写组)
其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users 表中的 default_hostgroup)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1);
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^SELECT.*FOR UPDATE$ | 1 | 1 |
| 2 | 1 | ^SELECT | 2 | 1 |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)
([email protected]:6032) [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)
([email protected]:6032) [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.00 sec)
登录用户是刚才我们在 mysql_user 表中创建的用户,端口为6033
[root@proxysql ~]# mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
Welcome to the MariaDB monitor. Commands end with ; or \g.
([email protected]:6033) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
([email protected]:6033) [(none)]> create database momoda;
Query OK, 1 row affected (0.00 sec)
([email protected]:6033) [(none)]> select user,host from mysql.user;
+----------+-----------------+
| user | host |
+----------+-----------------+
| root | 127.0.0.1 |
| proxysql | 192.168.69.134 |
| root | ::1 |
| | localhost |
| root | localhost |
| | server01 |
| root | server01 |
+----------+-----------------+
7 rows in set (0.00 sec)
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
([email protected]:6032) [(none)]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 2 | information_schema | proxysql | 0x581BB90874C061B7 | select mysql,user from user | 1 | 1552551000 | 1552551000 | 1169816 | 1169816 | 1169816 |
| 2 | information_schema | proxysql | 0x2387402BA97B0B3F | select ?.?.?.?,user from user | 1 | 1552550928 | 1552550928 | 1813598 | 1813598 | 1813598 |
| 1 | information_schema | proxysql | 0xF8AA204B58F9D7C3 | create database momoda | 1 | 1552550078 | 1552550078 | 2627 | 2627 | 2627 |
| 2 | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 7 | 1552550180 | 1552550887 | 14445411 | 1174085 | 3458309 |
| 1 | information_schema | proxysql | 0x02033E45904D3DF0 | show databases | 2 | 1552550063 | 1552550978 | 8040 | 2241 | 5799 |
| 1 | information_schema | proxysql | 0x594F2C744B698066 | select USER() | 3 | 1552550050 | 1552550882 | 0 | 0 | 0 |
| 2 | information_schema | proxysql | 0x57E16F2FF7887CFE | select host,user from user | 1 | 1552550910 | 1552550910 | 1786983 | 1786983 | 1786983 |
| 1 | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 3 | 1552550050 | 1552550882 | 0 | 0 | 0 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
8 rows in set (0.00 sec)