ProxySQL实现双主双从读写分离部署

文章目录

    • 1.环境说明
    • 2.安装proxysql和安装mysql和配置主从
      • 2.1给四台mysql安装mariadb
      • 2.2安装proxysql
        • 2.2.1配置mysql-master和mysql-master2
        • 2.2.2登录proxysql管理端
        • 2.2.3使用 insert 语句添加 mysql 主从到 mysql_servers 表中,其中:hostgroup_id 1 表示写组,2表示读组
        • 2.2.4在mysql的 master和master2 端添加属于proxysql的只读账号
        • 2.2.5在proxysql主机端修改变量设置健康检测的账号
    • 3.验证读写分离

1.环境说明

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

ProxySQL实现双主双从读写分离部署_第1张图片
把所有主机关闭防火墙和setLinux

[root@proxysql ~]# setenforce 0
[root@proxysql ~]# systemctl stop firewalld

2.安装proxysql和安装mysql和配置主从

安装流程图
ProxySQL实现双主双从读写分离部署_第2张图片

2.1给四台mysql安装mariadb

[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                                 :::*   

2.2安装proxysql

[root@proxysql ~]# cat <

2.2.1配置mysql-master和mysql-master2

[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)

2.2.2登录proxysql管理端

[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)

2.2.3使用 insert 语句添加 mysql 主从到 mysql_servers 表中,其中:hostgroup_id 1 表示写组,2表示读组

([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)

2.2.4在mysql的 master和master2 端添加属于proxysql的只读账号

[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)

2.2.5在proxysql主机端修改变量设置健康检测的账号

([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)

3.验证读写分离

登录用户是刚才我们在 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)


你可能感兴趣的:(运维知识,linux,mysql)