当前位置:   article > 正文

MySQL-ProxySQL读写分离连接池负载均衡分库分表故障切换查询重写流量镜像SQL审计自动重连自动下线在线配置路由引擎Query Cache缓存主从复制架构高可用MySQL中间件ProxySQL...

proxysql健康检测拒绝连接

市场分析

如题,我们首先分析市场上已有的同类产品:

  • MySQL Route:是现在MySQL官方Oracle公司发布出来的一个中间件。
  • Atlas:是由奇虎360公发的基于MySQL协议的数据库中间件产品,它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了若干Bug,并增加了很多功能特性。目前该产品在360内部得到了广泛应用。
  • DBProxy:是由美团点评公司技术工程部DBA团队(北京)开发维护的一个基于MySQL协议的数据中间层。它在奇虎360公司开源的Atlas基础上,修改了部分bug,并且添加了很多特性。
  • Cobar:是阿里巴巴B2B开发的关系型分布式系统,管理将近3000个MySQL实例。 在阿里经受住了考验,后面由于作者的走开的原因cobar没有人维护 了,阿里也开发了tddl替代cobar。
  • MyCAT:是社区爱好者在阿里cobar基础上进行二次开发,解决了cobar当时存 在的一些问题,并且加入了许多新的功能在其中。目前MyCAT社区活跃度很高,目前已经有一些公司在使用MyCAT。总体来说支持度比较高,也会一直维护下去。

ProxySQL

今天给大家介绍的一款是proxySQL,ProxySQL是使用C++语言开发的,强悍性能截图:

其官网有个ProxySQL1.3.2与MariaDB MaxScale 2.0.3、HAProxy1.7、NGINX1.11.8、MySQL Router 2.0等ProxySQL产品对比信息,可以看出其优秀值得使用。至少有很多大公司巨兽已经在使用ProxySQL,比如亚马逊数据库集群自动切换

ProxySQL官网文档也是很齐全,MySQL5.7结合ProxySQL 案例讲述的也很清晰,现在先简单介绍下其特色功能点:

  • 查询缓存
  • 查询路由
  • 故障转移
  • 在线配置立刻生效无需重启
  • 应用层代理
  • 跨平台
  • 高级拓展支持
  • 防火墙

通过上述,我们可以看到ProxySQL可以做许多事情,已经不仅仅是纯粹的MySQL读写分离,其实我们通过后面所述结合业务发散,ProxySQL还可以支持以下高级功能:

  • 读写分离
  • 数据库集群、分片
  • 分库分表
  • 主从切换
  • SQL审计
  • 连接池 多路复用
  • 负载均衡
  • 查询重写
  • 流量镜像
  • 自动重连
  • 自动下线
  • 高可用
  • .........

高可用架构

ProxySQL实战

准备

要实现MySQL的读写分离,首先要准备好MySQL主从复制架构(请SA协助或者自己百度),比如:
主()服务器:10.0.16.1
一般都是两台从()服务器:10.0.16.210.0.17.2
除主从复制架构外,还需要准备一台布置中间件的主机,这里是10.0.16.88

一共4台机器,下面是我的主从配置,大家可以参考下:

10.0.16.1主服务器中/etc/my.cnf中[mysqld]配置段配置:

  1. [mysqld]
  2. datadir=/var/lib/mysql
  3. socket=/var/lib/mysql/mysql.sock
  4. symbolic-links=0
  5. log_bin=/var/lib/mysql/binlog
  6. server_id=1
  7. innodb_file_per_table=ON
  8. skip_name_resolve=ON
  9. sync_binlog=1
  10. innodb_flush_log_at_trx_commit=1
  11. relay_log=/var/lib/mysql/slavelog

10.0.16.2从服务器中/etc/my.cnf中[mysqld]配置段配置:

  1. [mysqld]
  2. datadir=/var/lib/mysql
  3. socket=/var/lib/mysql/mysql.sock
  4. symbolic-links=0
  5. innodb_file_per_table=ON
  6. skip_name_resolve=ON
  7. server_id=21
  8. relay_log=/var/lib/mysql/slavelog
  9. set @@global.read_only=ON
  10. sync_binlog=1
  11. innodb_flush_log_at_trx_commit=1

