一、概述

常见的高可用方案如MMM和MHA等都将重点放在主库上,一旦主库出现故障,通过这些方案能将主库故障进行转移。
本文将给大家介绍一款由mariadb公司出品的中间件Maxscale,该中间件能实现读写分离和读负载均衡,安装和配置都十分简单。
官方文档https://mariadb.com/kb/en/maxscale-22-getting-started/


二、节点介绍

本次实验采用4台虚拟机,操作系统版本Centos6.10,mysql版本5.7.25
maxscale 10.40.16.60  路由  路由节点
node1     10.40.16.61  主库  提供写服务
node2     10.40.16.62  从库  提供读服务
node3     10.40.16.63  从库  提供读服务

节点拓扑图
Mysql - 读写分离与读负载均衡之Maxscale


三、安装

1. 配置一主二从

其中node1是主库,node2和node3是从库。具体的复制搭建这里就省略,要是这都不会,那么该文章对你就没意思了。顺便安利一个自己写的mysql一键安装脚本https://www.cnblogs.com/ddzj01/p/10678296.html
注明:集群中使用的复制账号为repl,密码是'123456'


2. 下载maxscale包

下载地址:https://downloads.mariadb.com/MaxScale/2.2.0/centos/6Server/x86_64/
Mysql - 读写分离与读负载均衡之Maxscale
我在做实验的时候,最开始使用的是maxscale的最新版本(如:2.2.21-GA),安装完后发现参数文件/etc/maxscale.cnf里面都只支持mariadb(protocol=MariaDBBackend),而不支持oracle官方的mysql。所以就选用一个了比较老的maxscale版本。等实验做完了,我再试着用最新版本的maxscale软件+老的参数文件也是能够运行的。所以如果使用的oracle官方的mysql,要想使用最新版本的maxscale,则需要使用老版本的参数文件去替换新版本中的参数文件。


3. 安装maxscale

在maxscale节点
yum install -y libaio libaio-devel
rpm -ivh maxscale-2.2.0-1.centos.6.x86_64.rpm


四、配置

1. 在node1(主库)创建相关账号

监控账号,maxscale使用该账号监控集群状态。如果发现某个从服务器复制线程停掉了,那么就不向其转发请求了。
(root@localhost)[(none)]> grant replication slave, replication client on *.* to scalemon@'%' identified by '123456';

路由账号,maxscale使用该账号将不同的请求分发到不同的节点上。当客户端连接到maxscale这个节点上时,maxscale节点会使用该账号去查后端数据库,检查客户端登陆的用户是否有权限或密码是否正确等等。
(root@localhost)[(none)]> grant select on mysql.* to maxscale@'%' identified by '123456';


2. 在maxscale节点配置参数文件/etc/maxscale.cnf

# MaxScale documentation on GitHub:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Documentation-Contents.md

# Global parameters
#
# Complete list of configuration options:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Getting-Started/Configuration-Guide.md

[maxscale]
threads=1            # 线程数,一般与cpu核数相同

# Server definitions
#
# Set the address of the server to the network
# address of a MySQL server.
#

[server1]
type=server
address=10.40.16.61  # node1的ip
port=3306
protocol=MySQLBackend

[server2]
type=server
address=10.40.16.62  # node2的ip
port=3306
protocol=MySQLBackend

[server3]
type=server
address=10.40.16.63  # node3的ip
port=3306
protocol=MySQLBackend

# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MySQL Monitor documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Monitors/MySQL-Monitor.md

[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3  # 集群的所有server
user=scalemon                    # 监控账号
passwd=123456                    # 监控账号密码
monitor_interval=10000           # 监控的时间间隔,单位为毫秒

# Service definitions
#
# Service Definition for a read-only service and
# a read/write splitting service.
#

# ReadConnRoute documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadConnRoute.md

# [Read-Only Service]           # 读负载均衡模块,由于读写分离模块也能实现读负载均衡,因此注释掉该模块
# type=service
# router=readconnroute
# servers=server1
# user=myuser
# passwd=mypwd
# router_options=slave

# ReadWriteSplit documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadWriteSplit.md

[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3  # 集群的所有server
user=maxscale                    # 路由账号
passwd=123456                    # 路由账号密码
max_slave_connections=100%       # 多少比例的从服务器被使用,默认就是所有的从服务器都提供读服务

# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Reference/MaxAdmin.md

[MaxAdmin Service]
type=service
router=cli

# Listener definitions for the services
#
# These listeners represent the ports the
# services will listen on.
#

# [Read-Only Listener]           # 注释该模块
# type=listener
# service=Read-Only Service
# protocol=MySQLClient
# port=4008

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

[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
# socket=default                # 注释该socket
port=6603                       # 为maxadmin选择一个端口
View Code

相关文章: