• ProxySQL实现mysql8主从同步读写分离


    ProxySQL基本介绍

    ProxySQL是 MySQL 的高性能、高可用性、协议感知代理。以下为结合主从复制对ProxySQL读写分离、黑白名单、路由规则等做些基本测试。

    先简单介绍下ProxySQL及其功能和配置,主要包括:

    最基本的读/写分离,且方式有多种;

    可定制基于用户、基于schema、基于语句的规则对SQL语句进行路由,规则很灵活;

    动态加载配置,即绝大部分的配置可以在线修改,但有少部分参数还是需要重启来生效;

    可缓存查询结果。虽然缓存策略比较简陋,但实现了基本的缓存功能;

    过滤危险的SQL,增加防火墙等功能;

    提供连接池、日志记录、审计日志等功能;

    请求流程

    核心功能

     

    读写分离:可查询走从库,写入走主库

    简单Sharding:ProxySQL的sharding是通过正则匹配来实现的,对于需要拆分SQL以及合并SQL执行结果的不能支持,所以写了简单sharding

    连接池管理:常规功能,为了提高SQL执行效率。

    多路复用:主要优化点在后端mysql连接的复用,对比smart client,中间层不仅对前端建连也会对后端建连,可自行控制后端连接的复用逻辑。

    流量管控:kill连接和kill query;whitelist配置。

    高可用:底层mysql,如果从库挂了,自动摘除流量;主库挂了暂不处理。proxysql自身高可用,提供cluster的功能,cluster内部会自行同步元数据以及配置变更信息。

    查询缓存:对username+schema+query的key进行缓存,设置ttl过期,不适合写完就查的场景,因为在数据在未过期之前可能是脏数据。

    动态配置:大部分的配置可动态变更,先load到runtime,在save到disk,通过cluster的功能同步到其他的节点。

    流量镜像:同一份流量可以多出写入,但是并不保证mirror的流量一定成功。

     

    ProxySQL结构

     

    • Qurey Processor 用于匹配查询规则并根据规则决定是否缓存查询或者将查询加入黑名单或者重新路由、重写查询或者镜像查询到其他hostgroup。

    • User Auth 为底层后端数据库认证提供了用户凭证。

    • Hostgroup manager – 负责管理发送SQL请求都后端数据库并跟踪SQL请求状态。

    • Connection pool – 负责管理后端数据库连接,连接池中建立的连接被所有的前端应用程序共享。

    • Monitoring – 负责监控后端数据库健康状态主从复制延时并临时下线不正常的数据库实例。

     数据库结构

     

    1. # mysql -uadmin -padmin -h127.0.0.1 -P6032
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 2
    5. Server version: 5.5.30 (ProxySQL Admin Module)
    6. mysql> show databases;
    7. +-----+---------------+-------------------------------------+
    8. | seq | name | file |
    9. +-----+---------------+-------------------------------------+
    10. | 0 | main | |
    11. | 2 | disk | /var/lib/proxysql/proxysql.db |
    12. | 3 | stats | |
    13. | 4 | monitor | |
    14. | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
    15. +-----+---------------+-------------------------------------+
    16. 5 rows in set (0.00 sec)
    • main:内存配置数据库,表里存放后端db实例、用户验证、路由规则等信息。表名以 runtime开头的表示proxysql当前运行的配置内容,不能通过dml语句修改,只能修改对应的不以 runtime 开头的(在内存)里的表,然后 LOAD 使其生效, SAVE 使其存到硬盘以供下次重启加载。

    • disk:是持久化到硬盘的配置,sqlite数据文件。SQLite3 数据库,默认位置为 $(DATADIR)/proxysql.db,在重新启动时,未保留的内存中配置将丢失。因此,将配置保留在 DISK 中非常重要。(SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎)

    • stats:proxysql运行抓取的统计信息,包括到后端各命令的执行次数、流量、processlist、查询种类汇总/执行时间等等。

    • monitor:库存储 monitor 模块收集的信息,主要是对后端db的健康/延迟检查。

    • stats_history:统计信息历史库

    实验环境

    机器名称IP配置服务角色备注
    proxy192.168.142.146proxysql控制器用于监控管理
    master192.168.142.147数据库主服务器
    slave1192.168.142.139数据库从服务器

    安装ProxySQL

    1. # 配置yum源
    2. cat >/etc/yum.repos.d/proxysql.repo << EOF
    3. [proxysql]
    4. name=ProxySQL YUM repository
    5. baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.5.x/centos/8
    6. gpgcheck=1
    7. gpgkey=https://repo.proxysql.com/ProxySQL/proxysql-2.5.x/repo_pub_key
    8. EOF
    9. c
    10. # 安装proxysql
    11. yum install -y proxysql

     启动 ProxySQL

    1. [root@master ~]# systemctl enable --now proxysql
    2. # 管理员登录
    3. [root@master ~]# mysql -uadmin -padmin -h 127.0.0.1 -P 6032
    4. Welcome to the MariaDB monitor. Commands end with ; or \g.
    5. Your MySQL connection id is 1
    6. Server version: 5.5.30 (ProxySQL Admin Module)
    7. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    9. MySQL [(none)]> show databases;
    10. +-----+---------------+-------------------------------------+
    11. | seq | name | file |
    12. +-----+---------------+-------------------------------------+
    13. | 0 | main | |
    14. | 2 | disk | /var/lib/proxysql/proxysql.db |
    15. | 3 | stats | |
    16. | 4 | monitor | |
    17. | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
    18. +-----+---------------+-------------------------------------+
    19. 5 rows in set (0.00 sec)
    20. 可见有五个库: main、disk、stats 、monitor 和 stats_history
    21. main: 内存配置数据库,即 MEMORY,表里存放后端 db 实例、用户验证、路由规则等信息。main 库中有如下信息:
    22. MySQL [(none)]> show tables from main;
    23. +----------------------------------------------------+
    24. | tables |
    25. +----------------------------------------------------+
    26. | coredump_filters |
    27. | global_variables |
    28. | mysql_aws_aurora_hostgroups |
    29. | mysql_collations |
    30. | mysql_firewall_whitelist_rules |
    31. | mysql_firewall_whitelist_sqli_fingerprints |
    32. | mysql_firewall_whitelist_users |
    33. | mysql_galera_hostgroups |
    34. | mysql_group_replication_hostgroups |
    35. | mysql_hostgroup_attributes |
    36. | mysql_query_rules |
    37. | mysql_query_rules_fast_routing |
    38. | mysql_replication_hostgroups |
    39. | mysql_servers |
    40. | mysql_users |
    41. | proxysql_servers |
    42. | restapi_routes |
    43. | runtime_checksums_values |
    44. | runtime_coredump_filters |
    45. | runtime_global_variables |
    46. | runtime_mysql_aws_aurora_hostgroups |
    47. | runtime_mysql_firewall_whitelist_rules |
    48. | runtime_mysql_firewall_whitelist_sqli_fingerprints |
    49. | runtime_mysql_firewall_whitelist_users |
    50. | runtime_mysql_galera_hostgroups |
    51. | runtime_mysql_group_replication_hostgroups |
    52. | runtime_mysql_hostgroup_attributes |
    53. | runtime_mysql_query_rules |
    54. | runtime_mysql_query_rules_fast_routing |
    55. | runtime_mysql_replication_hostgroups |
    56. | runtime_mysql_servers |
    57. | runtime_mysql_users |
    58. | runtime_proxysql_servers |
    59. | runtime_restapi_routes |
    60. | runtime_scheduler |
    61. | scheduler |
    62. +----------------------------------------------------+
    63. 36 rows in set (0.00 sec)
    64. 库下的主要表:
    65. mysql_servers: 后端可以连接 MySQL 服务器的列表
    66. mysql_users: 配置后端数据库的账号和监控的账号。
    67. mysql_query_rules: 指定 Query 路由到后端不同服务器的规则列表。
    68. 注: 表名以 runtime_开头的表示 ProxySQL 当前运行的配置内容,不能通过 DML 语句修改。
    69. 只能修改对应的不以 runtime 开头的表,然后 “LOAD” 使其生效,“SAVE” 使其存到硬盘以供下次重启加载。
    70. disk :持久化的磁盘的配置
    71. stats: 统计信息的汇总
    72. monitor:一些监控的收集信息,比如数据库的健康状态等
    73. stats_history: 这个库是 ProxySQL 收集的有关其内部功能的历史指标

    配置 ProxySQL 所需账户

    在 Master (192.168.142.146) 的MySQL 上创建 ProxySQL 的监控账户和对外访问账户

    1. create user 'monitor'@'192.168.%.%' identified with mysql_native_password by 'Monitor@123.com';
    2. grant all privileges on *.* to 'monitor'@'192.168.%.%' with grant option;
    3. #proxysql 的对外访问账户
    4. create user 'proxysql'@'192.168.%.%' identified with mysql_native_password by '123456';
    5. grant all privileges on *.* to 'proxysql'@'192.168.%.%' with grant option;

    配置ProxySQL

    配置ProxySQL主从分组信息

    1. MySQL [(none)]>
    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. check_type VARCHAR CHECK (LOWER(check_type) IN ('read_only','innodb_read_only','super_read_only','read_only|innodb_read_only','read_only&innodb_read_only')) NOT NULL DEFAULT 'read_only',
    8. comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))
    9. 1 row in set (0.00 sec)

     创建组:(定义写为1,读为0)

    1. MySQL [(none)]> insert into mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment) values (1,0,'proxy');
    2. Query OK, 1 row affected (0.00 sec)
    3. MySQL [(none)]> load mysql servers to runtime;
    4. Query OK, 0 rows affected (0.01 sec)
    5. MySQL [(none)]> save mysql servers to disk;
    6. Query OK, 0 rows affected (0.02 sec)
    7. 注意:ProxySQL会根据server的read_only的取值将服务器进行分组。read_only=0的server,master被分到编号为1的写组,read_only=1的server,slave则分到编号为0的读组
    8. MySQL [(none)]> select * from mysql_replication_hostgroups;
    9. +------------------+------------------+------------+---------+
    10. | writer_hostgroup | reader_hostgroup | check_type | comment |
    11. +------------------+------------------+------------+---------+
    12. | 1 | 0 | read_only | proxy |
    13. +------------------+------------------+------------+---------+
    14. 1 row in set (0.00 sec)

     添加主从服务器节点:

    1. MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values (1,'192.168.142.147',3306);
    2. Query OK, 1 row affected (0.00 sec)
    3. 1
    4. MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values (0,'192.168.142.139',3306);
    5. Query OK, 1 row affected (0.00 sec)
    6. MySQL [(none)]> load mysql servers to runtime;
    7. Query OK, 0 rows affected (0.01 sec)
    8. MySQL [(none)]> save mysql servers to disk;
    9. Query OK, 0 rows affected (0.00 sec)
    10. MySQL [(none)]> select * from mysql_servers;
    11. +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
    12. | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
    13. +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
    14. | 1 | 192.168.142.147 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
    15. | 0 | 192.168.142.139 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
    16. | 0 | 192.168.150.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
    17. +--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
    18. 3 rows in set (0.00 sec)

    为ProxySQL监控MySQL后端节点

    1. MySQL [(none)]> use monitor
    2. Reading table information for completion of table and column names
    3. You can turn off this feature to get a quicker startup with -A
    4. Database changed
    5. MySQL [monitor]> set mysql-monitor_username='monitor';
    6. Query OK, 1 row affected (0.00 sec)
    7. MySQL [monitor]> set mysql-monitor_password='Monitor@123.com';
    8. Query OK, 1 row affected (0.00 sec)
    9. MySQL [monitor]> load mysql variables to runtime;
    10. MySQL [monitor]> save mysql variables to disk;
    11. MySQL [(none)]> select @@mysql-monitor_username;
    12. +--------------------------+
    13. | @@mysql-monitor_username |
    14. +--------------------------+
    15. | monitor |
    16. +--------------------------+
    17. 1 row in set (0.00 sec)
    18. MySQL [(none)]> select @@mysql-monitor_password;
    19. +--------------------------+
    20. | @@mysql-monitor_password |
    21. +--------------------------+
    22. | Monitor@123.com |
    23. +--------------------------+
    24. 1 row in set (0.00 sec)
    25. Admin>select * from monitor.mysql_server_connect_log;
    26. +-----------------+------+------------------+-------------------------+--------------------------------------------------------------------------+
    27. | hostname | port | time_start_us | connect_success_time_us | connect_error |
    28. +-----------------+------+------------------+-------------------------+--------------------------------------------------------------------------+
    29. | 192.168.142.139 | 3306 | 1709457246014919 | 0 | NULL |
    30. | 192.168.142.147 | 3306 | 1709457246735181 | 1543 | NULL |
    31. | 192.168.142.139 | 3306 | 1709457306015351 | 0 | NULL |
    32. | 192.168.142.147 | 3306 | 1709457306922075 | 1232 | NULL |
    33. | 192.168.142.147 | 3306 | 1709457366016055 | 1555 | NULL |

     对心跳信息的监控:

    1. Admin>select * from mysql_server_ping_log limit 10;
    2. +-----------------+------+------------------+----------------------+--------------------------------------------------------------------------+
    3. | hostname | port | time_start_us | ping_success_time_us | ping_error |
    4. +-----------------+------+------------------+----------------------+--------------------------------------------------------------------------+
    5. | 192.168.142.147 | 3306 | 1709457295768656 | 238 | NULL |
    6. | 192.168.142.139 | 3306 | 1709457295768744 | 0 | NULL |

    查看read_only日志监控:

    1. Admin>select * from mysql_server_read_only_log limit 5;
    2. +-----------------+------+------------------+-----------------+-----------+--------------------------------------------------------------------------------------------------------------+
    3. | hostname | port | time_start_us | success_time_us | read_only | error |
    4. +-----------------+------+------------------+-----------------+-----------+--------------------------------------------------------------------------------------------------------------+
    5. | 192.168.142.147 | 3306 | 1709457334932365 | 449 | 0 | NULL |
    6. | 192.168.142.139 | 3306 | 1709457334932475 | 0 | NULL | NULL |
    7. | 192.168.142.147 | 3306 | 1709457336432467 | 1110 | 0 | NULL

     ProxySQL配置对外访问账号:

    1. MySQL [(none)]> show create table mysql_users\G
    2. *************************** 1. row ***************************
    3. table: mysql_users
    4. Create Table: CREATE TABLE mysql_users (
    5. username VARCHAR NOT NULL,
    6. password VARCHAR,
    7. active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1,
    8. use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0,
    9. default_hostgroup INT NOT NULL DEFAULT 0,
    10. default_schema VARCHAR,
    11. schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0,
    12. transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1,
    13. fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0,
    14. backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1,
    15. frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1,
    16. max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000,
    17. attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '',
    18. comment VARCHAR NOT NULL DEFAULT '',
    19. PRIMARY KEY (username, backend),
    20. UNIQUE (username, frontend))
    21. 1 row in set (0.00 sec)

     将对外访问账号添加到mysql_users表中:

    1. insert into mysql_users (username,password,default_hostgroup,transaction_persistent) values ('proxysql','123456',1,1);
    2. load mysql users to runtime;
    3. save mysql users to disk;
    4. MySQL [(none)]> select * from mysql_users\G

    在从库端192.168.142.139上通过对方访问账号proxy连接,测试是否路由能默认到hostgroup_id=1,它是一个写组

    1. [root@salve2 ~]# mysql -h192.168.142.146 -uproxysql -p'123456' -P 6033
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 4
    5. Server version: 5.5.30 (ProxySQL)
    6. Copyright (c) 2000, 2024, Oracle and/or its affiliates.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql> show databases;
    12. +--------------------+
    13. | Database |
    14. +--------------------+
    15. | information_schema |
    16. | itcast |
    17. | master1db |
    18. | mysql |
    19. | performance_schema |
    20. | sys |
    21. +--------------------+
    22. 6 rows in set (0.01 sec)
    23. mysql> select @@server_id;
    24. +-------------+
    25. | @@server_id |
    26. +-------------+
    27. | 1 |
    28. +-------------+
    29. 1 row in set (0.00 sec)
    30. mysql> create database keme;
    31. Query OK, 1 row affected (0.01 sec)

     添加读写分离规则(mysql_query_rules)

    1. Admin>insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values(1,1,'^select .* for update$',1,1);
    2. Query OK, 1 row affected (0.00 sec)
    3. Admin>insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values(2,1,'^select',0,1);
    4. Query OK, 1 row affected (0.00 sec)
    5. Admin>load mysql query rules to runtime;
    6. Query OK, 0 rows affected (0.00 sec)
    7. Admin>save mysql query rules to disk;
    8. Query OK, 0 rows affected (0.01 sec)

     测试读写分离

    1. [root@salve2 ~]# mysql -uproxysql -p123456 -h 192.168.142.147 -P 3306 -e "select @@server_id"
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. +-------------+
    4. | @@server_id |
    5. +-------------+
    6. | 1 |
    7. +-------------+

  • 相关阅读:
    【C++】学习笔记——类和对象_4
    毫米波雷达基础知识系列——FFT
    C/C++常用语法复习(输入、输出、判断、循环)
    网络通信基础(网络通信基本概念+TCP/IP 模型)
    个性化定义多个 Git 托管平台配置
    5、Nacos服务注册与发现
    爬虫获取静态网页数据
    【App自动化测试】(九)移动端复杂测试环境模拟——来电、短信、网络切换
    Tomcat的配置与优化
    深度学习(2)---循环神经网络(RNN)
  • 原文地址:https://blog.csdn.net/m0_66011019/article/details/136434535