10.0.17.2从服务器中/etc/my.cnf中[mysqld]配置段配置:

  1. [mysqld]
  2. datadir=/var/lib/mysql
  3. socket=/var/lib/mysql/mysql.sock
  4. symbolic-links=0
  5. innodb_file_per_table=ON
  6. skip_name_resolve=ON
  7. server_id=22
  8. relay_log=/var/lib/mysql/slavelog
  9. set @@global.read_only=ON
  10. sync_binlog=1
  11. innodb_flush_log_at_trx_commit=1

安装

  1. wget https://github.com/sysown/proxysql/releases/download/v1.4.12/proxysql_1.4.12-ubuntu16_amd64.deb
  2. sudo dpkg -i proxysql_1.4.12-ubuntu16_amd64.deb

操作

  1. 版本:sudo proxysql --version
  2. 启动:sudo service proxysql start
  3. 暂停:sudo service proxysql stop
  4. 重启:sudo service proxysql restart
  5. 状态:sudo service proxysql status

概念

  1. 客户端:6033端口
  2. 管理端:6032端口

配置文件

  1. //不推荐
  2. /etc/proxysql.cnf

控制台

上述之所以不推荐,是因为我们可以通过ProxySQL控制台在线修改配置,无需重启,立即生效。

mysql -uadmin -padmin -h127.0.0.1 -P6032 --prompt='Admin> ' --default-auth=mysql_native_password
设置SQL日志记录【ProxySQL】
SET mysql-eventslog_filename='queries.log';
添加主从【ProxySQL】
  1. insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'10.0.16.1',3306,1,'主库');
  2. insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.0.16.2',3306,9,'从库1');
  3. insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.0.17.2',3306,1,'从库2');
查看主从【ProxySQL】
  1. Admin> select * from mysql_servers;
  2. +--------------+-------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-----------------------+
  3. | hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
  4. +--------------+-------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-----------------------+
  5. | 1 | 10.0.16.1 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 主库 |
  6. | 2 | 10.0.16.2 | 3306 | ONLINE | 9 | 0 | 1000 | 0 | 0 | 0 | 从库1 |
  7. | 2 | 10.0.17.2 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 从库2 |
  8. +--------------+-------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-----------------------+
  9. 4 rows in set (0.00 sec)
创建主从账号【MySQL】
  1. CREATE USER 'proxysql'@'%' IDENTIFIED BY '123456';
  2. GRANT ALL PRIVILEGES ON *.* TO 'proxysql'@'%' WITH GRANT OPTION;
添加主从账号【ProxySQL】
insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',1,1);
查看主从账号【ProxySQL】
  1. Admin> select * from mysql_users;
  2. +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
  3. | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
  4. +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
  5. | proxysql | 123456 | 1 | 0 | 1 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 |
  6. +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
  7. 1 row in set (0.00 sec)
创建监控账号【MySQL】
  1. CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
  2. GRANT SELECT ON *.* TO 'monitor'@'%' WITH GRANT OPTION;
添加监控账号【ProxySQL】
  1. set mysql-monitor_username='monitor';
  2. set mysql-monitor_password='monitor';
查看监控账号【ProxySQL】
  1. SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-monitor_%';
  2. //也可以这样快速定位
  3. Admin> select @@mysql-monitor_username;
  4. +--------------------------+
  5. | @@mysql-monitor_username |
  6. +--------------------------+
  7. | monitor |
  8. +--------------------------+
  9. 1 row in set (0.01 sec)
  10. Admin> select @@mysql-monitor_password;
  11. +--------------------------+
  12. | @@mysql-monitor_password |
  13. +--------------------------+
  14. | monitor |
  15. +--------------------------+
  16. 1 row in set (0.00 sec)
检测监控【ProxySQL】

检测上述配置是否正确:connect_error为NULL则正确。

  1. Admin> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
  2. +-------------+------+------------------+-------------------------+---------------+
  3. | hostname | port | time_start_us | connect_success_time_us | connect_error |
  4. +-------------+------+------------------+-------------------------+---------------+
  5. | 10.0.16.1 | 3306 | 1543224623330044 | 1067 | NULL |
  6. | 10.0.16.2 | 3306 | 1543224622707711 | 1094 | NULL |
  7. | 10.0.17.2 | 3306 | 1543224563518239 | 1180 | NULL |
  8. +-------------+------+------------------+-------------------------+---------------+
  9. 3 rows in set (0.01 sec)
  10. Admin> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10;
  11. +-------------+------+------------------+----------------------+------------+
  12. | hostname | port | time_start_us | ping_success_time_us | ping_error |
  13. +-------------+------+------------------+----------------------+------------+
  14. | 10.0.16.1 | 3306 | 1543224683246221 | 169 | NULL |
  15. | 10.0.16.2 | 3306 | 1543224683106758 | 194 | NULL |
  16. | 10.0.17.2 | 3306 | 1543224673230502 | 268 | NULL |
  17. +-------------+------+------------------+----------------------+------------+
  18. 3 rows in set (0.00 sec)
