18

Maxscale介绍和应用-10931853

 4 years ago
source link: https://blog.51cto.com/wujianwei/2474493
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本博文演示环境:
centos7.2 x86_64 最小化安装。3台vm 时间同步,关闭iptables,selinux.,firewalld.
3台rpm包安装MariaDB10.5.1 。172.16.0.130机器单独安装 maxscale-2.3.7

172.16.0.130    master    maxscale
172.16.0.131    slave1
172.16.0.132    slave2

一.Maxscale简单介绍:

如何配置MariaDB MaxScale,并介绍了一些可能的使用方案。 MariaDB MaxScale在设计时考虑了灵活性,它由事件处理核心和各种支持功能以及可定制程序行为的插件模块组成
MariaDB MaxScale是一种数据库代理,可扩展MariaDB Server的高可用性,可伸缩性和安全性,同时通过将其与基础数据库基础架构分离来简化应用程序开发。
MariaDB MaxScale采用可扩展的体系结构进行工程设计,以支持插件,从而将其功能扩展到透明负载平衡之外,从而成为例如数据库防火墙。
借助用于多个路由器,过滤器和协议的内置插件,可以将MariaDB MaxScale配置为转发数据库请求并根据业务和技术要求修改数据库响应,例如,以屏蔽敏感数据或扩展读取

MariaDB MaxScale是一种数据库代理,可将数据库语句转发到一个或多个数据库服务器。
使用基于数据库语句的语义理解以及数据库后端集群中服务器的角色的规则执行转发。
MariaDB MaxScale旨在向应用程序透明地提供负载平衡和高可用功能。
MariaDB MaxScale具有可扩展且灵活的体系结构,其插件组件可支持不同的协议和路由方法

MariaDB MaxScale充分利用了Linux操作系统的异步I / O功能以及固定数量的工作线程。
epoll用于为通过套接字的输入和输出提供事件驱动的框架。
MariaDB MaxScale提供的许多服务都实现为在运行时加载的外部共享对象模块。
这些模块支持固定接口,并通过由一组功能指针组成的结构传递入口点。该结构称为“模块对象”。可以创建其他模块以与MariaDB MaxScale一起使用。
常用的模块类型是协议,路由器和过滤器。协议模块实现了客户端与MariaDB MaxScale之间以及MariaDB MaxScale与后端服务器之间的通信。
路由器检查来自客户端的查询并确定目标后端。这些决定通常基于路由规则和后端服务器状态。
过滤器对通过MariaDB MaxScale传递的数据进行处理。
筛选器通常用于记录查询或修改服务器响应。

二.Maxscale 安装:

支持源码包编译安装,支持rpm包安装,也支持二进制tar包安装
官方安装文档:
提问地址:
https://github.com/mariadb-corporation/MaxScale/tree/2.3/Documentation

反馈bug地址:
https://jira.mariadb.org/browse/MXS

本演示环境安装的是maxscale 是2.3.7 ,下面是官方的安装文档介绍
https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-installation-guide/

本博文采用的是rpm包安装,最简单

 yum install libaio.x86_64 libaio-devel.x86_64 novacom-server.x86_64 libedit -y
 wget https://downloads.mariadb.com/MaxScale/centos/7/x86_64/maxscale-2.3.7-1.centos.7.x86_64.rpm
rpm -ivh maxscale-2.3.7-1.centos.7.x86_64.rpm

 关闭和启动命令:
  service maxscale status|start|stop|restart
  systemctl enable maxscale
  systemctl  status|start|stop maxscale
  maxadmin shutdown maxscale

maxscale 管理账户登录: 默认账户:admin 
[root@mgr01 ~]# maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'

三.配置文件介绍:

默认配置文件在/etc/maxscale.cnf

官方模块参数介绍地址:
https://mariadb.com/kb/en/mariadb-maxscale-23-contents/#routers
配置文件参数介绍:
https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-configuration-usage-scenarios/

