1. 首页 > 国产数据库教程 > GreatSQL教程 > 正文

greatsql教程FG021-GreatSQL中间件实战

内容简介

本教程详细介绍GreatSQL数据库的中间件,包括中间件的概念、常用中间件、安装配置、使用管理等内容。风哥教程参考GreatSQL官方文档中间件指南,帮助读者掌握中间件的使用和管理。

中间件是连接应用和数据库的桥梁,通过中间件可以实现读写分离、负载均衡、分库分表等功能,提高系统性能和可用性。本教程将从基础概念入手,逐步深入到实战案例和最佳实践。

目录大纲

Part01-基础概念与理论知识

1.1 中间件概述

中间件是位于应用和数据库之间的软件层,负责处理应用与数据库之间的通信,提供各种功能如读写分离、负载均衡、分库分表等。

中间件的主要特点:

  • 透明性:应用无需感知后端数据库的具体配置
  • 高可用性:提供故障自动切换
  • 负载均衡:分散数据库压力
  • 扩展性:支持水平扩展

1.2 中间件的作用

中间件的主要作用包括:

  • 读写分离:将读操作和写操作分离到不同的数据库实例
  • 负载均衡:在多个数据库实例之间分配负载
  • 分库分表:将数据分散到多个数据库或表中
  • 故障切换:当主库故障时自动切换到从库
  • 连接池管理:优化数据库连接的使用
  • 监控与告警:监控数据库状态,提供告警机制

1.3 常用中间件介绍

常用的MySQL中间件包括:

  • ProxySQL:开源的MySQL代理,支持读写分离、负载均衡、故障切换
  • Sharding-JDBC:Java语言编写的分库分表中间件
  • MyCAT:开源的分布式数据库中间件,支持分库分表、读写分离
  • MaxScale:MariaDB开发的数据库代理,支持读写分离、负载均衡
  • Atlas:360开源的MySQL代理,支持读写分离、分库分表

Part02-生产环境规划与建议

2.1 中间件选型

风哥提示:中间件选型应根据业务需求、技术栈和运维能力进行综合考虑。

中间件选型建议:

  • ProxySQL:适合需要读写分离、负载均衡的场景,性能优异,配置灵活
  • Sharding-JDBC:适合Java应用,需要分库分表的场景
  • MyCAT:适合需要复杂分库分表策略的场景
  • MaxScale:适合MariaDB环境,功能丰富
  • Atlas:适合简单的读写分离场景,配置简单

2.2 架构设计

中间件架构设计建议:

  • 高可用设计:部署多个中间件实例,避免单点故障
  • 网络拓扑:中间件与数据库部署在同一网络,减少网络延迟
  • 监控设计:部署监控系统,实时监控中间件状态
  • 安全设计:配置防火墙,限制访问权限

2.3 性能优化建议

中间件性能优化建议:

  • 合理配置连接池大小
  • 优化中间件缓存设置
  • 调整负载均衡策略
  • 定期清理中间件日志
  • 监控中间件性能指标

更多视频教程www.fgedu.net.cn

Part03-生产环境项目实施方案

3.1 中间件安装配置

中间件安装配置步骤:

  1. 选择合适的中间件
  2. 下载并安装中间件
  3. 配置中间件参数
  4. 测试中间件功能
  5. 部署到生产环境

3.2 中间件集成方案

中间件集成方案:

  • 应用集成:修改应用连接字符串,连接到中间件
  • 数据库集成:配置中间件连接到后端数据库
  • 监控集成:将中间件监控集成到现有监控系统
  • 告警集成:配置中间件告警到现有告警系统

3.3 监控与管理方案

监控与管理方案:

  • 监控中间件运行状态
  • 监控中间件性能指标
  • 监控后端数据库状态
  • 定期备份中间件配置
  • 制定中间件维护计划

Part04-生产案例与实战讲解

4.1 ProxySQL实战

# 安装ProxySQL
# 添加ProxySQL仓库 yum install -y https://repo.proxysql.com/ProxySQL/proxysql-2.4.x/centos/7/x86_64/proxysql-2.4.4-1-centos7.x86_64.rpm
# 启动ProxySQL
systemctl start proxysql systemctl enable proxysql