读写映射【ProxySQL】

这里配置主从自动切换:互为主从,自动切换,保证高可用。

如果你没有做到互为主从,请跳过此项。

  1. insert into mysql_replication_hostgroups values(1,2,'高可用');
  2. Admin> select * from mysql_replication_hostgroups;
  3. +------------------+------------------+-----------+
  4. | writer_hostgroup | reader_hostgroup | comment |
  5. +------------------+------------------+-----------+
  6. | 1 | 2 | 高可用 |
  7. +------------------+------------------+-----------+
  8. 1 row in set (0.00 sec)
读写路由【ProxySQL】

读写分离规则,正则写法,也支持全匹配SQL,同时支持按照MySQL账号、库名、表名、客户IP等特征自动识别DB,从而达到分库分表、读写分离。ProxySQL的核心!

  1. insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,log,apply)values(1,1,'^UPDATE',1,1,1);
  2. insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,log,apply)values(2,1,'^SELECT',2,1,1);
SQL重写:分库

原来查询MySQL专业学生的SQL语句:

select * from it_db.stu where zhuanye='MySQL' and xxx;

分库后,该SQL语句需要重写为:

select * from MySQL.stu where 1=1 and xxx;

我们只需要插入一条SQL路由重写规则:

  1. insert into mysql_query_rules(rule_id,active,apply,destination_hostgroup,match_pattern,replace_pattern)
  2. values (1,1,1,20,"^(select.*?from) it_db\.(.*?) where zhuanye=['""](.*?)['""] (.*)$","\1 \3.\2 where 1=1 \4");

大家可以看到,直接通过强大的正则配置SQL路由重写,后端程序小白无需任何感知即可实现数据库分库分片,另外集群等原理类似。

查看路由【ProxySQL】

检测上述配置的路由规则。

  1. Admin> select * from mysql_query_rules \G;
  2. *************************** 1. row ***************************
  3. rule_id: 1
  4. active: 1
  5. username: NULL
  6. schemaname: NULL
  7. flagIN: 0
  8. client_addr: NULL
  9. proxy_addr: NULL
  10. proxy_port: NULL
  11. digest: NULL
  12. match_digest: NULL
  13. match_pattern: ^UPDATE
  14. negate_match_pattern: 0
  15. re_modifiers: CASELESS
  16. flagOUT: NULL
  17. replace_pattern: NULL
  18. destination_hostgroup: 1
  19. cache_ttl: NULL
  20. reconnect: NULL
  21. timeout: NULL
  22. retries: NULL
  23. delay: NULL
  24. next_query_flagIN: NULL
  25. mirror_flagOUT: NULL
  26. mirror_hostgroup: NULL
  27. error_msg: NULL
  28. OK_msg: NULL
  29. sticky_conn: NULL
  30. multiplex: NULL
  31. log: 1
  32. apply: 1
  33. comment: NULL
  34. *************************** 2. row ***************************
  35. rule_id: 2
  36. active: 1
  37. username: NULL
  38. schemaname: NULL
  39. flagIN: 0
  40. client_addr: NULL
  41. proxy_addr: NULL
  42. proxy_port: NULL
  43. digest: NULL
  44. match_digest: NULL
  45. match_pattern: ^SELECT
  46. negate_match_pattern: 0
  47. re_modifiers: CASELESS
  48. flagOUT: NULL
  49. replace_pattern: NULL
  50. destination_hostgroup: 2
  51. cache_ttl: NULL
  52. reconnect: NULL
  53. timeout: NULL
  54. retries: NULL
  55. delay: NULL
  56. next_query_flagIN: NULL
  57. mirror_flagOUT: NULL
  58. mirror_hostgroup: NULL
  59. error_msg: NULL
  60. OK_msg: NULL
  61. sticky_conn: NULL
  62. multiplex: NULL
  63. log: 1
  64. apply: 1
  65. comment: NULL
  66. 2 rows in set (0.01 sec)
