再做mysql的读写分离时候,尝试了mysql-router中间组件发现mysql-router更多的是负载均衡的内容
centos6.7中yum库中有 mysql-router-community-8.0.13-1.el6.x86_64
直接yum安装后查看安装路径 rpm -ql mysql-router-community-8.0.13-1.el6.x86_64
[root@mycat3 mysqlrouter]# rpm -ql mysql-router-community-8.0.13-1.el6.x86_64
/etc/init.d/mysqlrouter #发现已经添加到系统服务中了
/etc/mysqlrouter #配置文件目录
/etc/mysqlrouter/mysqlrouter.conf #配置文件
/usr/bin/mysqlrouter
/usr/bin/mysqlrouter_plugin_info
/usr/lib64/libmysqlharness.so.1
/usr/lib64/libmysqlrouter.so.1
/usr/lib64/mysqlrouter
/usr/lib64/mysqlrouter/keepalive.so
/usr/lib64/mysqlrouter/metadata_cache.so
/usr/lib64/mysqlrouter/mysql_protocol.so
/usr/lib64/mysqlrouter/routing.so
/usr/lib64/mysqlrouter/syslog.so
/usr/share/doc/mysql-router-community-8.0.13
/usr/share/doc/mysql-router-community-8.0.13/LICENSE.router
/usr/share/doc/mysql-router-community-8.0.13/README.router
/var/log/mysqlrouter
/var/run/mysqlrouter
编辑mysqlrouter.conf配置文件
[root@mycat3 mysqlrouter]# vim /etc/mysqlrouter/mysqlrouter.conf
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# MySQL Router configuration file
#
# Documentation is available at
# http://dev.mysql.com/doc/mysql-router/en/
[DEFAULT]
logging_folder = /var/log/mysqlrouter
runtime_folder = /var/run/mysqlrouter
config_folder = /etc/mysqlrouter
[logger]
level = INFO
# If no plugin is configured which starts a service, keepalive
# will make sure MySQL Router will not immediately exit. It is
# safe to remove once Router is configured.
[keepalive]
interval = 60
[routing:read_write] #增加读写组件
bind_address = 192.168.3.3 #代理主机ip
bind_port = 7001 #代理主机的端口
destinations = 192.168.3.7:3306 #后端逻辑库
mode = read-write #模式为可读可写
[routing:read_only] # 增加可读组件
bind_address = 192.168.3.3 #代理主机ip
bind_port = 7002 #代理主机端口
destinations = 192.168.3.8:3306 #后端逻辑库
mode = read-only #模式为可读
其中在可读可写组件中可增加多条库名 形成可读可写库池,根据算法来负载均衡,选择哪一条;
也可以在可读组件中添加更多slave库,形成了可读库池,根据算法来负载均衡,选择可读库池中的一条
更像是拿出代理主机的某一个端口,指定来代理某一个功能,可读可写给一个端口,把这个端口指定功能(可读可写)后端逻辑库的池,通过算法来负载均衡,选择库池中的一个。
参考文档 mysql-router的官网
https://dev.mysql.com/doc/mysql-router/8.0/en/mysql-router-deploying-basic-routing.html
routing_strategy
属性 值
介绍 8.0.4
类型 串
有效值
first-available
next-available
round-robin
round-robin-with-fallback
路由策略定义了MySQL路由器选择连接MySQL服务器的方式。
重要
MySQL Router 8.0.4引入了该 routing_strategy选项作为定义策略的更灵活方式。以前,此行为是使用现已弃用的mode选项定义的 。
双方routing_strategy并 mode不能在同一时间进行设置。设置一个是静态路由,而InnoDB集群是可选的。
可用策略:
first-available:新连接从目标列表路由到第一个可用服务器。如果失败,则使用下一个可用服务器。此循环将持续到所有服务器都不可用。
next-available:喜欢 first-available,因为新连接从目的地列表路由到第一个可用服务器。不同的是first-available,如果服务器被标记为无法访问,则它将被丢弃,并且永远不会再次用作目标。
这种策略是落后与MySQL路由器2.X的兼容 mode的读写 性能。其局限性包括:
在丢弃所有选择节点之后,无法将服务器添加回列表。
重新启动MySQL路由器后,所有丢弃服务器的知识都将丢失,所有服务器都将再次可用。
round-robin:对于负载平衡,每个新连接都以循环方式进入下一个可用服务器。
round-robin-with-fallback:对于负载平衡,每个新连接以循环方式对下一个可用的辅助服务器进行。如果辅助服务器不可用,则主列表中的服务器将以循环方式使用。
如果mode是读写模式,则默认策略从MySQL Router 8.0开始可用,并在MySQL Router 2.1中显示 下一个可用的行为。
如果mode是只读的,则默认策略为round-robin。
如果InnoDB集群角色是PRIMARY,则默认策略是round-robin, 首先可用和 循环都是可用的策略。
如果InnoDB的集群作用是二次,默认的策略是round-robin,和 第一可用, 循环赛和 循环与-回退是可用的策略。
如果InnoDB集群角色为 PRIMARY_AND_SECONDARY,则默认策略为round-robin, 首先可用和 循环都是可用策略。
max_connections
属性 值
类型 整数
默认值 512
最低价值 1
最大价值 65536
每个路由都可以限制路由或连接的数量。一种可能的用途是帮助防止可能的拒绝服务(DOS)攻击。默认值为512,有效范围介于1和65536之间。
这类似于MySQL Server的max_connections服务器系统变量。
max_connections = 512
注意
MySQL路由器2.1.5和8.0.4引入了将并发连接限制从大约500到5000连接增加的功能。此操作系统相关限制已更改为使用poll()实现而不是select()。
thread_stack_size
属性 值
介绍 8.0.12
类型 整数
默认值 64
最低价值 1
最大价值 65535
为每个线程分配的堆栈大小。它以千字节为单位,默认为64。
[DEFAULT]
thread_stack_size=128
max_connect_errors
属性 值
类型 整数
默认值 100
最低价值 1
最大价值 4294967295
默认值为100,有效范围介于1和2 ^ 32之间(4294967295,无符号整数)。
这类似于 MySQL Server的max_connect_errors服务器系统变量。
如果应用程序执行频繁的重新连接,这可能会导致轻微的性能损失,因为MySQL路由器会尝试发现是否存在与连接相关的错误。
每个路由都有自己的被阻止主机列表。被阻止的客户端收到MySQL服务器错误1129代码,错误消息略有不同:“1129:来自fail.example.com的连接错误太多”。路由器日志包含被阻止客户端的额外信息,例如:fail.example.com的INFO […] 1身份验证错误(最多100个)警告[…]阻止客户端主机fail.example.com
max_connect_errors = 100
client_connect_timeout
属性 值
类型 整数
默认值 9
最低价值 2
最大价值 31536000
这类似于MySQL Server的connect_timeout服务器系统变量。
默认值为9,比MySQL 5.7默认值小1。有效范围介于2和31536000之间。
client_connect_timeout = 9