# 配置ProxySQL
# 连接ProxySQL管理接口
mysql -u admin -padmin -h 127.0.0.1 -P 6032
# 添加后端数据库服务器 INSERT INTO
mysql_servers (hostgroup_id, hostname, port, weight, max_connections) VALUES (1, ‘192.168.1.100’, 3306, 100, 1000), (2, ‘192.168.1.101’, 3306, 100, 1000), (2, ‘192.168.1.102’, 3306, 100, 1000);
# 配置读写分离规则 INSERT INTO
mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, apply) VALUES (1, 1, ‘^SELECT.*FOR UPDATE’, 1, 1), (2, 1, ‘^SELECT’, 2, 1);
# 配置用户 INSERT INTO
mysql_users (username, password, default_hostgroup) VALUES (‘fgedu’, ‘Fgedu@123456’, 1);
# 加载配置到运行时 LOAD MYSQL SERVERS TO RUNTIME; LOAD MYSQL USERS TO RUNTIME; LOAD MYSQL QUERY RULES TO RUNTIME;
# 保存配置到磁盘 SAVE MYSQL SERVERS TO DISK; SAVE MYSQL USERS TO DISK; SAVE MYSQL QUERY RULES TO DISK;

Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)

学习交流加群风哥微信: itpux-com

# 测试ProxySQL连接
mysql -u fgedu -pFgedu@123456 -h 127.0.0.1 -P 6033 -e “SELECT @@hostname;” mysql -u fgedu -pFgedu@123456 -h 127.0.0.1 -P 6033 -e “SELECT * FROM fgedudb.fgedu_users;”

+————+
| @@hostname |
+————+
| fgedu.net.cn |
+————+
+—-+———-+——————+
| id | username | email |
+—-+———-+——————+
| 1 | fgedu_user1 | fgedu_user1@fgedu.net.cn |
| 2 | fgedu_user2 | fgedu_user2@fgedu.net.cn |
| 3 | fgedu_user3 | fgedu_user3@fgedu.net.cn |
+—-+———-+——————+

4.2 Sharding-JDBC实战

# 添加Sharding-JDBC依赖 org.apache.shardingsphere sharding-jdbc-spring-boot-starter 5.1.1

# 配置Sharding-JDBC
# application.yml spring: shardingsphere: datasource: names: ds0, ds1 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://192.168.1.100:3306/fgedudb?serverTimezone=UTC username: root password: GreatSQL123! ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://192.168.1.101:3306/fgedudb?serverTimezone=UTC username: root password: GreatSQL123! rules: sharding: tables: fgedu_orders: actual-data-nodes: ds$->{0..1}.fgedu_orders_$->{202601..202612} table-strategy: standard: sharding-column: created_at sharding-algorithm-name: order-table-inline database-strategy: standard: sharding-column: user_id sharding-algorithm-name: order-db-inline sharding-algorithms: order-table-inline: type: INLINE props: algorithm-expression: fgedu_orders_${date_format(created_at, ‘yyyyMM’)} order-db-inline: type: INLINE props: algorithm-expression: ds${user_id % 2} props: sql-show: true

学习交流加群风哥QQ113257174

4.3 MyCAT实战

# 安装MyCAT
# 下载MyCAT wget http://dl.mycat.org.cn/1.6.7.6/Mycat-server-1.6.7.6-release-20220524173809-linux.tar.gz
# 解压MyCAT tar -zxvf Mycat-server-1.6.7.6-release-20220524173809-linux.tar.gz -C /greatsql/
# 配置MyCAT
# 修改server.xml
cat > /greatsql/mycat/conf/server.xml << 'EOF' Fgedu@123456 fgedudb EOF
# 修改schema.xml
cat > /greatsql/mycat/conf/schema.xml << 'EOF'

select user() select user() EOF
# 启动MyCAT /greatsql/mycat/bin/mycat start

# 测试MyCAT连接
mysql -u fgedu -pFgedu@123456 -h 127.0.0.1 -P 8066 -e “SELECT @@hostname;” mysql -u fgedu -pFgedu@123456 -h 127.0.0.1 -P 8066 -e “SELECT * FROM fgedudb.fgedu_orders;”

+————+
| @@hostname |
+————+
| fgedu.net.cn |
+————+
+—-+—————+———+——–+——–+———————+
| id | order_no | user_id | amount | status | created_at |
+—-+—————+———+——–+——–+———————+
| 1 | ORD20260409001 | 1 | 100.00 | 1 | 2026-04-09 20:00:00 |
| 2 | ORD20260409002 | 2 | 200.00 | 1 | 2026-04-09 20:00:00 |
| 3 | ORD20260409003 | 3 | 150.00 | 1 | 2026-04-09 20:00:00 |
+—-+—————+———+——–+——–+———————+

Part05-风哥经验总结与分享

5.1 中间件最佳实践

  • 合理选型:根据业务需求选择合适的中间件
  • 高可用部署:部署多个中间件实例,避免单点故障
  • 性能优化:根据实际情况调整中间件参数
  • 监控管理:建立完善的监控和管理机制
  • 备份恢复:定期备份中间件配置,确保可恢复性
  • 版本管理:使用稳定版本的中间件,定期更新
  • 安全配置:配置合理的访问权限,确保系统安全

5.2 常见问题与解决方案