配置生效【ProxySQL】
  1. //加载到内存
  2. load mysql users to runtime;
  3. load mysql servers to runtime;
  4. load mysql query rules to runtime;
  5. load mysql variables to runtime;
  6. load admin variables to runtime;
  7. //永久生效
  8. save mysql users to disk;
  9. save mysql servers to disk;
  10. save mysql query rules to disk;
  11. save mysql variables to disk;
  12. save admin variables to disk;
PHP、JAVA、CLI端使用

*********程序配置参数记得更改:弃用直连MYSQL改为ProxySQL代理连接*********

ProxySQL 6033端口正好是MySQL 3306端口的反转!

后端程序PDO直接连接此DSN,一切分发路由 由ProxySQL来代理,对于后端小白来说无需关心如此复杂的DBA幕后故事也可以实现MYSQL读写分离、分库分表高可用!

mysql -uproxysql -p123456 -h127.0.0.1 -P6033 --default-auth=mysql_native_password
验证【ProxySQL】
  1. Admin> select * from stats_mysql_query_rules;
  2. +---------+------+
  3. | rule_id | hits |
  4. +---------+------+
  5. | 1 | 2 |
  6. | 2 | 2160 |
  7. +---------+------+
  8. 2 rows in set (0.00 sec)
  9. Admin> select * from stats_mysql_query_digest;
  10. +-----------+------------+----------+--------------------+-------------------------------------------------------------------------------------+------------+------------+------------+----------+----------+----------+
  11. | hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
  12. +-----------+------------+----------+--------------------+-------------------------------------------------------------------------------------+------------+------------+------------+----------+----------+----------+
  13. | 1 | master | proxysql | 0x48407E0543261BAF | UPDATE `sys_user_base` SET last_login_time=?,pw_grade=? WHERE `user_id` = ? | 1 | 1543218281 | 1543218281 | 269595 | 269595 | 269595 |
  14. | 2 | master | proxysql | 0x7B56979CCBF5FE63 | SELECT `value` FROM `ent_apiceshiqiye_config` WHERE `type` = ? LIMIT ? | 4 | 1543220121 | 1543220171 | 1021 | 157 | 446 |
  15. +-----------+------------+----------+--------------------+-------------------------------------------------------------------------------------+------------+------------+------------+----------+----------+----------+
  16. 2 rows in set (0.00 sec)
  17. select * from stats_mysql_query_digest_reset;//清空日志
  18. Admin> select * from stats_mysql_commands_counters where Total_cnt>0;
  19. +--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
  20. | Command | Total_Time_us | Total_cnt | cnt_100us | cnt_500us | cnt_1ms | cnt_5ms | cnt_10ms | cnt_50ms | cnt_100ms | cnt_500ms | cnt_1s | cnt_5s | cnt_10s | cnt_INFs |
  21. +--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
  22. | CREATE_TABLE | 177478 | 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
  23. | SELECT | 9537803 | 2175 | 15 | 1820 | 187 | 73 | 7 | 33 | 13 | 25 | 1 | 1 | 0 | 0 |
  24. | UPDATE | 445145 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 |
  25. | SHOW | 542898 | 124 | 0 | 0 | 22 | 97 | 2 | 0 | 1 | 2 | 0 | 0 | 0 | 0 |
  26. +--------------+---------------+-----------+-----------+-----------+---------+---------+----------+----------+-----------+-----------+--------+--------+---------+----------+
  27. 4 rows in set (0.00 sec)
注意【ProxySQL】
  1. //选表
  2. show tables from stats;
显示所有数据库【ProxySQL】
  1. Admin> show databases;
  2. +-----+---------------+-------------------------------------+
  3. | seq | name | file |
  4. +-----+---------------+-------------------------------------+
  5. | 0 | main | |
  6. | 2 | disk | /var/lib/proxysql/proxysql.db |
  7. | 3 | stats | |
  8. | 4 | monitor | |
  9. | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
  10. +-----+---------------+-------------------------------------+
  11. 5 rows in set (0.00 sec)
显示所有数据表:main【ProxySQL】

此库对比下面的disk库,除了多了runtime_开头的表意外,其余表数量、结构、内容是完全一样的,可以粗暴认为是其拷贝,其实这是由ProxySQL三层架构达到修改配置在线生效无需重启决定的:

  • disk库是永久保存到磁盘中的信息;
  • main库是内存中运行的信息,其中runtime_系列表代表当前系统中正在使用的配置信息。

下述所有表的CRUD和使用MYSQL感觉一样,无需重复介绍,见名知意。

  1. Admin> show tables from main;
  2. +--------------------------------------------+
  3. | tables |
  4. +--------------------------------------------+
  5. | global_variables |
  6. | mysql_collations |
  7. | mysql_group_replication_hostgroups |
  8. | mysql_query_rules |
  9. | mysql_query_rules_fast_routing |
  10. | mysql_replication_hostgroups |
  11. | mysql_servers |
  12. | mysql_users |
  13. | proxysql_servers |
  14. | runtime_checksums_values |
  15. | runtime_global_variables |
  16. | runtime_mysql_group_replication_hostgroups |
  17. | runtime_mysql_query_rules |
  18. | runtime_mysql_query_rules_fast_routing |
  19. | runtime_mysql_replication_hostgroups |
  20. | runtime_mysql_servers |
  21. | runtime_mysql_users |
  22. | runtime_proxysql_servers |
  23. | runtime_scheduler |
  24. | scheduler |
  25. +--------------------------------------------+
  26. 20 rows in set (0.00 sec)
显示所有数据表:disk【ProxySQL】

用于将配置持久化到磁盘上。配置持久化后,下次重启ProxySQL时就会读取这些已被持久化的配置。

  1. Admin> show tables from disk;
  2. +------------------------------------+
  3. | tables |
  4. +------------------------------------+
  5. | global_variables |
  6. | mysql_collations |
  7. | mysql_group_replication_hostgroups |
  8. | mysql_query_rules |
  9. | mysql_query_rules_fast_routing |
  10. | mysql_replication_hostgroups |
  11. | mysql_servers |
  12. | mysql_users |
  13. | proxysql_servers |
  14. | scheduler |
  15. +------------------------------------+
  16. 10 rows in set (0.01 sec)
显示所有数据表:stats【ProxySQL】

这个数据库包含了ProxySQL收集的关于其内部功能的指标。通过这个数据库,你可以知道触发某个计数器的频率,途经ProxySQL的查询执行次数等等。

  1. Admin> show tables from stats;
  2. +--------------------------------------+
  3. | tables |
  4. +--------------------------------------+
  5. | global_variables |
  6. | stats_memory_metrics |
  7. | stats_mysql_commands_counters |
  8. | stats_mysql_connection_pool |
  9. | stats_mysql_connection_pool_reset |
  10. | stats_mysql_global |
  11. | stats_mysql_prepared_statements_info |
  12. | stats_mysql_processlist |
  13. | stats_mysql_query_digest |
  14. | stats_mysql_query_digest_reset |
  15. | stats_mysql_query_rules |
  16. | stats_mysql_users |
  17. | stats_proxysql_servers_checksums |
  18. | stats_proxysql_servers_metrics |
  19. | stats_proxysql_servers_status |
  20. +--------------------------------------+
  21. 15 rows in set (0.00 sec)
显示所有数据表:monitor【ProxySQL】
  1. Admin> show tables from monitor;
  2. +------------------------------------+
  3. | tables |
  4. +------------------------------------+
  5. | mysql_server_connect_log |//MySQL表连接日志,connect_error为NULL则成功
  6. | mysql_server_group_replication_log |//MySQL主从复制日志
  7. | mysql_server_ping_log |//MySQL表PING日志,connect_error为NULL则成功
  8. | mysql_server_read_only_log |//MySQL从库read_only值监控日志,以备自动切换主从状态
  9. | mysql_server_replication_lag_log |//MySQL服务主从延迟的检测
  10. +------------------------------------+
  11. 5 rows in set (0.00 sec)
 示所有数据表:stats_history【ProxySQL】
  1. Admin> show tables from stats_history;
  2. +------------------------+
  3. | tables |
  4. +------------------------+
  5. | mysql_connections |
  6. | mysql_connections_day |
  7. | mysql_connections_hour |
  8. | mysql_query_cache |
  9. | mysql_query_cache_day |
  10. | mysql_query_cache_hour |
  11. | system_cpu |
  12. | system_cpu_day |
  13. | system_cpu_hour |
  14. | system_memory |
  15. | system_memory_day |
  16. | system_memory_hour |
  17. +------------------------+
  18. 12 rows in set (0.02 sec)

ProxySQL教程

ProxySQL库、表介绍

https://github.com/sysown/proxysql/wiki/Main-(runtime) 

转载于:https://my.oschina.net/cart/blog/2907232

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/230619
推荐阅读
相关标签
  

闽ICP备14008679号