在名为[MaxScale]的部分中的全局设置允许调整影响整个MariaDB MaxScale的各种参数。此部分必须在默认为/etc/maxscale.cnf的根配置文件中定义
参数:threads 介绍:threads=[<number of threads> | auto ] 默认是1,最好是根据实际需要来调整
将工作线程数量增加到超过处理器内核数量并不会提高性能,反而可能降低性能,并且可能不必要地消耗资源。

演示成功的配置文件内容:

[root@mgr03 ~]# cat /etc/maxscale.cnf
#https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-configuration-usage-scenarios/
[maxscale]
#threads=auto
threads=1
log_info=1
logdir=/tmp/
auth_read_timeout=10 
#提取用户身份验证数据时,MySQL与后端数据库的连接的读取超时(以秒为单位)。增加此参数的值将导致在主动获取用户数据时,MariaDB MaxScale等待更长的时间来等待来自后端服务器的响应。如果身份验证失败,并且您有大量的数据库用户和授权,或者与后端服务器的连接速度很慢,则增加此值是个好主意。默认为1秒.
auth_write_timeout=10
#提取用户身份验证数据时,MySQL与后端数据库的连接的写入超时(以秒为单位),当前,MariaDB MaxScale不会在后端服务器中写入或修改数据。默认值为2秒。官方建议10s
query_retries=1
#被重试的内部查询的次数。默认值为重试一次.中断查询是指任何因网络错误而中断的查询。连接超时包括在网络错误中,因此建议确保将query_retry_timeout的值设置为适当的值。2.3.0之后默认开启
query_retry_timeout=5
##任何重试查询的总超时时间(以秒为单位)。默认值为5秒,
ms_timestamp=1
#       ms_timestamp=<0|1>。
#       在日志文件中启用或禁用高精度时间戳。启用此选项可以为所有日志文件时间戳增加毫秒精度。
skip_permission_checks=false 
#默认是false.
#当您知道权限确定并且想要加快启动过程时,此功能很有用。此参数为布尔值,默认情况下处于禁用状态
#建议不要禁用权限检查,以便在启动maxscale时检测到任何缺少的特权。如果由于在检查权限时发生大量连接超时而导致MaxScale启动缓慢,则禁用权限检查可以加快启动过程。

syslog=1
#       syslog=<0|1>. 
#启用或禁用将消息记录到syslog。默认情况下,启用到syslog的日志记录
maxlog=1
# 启用以禁用将消息记录到MariaDB MaxScale的日志文件中. 默认情况下,启用对maxlog的日志记录。
# 要启用到MariaDB MaxScale日志文件的日志记录,请使用值1,而禁用则使用值0。
log_warning=1
#       log_warning=<0|1>,
#       启用或禁用系统日志优先级为警告的消息的日志记录。默认情况下,启用此优先级的消息.
log_notice=1
#启用或禁用系统日志优先级为notice的消息的日志记录。此优先级的消息提供有关MariaDB MaxScale的功能的信息,并且默认情况下处于启用状态。
##要禁用这些消息,请使用值0,而要启用它们,请使用值1

log_debug=0
[server1]
type=server
address=172.16.0.131
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1
[server2]
type=server
address=172.16.0.132
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1
[server3]
type=server
address=172.16.0.130
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1

