MySQL 中间件 MySQL-Router

目录

[1 MySQL-Router 的介绍](#1 MySQL-Router 的介绍)

[2 MySQL-Router 负载均衡](#2 MySQL-Router 负载均衡)

[2.1 设计目的:](#2.1 设计目的:)

[2.2 HAProxy 与 Nginx 和 MySQL-Router 之间的区别](#2.2 HAProxy 与 Nginx 和 MySQL-Router 之间的区别)

[2.3 MySQL-Router 的优势](#2.3 MySQL-Router 的优势)

[3 MySQL-Router 的获取](#3 MySQL-Router 的获取)

[3 MySQL-Router 的使用](#3 MySQL-Router 的使用)

[3.1 实验环境](#3.1 实验环境)

[3.2 MySQL-Router 部署](#3.2 MySQL-Router 部署)

[3.3 MySQL-Router 配置](#3.3 MySQL-Router 配置)

[3.4 测试实现的效果](#3.4 测试实现的效果)


1 MySQL-Router 的介绍

MySQL Router 是 MySQL 官方提供的一款中间件软件,它主要用于实现数据库的路由和负载均衡功能。MySQL Router 可以帮助开发者更轻松地管理复杂的数据库架构,尤其是在需要处理多个数据库实例或者需要实现读写分离等场景下非常有用。

2 MySQL-Router 负载均衡

2.1 设计目的:

  • MySQL Router 是专门为 MySQL 数据库设计的官方工具,主要用来处理 MySQL 数据库的负载均衡、读写分离等任务。
  • 它内置了对 MySQL 协议的理解和支持,因此在处理 MySQL 相关任务时更加高效和可靠。

2.2 HAProxyNginx 和 MySQL-Router 之间的区别

  • MySQL Router 更适合用于 MySQL 数据库相关的任务,提供了对 MySQL 协议的深入支持,简化了读写分离和故障转移的配置。
  • HAProxyNginx 则更适合于更广泛的负载均衡场景,不仅仅局限于数据库层面,可以用于多种类型的 TCP/HTTP 服务。

2.3 MySQL-Router 的优势

  • 专门针对 MySQL: MySQL Router 专为 MySQL 设计,能够更好地理解和处理 MySQL 协议,提供更好的兼容性和优化。
  • 读写分离: 支持自动的读写分离,可以根据查询类型将请求路由到正确的数据库实例。
  • 高可用性: 支持自动故障检测和恢复,能够快速地将流量重新路由到健康的实例。
  • 多协议支持: 除了 MySQL 协议外,还支持 RESTful API 和 WebSocket 协议,适合多种应用场景。

3 MySQL-Router 的获取

MySQL-Routerhttps://downloads.mysql.com/archives/router/

3 MySQL-Router 的使用

3.1 实验环境

虚拟机 MySQL-Router MySQL-01 MySQL-02 MySQL-03
IP地址 192.168.239.50 192.168.239.210 192.168.239.220 192.168.239.230
端口号 7001 | 7002 3306 3306 3306

3.2 MySQL-Router 部署

开一台没有使用 过的虚拟机当代理服务器

bash 复制代码
[root@docker-rhel src]# ls
mysql-router-community-8.4.0-1.el7.x86_64.rpm
[root@docker-rhel src]# yum install mysql-router-community-8.4.0-1.el7.x86_64.rpm -y

3.3 MySQL-Router 配置

bash 复制代码
[root@docker-rhel src]# vim /etc/mysqlrouter/mysqlrouter.conf 

[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 192.168.239.210:3306,192.168.239.220:3306,192.168.239.230:3306
routing_strategy = round-robin #轮循

[routing:rw]
bind_address = 0.0.0.0
bind_port = 7002
destinations = 192.168.239.230:3306,192.168.239.220:3306,192.168.239.210:3306
routing_strategy = first-available  # 第一个链接的就会一直链接

# routing:ro - 这里要说明他只是给配置人员看的只读,他不能限制实现只读或者读写功能
# bind_address = 0.0.0.0: 绑定到所有可用网络接口。
# bind_port = 7001: 监听端口为 7001。
# destinations = 192.168.239.210:3306,192.168.239.220:3306,192.168.239.230:3306: 指定了三个后端 MySQL 实例的地址和端口。
# routing_strategy = round-robin: 表明使用轮询策略来均衡读取请求。

重启MySQL-Router 服务

bash 复制代码
[root@docker-rhel src]# systemctl restart mysqlrouter.service 

查看端口是否开放

bash 复制代码
[root@docker-rhel src]# netstat -tunlpe | grep 7001
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      987        61464      3776/mysqlrouter    
[root@docker-rhel src]# netstat -tunlpe | grep 7002
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      987        58689      3776/mysqlrouter  

3.4 测试实现的效果

bash 复制代码
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-01   |
+------------+
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-02   |
+------------+
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-3    |
+------------+
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-01   |
+------------+
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7002 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-3    |
+------------+
[root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7002 -e "SELECT @@HOSTNAME;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@HOSTNAME |
+------------+
| mysql-3    |
+------------+
相关推荐
试试勇气17 小时前
Linux学习笔记(九)--Linux进程终止与进程等待
linux·笔记·学习
GIS数据转换器17 小时前
带高度多边形,生成3D建筑模型,支持多种颜色或纹理的OBJ、GLTF、3DTiles格式
数据库·人工智能·机器学习·3d·重构·无人机
盒马coding17 小时前
第19节-非规范化数据类型-Drop-Type
数据库·postgresql
AORO202517 小时前
航运、应急、工业适用,AORO P1100三防平板引领行业数字化变革
运维·服务器·网络·智能手机·电脑·信息与通信
wheeldown17 小时前
【Linux】Linux 进程信号核心拆解:pending/block/handler 三张表 + signal/alarm 实战
linux·运维·服务器
一人の梅雨17 小时前
大麦网关键词列表接口的产业级实现:从演出聚合到市场趋势预测的全维度技术方案
大数据·数据库·人工智能
运维老司机17 小时前
ThinkPad 安装 Ubuntu 系统教程
linux·运维·ubuntu
云飞云共享云桌面19 小时前
替代传统电脑的共享云服务器如何实现1拖8SolidWorks设计办公
linux·运维·服务器·网络·电脑·制造
AI云原生19 小时前
云原生系列Bug修复:Docker镜像无法启动的终极解决方案与排查思路
运维·服务器·python·docker·云原生·容器·bug
专业软件系统开发20 小时前
药品说明书查询系统源码 本地数据库 PHP版本
数据库·查询系统源码·说明书查询源码