123. MySQL MHA+ProxySQL高可用架构部署

基于上篇博文更新—》点击传送 MHA高可用架构部署教程

ProxySQL是一个高性能的MySQL中间件,我们今天拿它当作MySQL的读写分离器,我们把用户的写操作向master-slave集群的master中写入,而集群中其他的slave一起做负载均衡的读操作。

1. 环境要求:

MHA 高可用环境 ,能够实现主库挂掉后会切换到从库 并且vip 也会漂移到从库

2. 安装配置ProxySQL

1.安装

1、下载地址 https://www.percona.com/downloads/proxysql/

2、依赖包安装:
yum install -y perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL

[root@db03 ~]# rpm -ivh proxysql-1.4.14-1.1.el7.x86_64.rpm 
warning: proxysql-1.4.14-1.1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 8507efa5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:proxysql-1.4.14-1.1.el7          ################################# [100%]

3、启动:
[root@db03 ~]# systemctl start proxysql

4、查看端口:
[root@db03 ~]# netstat -lntup |grep proxysql
tcp        0      0 0.0.0.0:6032            0.0.0.0:*               LISTEN      4771/proxysql       
tcp        0      0 0.0.0.0:6033            0.0.0.0:*               LISTEN      4771/proxysql       
6032是管理端口
6033是对外服务端口
默认管理用户及密码:admin,admin

2.配置

1. 连接proxysql 6032管理端口
登入proxysql 把MySQL主从信息添加进去,将主库master放入写节点中,也加就是hostgroup_id 为100,slave节点做读放到1000[root@db03 ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
db03 [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(100,'10.0.0.55',3306,1,1000,10,'vip');
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,'10.0.0.52',3306,1,1000,10,'slave');
Query OK, 1 row affected (0.00 sec)

注意:这里我直接将写节点的VIP  10.0.0.55
db03 [(none)]>select * from mysql_servers;
+--------------+-----------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 100          | 10.0.0.55 | 3306 | ONLINE | 1      | 0           | 1000            | 10                  | 0       | 0              | vip     |
| 1000         | 10.0.0.52 | 3306 | ONLINE | 1      | 0           | 1000            | 10                  | 0       | 0              | slave   |
+--------------+-----------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

3. 配置后端使用的MySQL用户,需要先在后端MySQL(52,53) 里真实存在,一个是监控账号,一个是程序账号
 [root@db03 ~]# mysql

db03 [(none)]>GRANT ALL PRIVILEGES ON *.* TO 'proxysql'@'10.0.0.53'  identified by 'proxysql';
Query OK, 0 rows affected, 1 warning (0.00 sec)

db03 [(none)]>GRANT ALL PRIVILEGES ON *.* TO 'sbuser'@'%' identified by 'sbuser';
Query OK, 0 rows affected, 1 warning (0.00 sec)

4. 在后端MySQL里添加完之后再配置proxysql: 这里需要注意,default_hostgroup需要和上面的对应
 [root@db03 ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
db03 [(none)]>insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values('sbuser','sbuser',1,100,1);
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| sbuser   | sbuser   | 1      | 0       | 100               | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
1 row in set (0.00 sec)

5. 设置健康的监测账号
[root@db03 ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1

db03 [(none)]>set mysql-monitor_username='proxysql';
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>set mysql-monitor_password='proxysql';
Query OK, 1 row affected (0.00 sec)

-- 应用到线上
db03 [(none)]>load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)

db03 [(none)]>load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

db03 [(none)]>load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)

-- 持久化
save mysql servers to disk;
save mysql users to disk;
save mysql variables to disk;

 明文密码在这里可以用save命令来转换成了hash值的密码:
 save mysql users to mem;
 select * from mysql_users;
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| sbuser   | *CA96E56547F43610DDE9EB7B12B4EF4C51CDDFFC | 1      | 0       | 100               |                | 0             | 1                      | 0            | 1       | 1        | 10000           |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+

6. 配置路由
-- 发送到M
db03 [(none)]>INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT.*FOR UPDATE$',100,1);
Query OK, 1 row affected (0.00 sec)

-- 发送到S
db03 [(none)]>INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT',1000,1);
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)

db03 [(none)]>save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)

3.连接数据库6033 测试读写分离

[root@db03 ~]# mysql -usbuser -psbuser -P6033
db03 [(none)]>create database z1_email;
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>use z1_email;
Database changed

db03 [z1_email]>create table a1(id int);
Query OK, 0 rows affected (0.10 sec)

db03 [z1_email]>insert into a1 values(134);
Query OK, 1 row affected (0.14 sec)

db03 [z1_email]>insert into a1 values(146);
Query OK, 1 row affected (0.00 sec)

db03 [z1_email]>insert into a1 values(157);
Query OK, 1 row affected (0.00 sec)

db03 [z1_email]>show tables;
+--------------------+
| Tables_in_z1_email |
+--------------------+
| a1                 |
+--------------------+
1 row in set (0.00 sec)

db03 [z1_email]>select * from a1;
+------+
| id   |
+------+
|  134 |
|  146 |
|  157 |
+------+
3 rows in set (0.00 sec)