# https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-monitor/
[MySQL Monitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3
user=maxmonuser
password=maxmonpwd
monitor_interval=10000
##monitor_interval=10000 #探测间隔,单位毫秒,默认2000
auto_failover=true
#是否故障自动切换
auto_rejoin=true
#故障实例恢复后自动加入集群
detect_standalone_master=true
#探测独立的master,是否允许集群中最后一个实例成为主库
#switchover_on_low_disk_space=true
##为了使此参数生效,必须为服务器或监视器指定disk_space_threshold。另外,必须为监视器定义disk_space_check_interval

#allow_cluster_recovery=true
failcount=3 
#在集群中最后一个实例成为主库前检查其它从库是否存活的次数,默认为5
detect_stale_master=true 
#此参数从2.0版本开始默认就是开启的,即使复制停止或配置错误,也允许先前的主服务器可用。所有从属服务器均不可访问或复制由于某种原因而中断,master依然可以继续提供服务。
detect_stale_slave=true
#此参数默认是开启的,将没有主服务器的正在运行的从服务器视为有效的从服务器。slave丢失master时,slave仍可用于读取。
failover_timeout=90
#此参数默认是90s,如果在配置的时间段内未成功进行故障转移/切换,则会记录一条消息,并禁用自动故障转移。这样可以防止对行为异常的群集进行进一步的自动修改.
verify_master_failure=true
master_failure_timeout=10
#启用其他主服务器故障验证以进行自动故障转移.verify_master_failure默认开启,是布尔值,master_failure_timeout默认是10.

script=/tmp/reset_slave.sh
events=master_down 
#检查到master挂掉的话,执行上面的脚本/tmp/reset_slave.sh

# https://mariadb.com/kb/en/mariadb-maxscale-23-readconnroute/
[Read-Only-Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=maxuser
password=maxpwd
router_options=slave
#router_options=master,slave
#weightby=server_weight
# ReadWriteSplit documentation:
# https://mariadb.com/kb/en/mariadb-maxscale-23-readwritesplit/

[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxuser
password=maxpwd
#max_slave_connections=100%
max_slave_connections=2
#设置路由器会话随时使用的最大从站数量。默认值是每个客户端连接最多使用255个从属连接。在旧版本中,默认设置是不加限制地使用所有可用的从站。
# 例如,如果您为MaxScale配置了一个主机和三个从机,并设置max_slave_connections = 2,则对于每个客户端连接,将打开到主机的连接和两个从机连接。然后,在这两个从属服务器之间完成读查询负载平衡,并将写操作发送到主服务器.
max_slave_replication_lag=4
#指定从站被允许落后于主站多少秒。如果滞后时间大于配置的值,则不能使用从站进行路由.
master_accept_reads=true
#默认读是不被路由到master的。允许将主服务器用于读取。如果您使用的服务器数量较少,并且希望也使用主服务器进行读取,则启用此选项非常有用
#weightby=server_weight

# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://mariadb.com/kb/en/mariadb-maxscale-23-maxadmin-admin-interface/

[MaxAdmin-Service]
type=service
router=cli

[Read-Only-Listener]
type=listener
service=Read-Only-Service
#protocol=MySQLClient
protocol=MariaDBClient
port=4008

[Read-Write-Listener]
type=listener
service=Read-Write-Service
#protocol=MySQLClient
protocol=MariaDBClient
port=4006

[MaxAdmin-Listener]
type=listener
service=MaxAdmin-Service
protocol=maxscaled
#socket=default
port=6603

四.提前配置好Mariadb 1主2从:

4.1配置基于gitd的主从复制

注意:在配置主从复制时,maxscale监控集群不识别主机名,只识别ip地址
所以在 CHANGE MASTER TO MASTER_HOST='172.16.0.130',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G 时,记得MASTER_HOST必须填写ip地址,
否则会导致监控主从复制集群失败

例如slave上:

CHANGE MASTER TO MASTER_HOST='mgr01',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G
[root@mgr01 ~]#  maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 172.16.0.131    |  3306 |           0 | Master, Slave of External Server, Running
server2            | 172.16.0.132    |  3306 |           0 | Slave of External Server, Running
server3            | 172.16.0.130    |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

Master, Slave of External Server, Running 像这样的代表监控主从复制集群关系失败
Slave of External Server, Running

例如slave上:


CHANGE MASTER TO MASTER_HOST='172.16.0.130',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G

[root@mgr01 ~]# maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 172.16.0.131    |  3306 |           0 | Slave, Running
server2            | 172.16.0.132    |  3306 |           0 | Slave, Running
server3            | 172.16.0.130    |  3306 |           0 | Master, Running
-------------------+-----------------+-------+-------------+--------------------

以上status代表正常

4.2 数据库账户创建和授权:

1.监控后端mysql服务的状态
2.复制集群操作(failover,switchover,rejoin,reset-replication)

要求监视用户具有以下特权:

create user 'maxmonuser'@'%' identified by 'maxmonpwd';
grant super, REPLICATION SLAVE,replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'%';

权限作用介绍:

SUPER, to modify slave connections and set globals such as read_only
REPLICATION CLIENT, to list slave connections
RELOAD, to flush binary logs
PROCESS, to check if the event_scheduler process is running
SHOW DATABASES and EVENT, to list and modify server events

授权注意事项:

grant super, replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'172.16.0.%';
MariaDB不支持上述方式的授权,只能采用下面方法授权。
grant super, replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'%';

五.读写分离配置和测试:

创建读写分离操作后端mysql服务的账户:

create user 'maxuser'@'172.16.0.%' identified by 'maxpwd';
grant SELECT on mysql.* to 'maxuser'@'172.16.0.%';
GRANT SHOW DATABASES,super  ON *.* TO 'maxuser'@'172.16.0.%';
select user,host from mysql.user;

创建测试用户:

grant all on *.* to 'maxscale'@'172.16.0.%' identified by 'maxscalepass'; flush privileges;
或者
grant all on *.* to 'maxscale'@'%' identified by 'maxscalepass'; flush privileges;

通过maxscale机器ip 和maxscale.cnf配置文件中指定的4006端口来登录进行读写库
mysql -umaxscale -p'maxscalepass' -h 172.16.0.130 -P4006

读写分离配置:
https://mariadb.com/kb/en/mariadb-maxscale-23-readwritesplit/#readwritesplit

读写分割路由决策:
下面的动作会被路由到master上:

write statements,
all statements within an open transaction,
stored procedure calls
user-defined function calls
DDL statements (DROP|CREATE|ALTER TABLE … etc.)
EXECUTE (prepared) statements that modify the database
all statements using temporary tables

如果用max_slave_replication_lag参数配置了readwritesplit服务,并且如果所有从属服务器都遭受过多的复制滞后,则语句将被路由到主服务器或者其他的slave上。
加入设定为:max_slave_replication_lag=4
#指定从站被允许落后于主站多少秒。如果滞后时间大于配置的值,则不能使用从站进行路由.
日志内容warning如下:

2020-02-28 21:52:59.040   info   : (4) Route query to master: server3   [172.16.0.130]:3306 <
2020-02-28 21:52:59.057   info   : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.059   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event 
2020-02-28 21:52:59.059   info   : (4) Route query to slave: server1    [172.16.0.131]:3306 <
2020-02-28 21:52:59.060   info   : (4) Reply complete, last reply from server1
2020-02-28 21:52:59.495   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now()) 
2020-02-28 21:52:59.495   info   : (4) Route query to master: server3   [172.16.0.130]:3306 <
2020-02-28 21:52:59.511   info   : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.513   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event 
2020-02-28 21:52:59.513   warning: (4) Replication lag of 'server1' is 48s, which is above the configured limit 4s. 'server1' is excluded from query routing.
2020-02-28 21:52:59.514   info   : (4) Route query to slave: server2    [172.16.0.132]:3306 <
2020-02-28 21:52:59.535   info   : (4) Reply complete, last reply from server2
2020-02-28 21:52:59.906   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now()) 
2020-02-28 21:52:59.908   info   : (4) Route query to master: server3   [172.16.0.130]:3306 <
2020-02-28 21:52:59.929   info   : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.935   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event 
2020-02-28 21:52:59.935   info   : (4) Route query to slave: server2    [172.16.0.132]:3306 <
2020-02-28 21:52:59.950   info   : (4) Reply complete, last reply from server2
2020-02-28 21:53:00.304   info   : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now()) 