问题 解决方案
中间件性能下降 优化中间件配置,增加硬件资源
中间件故障 部署高可用架构,实现自动故障切换
连接数过多 优化连接池配置,调整最大连接数
数据不一致 确保主从复制正常,使用事务保证一致性
配置错误 仔细检查配置文件,定期备份配置

更多学习教程公众号风哥教程itpux_com

5.3 性能优化技巧

# 创建中间件监控脚本
cat > /greatsql/scripts/middleware_monitor.sh << 'EOF'
#!/bin/bash # middleware_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
echo “=== Middleware Monitor ===” echo “Date: $(date)” echo “”
# 检查ProxySQL状态
echo “1. Checking ProxySQL status…” systemctl status proxysql echo “”
# 检查MyCAT状态
echo “2. Checking MyCAT status…” /greatsql/mycat/bin/mycat status echo “”
# 检查ProxySQL连接数
echo “3. Checking ProxySQL connections…” mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM stats_mysql_connection_pool;” echo “”
# 检查ProxySQL查询统计
echo “4. Checking ProxySQL query stats…” mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM stats_mysql_query_digest ORDER BY count_star DESC LIMIT 10;” echo “” echo “Middleware monitoring completed!” EOF
# 设置脚本权限
chmod +x /greatsql/scripts/middleware_monitor.sh

# 执行中间件监控脚本 /greatsql/scripts/middleware_monitor.sh

=== Middleware Monitor ===
Date: Wed Apr 9 20:00:00 CST 2026
1. Checking ProxySQL status…
● proxysql.service – High Performance MySQL Proxy
Loaded: loaded (/usr/lib/systemd/system/proxysql.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2026-04-09 20:00:00 CST; 1h 0min ago
Main PID: 12345 (proxysql)
Status: “ProxySQL 2.4.4 is running (705418616)”
Tasks: 12
Memory: 100.0M
CPU: 5.0%
CGroup: /system.slice/proxysql.service
└─12345 /usr/bin/proxysql -c /etc/proxysql.cnf
2. Checking MyCAT status…
Mycat-server is running (23456).
3. Checking ProxySQL connections…
+————–+———-+—————–+—————–+——————+——————+——————+——————-+——————-+——————-+——————+——————+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | current_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+————–+———-+—————–+—————–+——————+——————+——————+——————-+——————-+——————-+——————+——————+
| 1 | 192.168.1.100 | 3306 | ONLINE | 100 | 0 | 1000 | 10 | 0 | 0 | 0 | NULL |
| 2 | 192.168.1.101 | 3306 | ONLINE | 100 | 0 | 1000 | 5 | 0 | 0 | 0 | NULL |
| 2 | 192.168.1.102 | 3306 | ONLINE | 100 | 0 | 1000 | 5 | 0 | 0 | 0 | NULL |
+————–+———-+—————–+—————–+——————+——————+——————+——————-+——————-+——————-+——————+——————+
4. Checking ProxySQL query stats…
+———–+—————-+——————+——————+——————+——————+——————+——————+——————+——————+——————+
| hostgroup | schemaname | digest | count_star | count_read | count_write | sum_time | min_time | max_time | sum_rows_affected | sum_rows_sent |
+———–+—————-+——————+——————+——————+——————+——————+——————+——————+——————+——————+
| 2 | fgedudb | SELECT * FROM fgedu_users | 100 | 100 | 0 | 1000 | 5 | 20 | 0 | 300 |
| 1 | fgedudb | INSERT INTO fgedu_orders | 50 | 0 | 50 | 500 | 8 | 15 | 50 | 0 |
+———–+—————-+——————+——————+——————+——————+——————+——————+——————+——————+——————+
Middleware monitoring completed!

中间件选型建议

  • 小型应用:Atlas或ProxySQL,配置简单,易于维护
  • 中型应用:ProxySQL或MaxScale,性能优异,功能丰富
  • 大型应用:MyCAT或Sharding-JDBC,支持复杂分库分表
  • Java应用:Sharding-JDBC,与Java应用集成紧密
  • 多语言应用:ProxySQL或MyCAT,支持多种编程语言

中间件实施建议

风哥提示:中间件的实施需要考虑性能、可靠性和可维护性,选择合适的中间件并合理配置是成功的关键。
  • 在测试环境充分验证中间件功能
  • 制定详细的部署和回滚计划
  • 培训运维人员,掌握中间件管理技能
  • 建立完善的监控和告警机制
  • 定期进行中间件性能评估和优化

中间件案例分享

案例背景:某电商平台需要提高数据库性能和可用性,应对高并发访问。

解决方案:

  1. 部署ProxySQL实现读写分离
  2. 配置3个从库分担读负载
  3. 实现故障自动切换
  4. 监控中间件和数据库状态

实施效果:

  • 读性能提升3倍以上
  • 系统可用性达到99.99%
  • 故障恢复时间从小时级缩短到分钟级

from greatsql视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息