进入管理账户6032端口查看,可以看到读写分离已经完成了
db03 [(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 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 100       | information_schema | sbuser   | 0x02033E45904D3DF0 | show databases                   | 4          | 1578121919 | 1578121984 | 39508    | 7820     | 11915    |
| 100       | information_schema | sbuser   | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 3          | 1578121911 | 1578121981 | 0        | 0        | 0        |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
2 rows in set (0.00 sec)

4. 测试

模拟主库宕机的情况

分析:主库挂掉后proxysql的写入情况
主库故障,使用MHA 手动failover 将 vip 切换到从库 10.0.0.52,此时 10.0.0.52 上的 vip是10.0.0.55

db03 [(none)]>select hostgroup_id,hostname,port,status,weight from runtime_mysql_servers;
+--------------+-----------+------+--------+--------+
| hostgroup_id | hostname  | port | status | weight |
+--------------+-----------+------+--------+--------+
| 100          | 10.0.0.55 | 3306 | ONLINE | 1      |
| 1000         | 10.0.0.52 | 3306 | ONLINE | 1      |
+--------------+-----------+------+--------+--------+
2 rows in set (0.00 sec)

从上面可以看出来 mysql_servers 中的  hostname 的写是10.0.0.55 读是10.0.0.52,这样一来是不是 主库挂了后手动切换后就可以直接写了呢? 测试一下

在主节点上模拟主库挂掉的情况
[root@db01 ~]# pkill mysqld

[root@db03 ~]# mysql -usbuser -psbuser -P6033
db03 [z1_email]>insert into a1 values(158);
Query OK, 1 row affected (0.00 sec)

VIP已经自动切换至db02
    inet 10.0.0.55/24 brd 10.0.0.255 scope global secondary eth0:1

 此时主库恢复后 change 到新的主库
db01 [(none)]>change master to 
    -> master_host='10.0.0.52',
    -> master_user='repl',
    -> master_password='123' ,
    -> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.35 sec)

db01 [(none)]>start slave;
Query OK, 0 rows affected (0.00 sec)

查看主从同步状态是OK的
db01 [(none)]>show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.52
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 730
               Relay_Log_File: db01-relay-bin.000004
                Relay_Log_Pos: 414
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 730
              Relay_Log_Space: 620
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 53
                  Master_UUID: a4426e6e-2eb8-11ea-bf7c-000c29180ccf
             Master_Info_File: /data/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 8c2615a4-2eb8-11ea-a282-000c292aecb1:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)


登录管理员用户
[root@db03 binlog]# mysql -uadmin -padmin -P6032 -h127.0.0.1
db03 [(none)]>select hostgroup_id,hostname,port,status,weight from runtime_mysql_servers;
+--------------+-----------+------+---------+--------+
| hostgroup_id | hostname  | port | status  | weight |
+--------------+-----------+------+---------+--------+
| 100          | 10.0.0.55 | 3306 | SHUNNED | 1      |
| 1000         | 10.0.0.52 | 3306 | ONLINE  | 1      |
+--------------+-----------+------+---------+--------+
2 rows in set (0.00 sec)

然后我们加入10.0.0.51  并且我这里分配的权重是9  
db03 [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,'10.0.0.51',3306,9,1000,10,'test proxysql');
Query OK, 1 row affected (0.00 sec)

db03 [(none)]>load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)

db03 [(none)]>save mysql servers to disk;
Query OK, 0 rows affected (0.00 sec)


查看runtime_mysql_servers 
db03 [(none)]> select hostgroup_id,hostname,port,status,weight from runtime_mysql_servers;
+--------------+-----------+------+--------+--------+
| hostgroup_id | hostname  | port | status | weight |
+--------------+-----------+------+--------+--------+
| 100          | 10.0.0.55 | 3306 | ONLINE | 1      |
| 1000         | 10.0.0.51 | 3306 | ONLINE | 9      |
| 1000         | 10.0.0.52 | 3306 | ONLINE | 1      |
+--------------+-----------+------+--------+--------+
3 rows in set (0.00 sec)

现在主库为10.0.0.52 如果此时主库挂了怎么办? 是否还会影响在proxysql中的读写操作呢?
我们再次模拟 主库挂掉的情况 此时主库是 10.0.0.52
[root@db02 ~]# pkill mysqld


[root@db03 ~]# mysql -usbuser -psbuser -P6033
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 47
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

db03 [(none)]>
db03 [(none)]>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| z1_email           |
+--------------------+
5 rows in set (0.00 sec)

db03 [(none)]>use z1_email;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
db03 [z1_email]>show tables;
+--------------------+
| Tables_in_z1_email |
+--------------------+
| a1                 |
+--------------------+
1 row in set (0.00 sec)

此时再去 proxysql的程序端口6033中做读操作  超时不可读
db03 [z1_email]>select * from a1;
Empty set (0.00 sec)

最后做一个总结: 

MHA + proxysql 可以做到高可用和读写分离,在主库挂掉后切换到从库,通过主库的vip漂移的特性将proxysql中的写节点配置成vip,

并且总是主库在做写操作的,因为vip在哪台机器哪台机器就是主库。

而且如果我们做了如下结构的proxysql策略,则无论是 哪台机器挂掉 ,只要进行切换就不会影响读和写
db03 [(none)]> select hostgroup_id,hostname,port,status,weight from runtime_mysql_servers;
+--------------+-----------+------+--------+--------+
| hostgroup_id | hostname  | port | status | weight |
+--------------+-----------+------+--------+--------+
| 100          | 10.0.0.55 | 3306 | ONLINE | 1      |
| 1000         | 10.0.0.51 | 3306 | ONLINE | 9      |
| 1000         | 10.0.0.52 | 3306 | ONLINE | 1      |
+--------------+-----------+------+--------+--------+
3 rows in set (0.00 sec)

在这里插入图片描述

发布了148 篇原创文章 · 获赞 65 · 访问量 7618

猜你喜欢

转载自blog.csdn.net/chengyinwu/article/details/103832864
今日推荐