被路由到slave上的情况:

可以路由到从站的查询必须是自动提交的,并且属于以下组之一
read-only database queries,
read-only queries to system, or user-defined variables,
SHOW statements
system function calls.

六.故障自动切换:

A: Failover故障自动转移将发生故障的主节点替换为正在运行的从节点。它执行以下操作:

6.1选择旧主机的最新从机作为新主机。选择标准按降序排列


1.gtid_IO_pos (latest event in relay log)
2.gtid_current_pos (most processed events)
3.log_slave_updates is on
4.disk space is not low

6.2如果新的主服务器具有未处理的中继日志项目,请取消并稍后重试

6.3 准备新的master

1.从旧的主服务器上删除新主服务器用来复制的从服务器连接。
2.关闭新master上的read_only参数
3.启用计划的服务器事件(如果启用了事件处理)。仅启用在旧主机上启用的事件
4运行promotion_sql_file中的命令
5.如果存在外部主服务器,则开始复制

6.4重定向所有其他从属服务器以从新的主服务器复制

STOP SLAVE and RESET SLAVE
CHANGE MASTER TO
START SLAVE

6.5检查所有从属是否正在复制
如果步骤6.1到6.3成功,则故障转移被视为成功,因为群集至少具有一个有效的主服务器

B: Switchover手动切换将正在运行的master与正在运行的slave交换。它执行以下操作:

