MySQL 中心件 MySQL-Router

[复制链接]
发表于 2026-2-1 00:42:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
1 MySQL-Router 的先容

2 MySQL-Router 负载平衡

2.1 筹划目的:

2.2 HAProxy 与 Nginx 和 MySQL-Router 之间的区别

2.3 MySQL-Router 的上风

3 MySQL-Router 的获取

3 MySQL-Router 的使用

3.1 实行情况

3.2 MySQL-Router 摆设

3.3 MySQL-Router 设置

3.4 测试实现的效果


1 MySQL-Router 的先容

MySQL Router 是 MySQL 官方提供的一款中心件软件,它重要用于实现数据库的路由和负载平衡功能。MySQL Router 可以资助开辟者更轻松地管理复杂的数据库架构,尤其是在须要处理惩罚多个数据库实例大概须要实现读写分离等场景下非常有效。

2 MySQL-Router 负载平衡


2.1 筹划目的:



  • MySQL Router 是专门为 MySQL 数据库筹划的官方工具,重要用来处理惩罚 MySQL 数据库的负载平衡、读写分离等任务。
  • 它内置了对 MySQL 协议的明确和支持,因此在处理惩罚 MySQL 干系任务时更加高效和可靠。
2.2 HAProxy 与 Nginx 和 MySQL-Router 之间的区别



  • MySQL Router 更恰当用于 MySQL 数据库干系的任务,提供了对 MySQL 协议的深入支持,简化了读写分离和故障转移的设置。
  • HAProxy 和 Nginx 则更恰当于更广泛的负载平衡场景,不但仅范围于数据库层面,可以用于多种范例的 TCP/HTTP 服务。
2.3 MySQL-Router 的上风



  • 专门针对 MySQL: MySQL Router 专为 MySQL 筹划,可以大概更好地明确和处理惩罚 MySQL 协议,提供更好的兼容性和优化。
  • 读写分离: 支持自动的读写分离,可以根据查询范例将哀求路由到准确的数据库实例。
  • 高可用性: 支持自动故障检测和规复,可以大概快速地将流量重新路由到康健的实例。
  • 多协议支持: 除了 MySQL 协议外,还支持 RESTful API 和 WebSocket 协议,恰当多种应用场景。

3 MySQL-Router 的获取

MySQL-Router
https://downloads.mysql.com/archives/router/



3 MySQL-Router 的使用

3.1 实行情况

捏造机MySQL-RouterMySQL-01MySQL-02MySQL-03IP地点192.168.239.50192.168.239.210192.168.239.220192.168.239.230端标语  7001 | 70023306330633063.2 MySQL-Router 摆设

开一台没有使用 过的捏造机今世理服务器
  1. [root@docker-rhel src]# ls
  2. mysql-router-community-8.4.0-1.el7.x86_64.rpm
  3. [root@docker-rhel src]# yum install mysql-router-community-8.4.0-1.el7.x86_64.rpm -y
复制代码
3.3 MySQL-Router 设置

  1. [root@docker-rhel src]# vim /etc/mysqlrouter/mysqlrouter.conf
  2. [routing:ro]
  3. bind_address = 0.0.0.0
  4. bind_port = 7001
  5. destinations = 192.168.239.210:3306,192.168.239.220:3306,192.168.239.230:3306
  6. routing_strategy = round-robin #轮循
  7. [routing:rw]
  8. bind_address = 0.0.0.0
  9. bind_port = 7002
  10. destinations = 192.168.239.230:3306,192.168.239.220:3306,192.168.239.210:3306
  11. routing_strategy = first-available  # 第一个链接的就会一直链接
  12. # routing:ro - 这里要说明他只是给配置人员看的只读,他不能限制实现只读或者读写功能
  13. # bind_address = 0.0.0.0: 绑定到所有可用网络接口。
  14. # bind_port = 7001: 监听端口为 7001。
  15. # destinations = 192.168.239.210:3306,192.168.239.220:3306,192.168.239.230:3306: 指定了三个后端 MySQL 实例的地址和端口。
  16. # routing_strategy = round-robin: 表明使用轮询策略来均衡读取请求。
复制代码
重启MySQL-Router 服务 
  1. [root@docker-rhel src]# systemctl restart mysqlrouter.service
复制代码
 检察端口是否开放
  1. [root@docker-rhel src]# netstat -tunlpe | grep 7001
  2. tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      987        61464      3776/mysqlrouter   
  3. [root@docker-rhel src]# netstat -tunlpe | grep 7002
  4. tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      987        58689      3776/mysqlrouter  
复制代码


3.4 测试实现的效果

  1. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------------+
  4. | @@HOSTNAME |
  5. +------------+
  6. | mysql-01   |
  7. +------------+
  8. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
  9. mysql: [Warning] Using a password on the command line interface can be insecure.
  10. +------------+
  11. | @@HOSTNAME |
  12. +------------+
  13. | mysql-02   |
  14. +------------+
  15. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +------------+
  18. | @@HOSTNAME |
  19. +------------+
  20. | mysql-3    |
  21. +------------+
  22. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7001 -e "SELECT @@HOSTNAME;"
  23. mysql: [Warning] Using a password on the command line interface can be insecure.
  24. +------------+
  25. | @@HOSTNAME |
  26. +------------+
  27. | mysql-01   |
  28. +------------+
  29. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7002 -e "SELECT @@HOSTNAME;"
  30. mysql: [Warning] Using a password on the command line interface can be insecure.
  31. +------------+
  32. | @@HOSTNAME |
  33. +------------+
  34. | mysql-3    |
  35. +------------+
  36. [root@mysql-3 ~]# mysql -ushuyan -pOpenlab123! -h 192.168.239.50 -P 7002 -e "SELECT @@HOSTNAME;"
  37. mysql: [Warning] Using a password on the command line interface can be insecure.
  38. +------------+
  39. | @@HOSTNAME |
  40. +------------+
  41. | mysql-3    |
  42. +------------+
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表