1.准备降级的旧的master:

Stop any external replication.
Kill connections from super-users since read_only does not affect them.
Enable the read_only-flag to stop writes.
Disable scheduled server events (if event handling is on).
Run the commands in demotion_sql_file.
Flush the binary log (FLUSH LOGS) so that all events are on disk.

2.等待新的master赶上旧的master
3.像故障转移步骤3和4一样,升级新的主服务器并重定向从服务器。还要重定向已降级的旧主服务器
4.检查所有从属是否正在复制

提示:Failover/switchover requires MariaDB 10.0.2 or later.

七.自动重新加入到集群:

Rejoin将独立服务器加入群集,或重定向从非主服务器复制的从服务器。独立服务器通过以下方式加入:

Run the commands in demotion_sql_file.
Enable the read_only-flag.
Disable scheduled server events (if event handling is on).
Start replication: CHANGE MASTER TO and START SLAVE.

从错误的主服务器复制的服务器仅通过STOP SLAVE,RESET SLAVE,CHANGE MASTER TO和START SLAVE命令进行重定向

八.数据库账户权限:

提示数据库账户权限:

(root@'mgr03':mysql.sock)[(none)]>show grants for maxmonuser@'%';
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for maxmonuser@%                                                                                                                                  |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT RELOAD, SUPER, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO `maxmonuser`@`%` IDENTIFIED BY PASSWORD '*DE290C4A038E4E06BB0AF2C0B8B01ABB51572796' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

(root@'mgr03':mysql.sock)[(none)]>show grants for maxscale@'%';
+------------------------------------------------------------------------------------------------------------------+
| Grants for maxscale@%                                                                                            |
+------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `maxscale`@`%` IDENTIFIED BY PASSWORD '*E7ACDE28142BFC311A2CE191B3CB3E510A27EB60' |
+------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

(root@'mgr03':mysql.sock)[(none)]>show grants for maxuser@'172.16.0.%';
+---------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].%                                                                                                   |
+---------------------------------------------------------------------------------------------------------------------------------+
| GRANT SHOW DATABASES, SUPER ON *.* TO `maxuser`@`172.16.0.%` IDENTIFIED BY PASSWORD '*5EDBD32E469DAE0CE10E6999C3899DEFCB9F12E0' |
| GRANT SELECT ON `mysql`.* TO `maxuser`@`172.16.0.%`                                                                             |
+---------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

(root@'mgr03':mysql.sock)[(none)]>show grants for repuser@'172.16.0.%';
+-----------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].%                                                                                               |
+-----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `repuser`@`172.16.0.%` IDENTIFIED BY PASSWORD '*15BB449A1D5BDA166BB45F5989B2417834A13C23' |
+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

参考文档资料:
https://blog.51cto.com/linzhijian/1913794
http://udn.yyuap.com/thread-38241-1-1.html
https://downloads.mariadb.com/MaxScale/centos/7/x86_64/
https://blog.csdn.net/weixin_34194551/article/details/92492325
https://blog.csdn.net/thundermeng/article/details/83900503
https://www.jianshu.com/p/1ac435a6510e
http://www.ttlsa.com/mysql/maxscale-install-read-write-split/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK