使用Maxscale实现mysql读写分离

MaxScale 是 MariaDB 的产品之一,可以很方便的实现读写分离方案;并且提供了读写分离的负载均衡和高可用性保障。

10年积累的成都网站制作、网站建设、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有宜宾免费网站建设让你可以放心的选择与我们合作。

一、安装配置

前提:先配置好MySQL的主从,Maxscale会自动的根据主从复制信息来判断Master和slave

注:不能配置mysql互为主从,如果互为主从,两台都会被认为slave,在这种情况下,写请求会被拒绝,只接受读请求。

1.1.安装

官网下载对应的rpm包,

wget  https://downloads.mariadb.com/MaxScale/2.0.3/centos/6server/x86_64/maxscale-2.0.3-1.centos.6.x86_64.rpm
rpm -ivh maxscale-2.0.3-1.centos.6.x86_64.rpm

maxscale的配置文件为:/etc/maxscale.conf,配置文件由多个配置模块组成

vi /etc/maxscale.conf

#全局配置:
[maxscale]
threads=auto
log_info=1
log_notice=1
log_warning=1

#后端mysql定义
[server1]
type=server
address=192.168.10.1
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.10.2
port=3306
protocol=MySQLBackend

#监控配置
[MySQL Monitor] 
type=monitor
module=mysqlmon      #监控模块默认使用mysqlmon,当然还有其他可选择的模块
servers=server1,server2
user=admin        #admin监控后端mysql的复制状况,必须具有REPLICATION CLIENT权限      
passwd=123456
monitor_interval=10000

#触发器定义 注意,以下两句的上下顺序不能变,不然无法生效
script=/opt/mysql_monitor.sh  #定义事件触发脚本执行
events=master_down       #事件触发器当master down时,执行上面的脚本,maxscale内置了很
                                 多的events
#读写分离service
[Read-Write Service] 
type=service
router=readwritesplit       #读写分离路由模式
servers=server1,server2
user=maxscale    #该用户从后端mysql获取用户信息,对客户端进行身份验证,必须具有mysql.user table的select权限                
passwd=maxscale
max_slave_connections=100%                   
max_slave_replication_lag=3600000   #当slave的数据落后master小于3600秒时仍然可用
connection_timeout=300                          
router_options=master_failure_mode=error_on_write #允许master down掉,slave仍然可读
#router_options=master_accept_reads=true      #允许master接受读请求

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

#管理服务配置 
[MaxAdmin Service]
type=service
router=cli

#管理服务监听
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default

二、管理

Maxscale提供了maxadmin命令用于查看管理

[root@server ~]# maxadmin  list -
Unknown or missing option for the list command. Valid sub-commands are:
    clients    List all the client connections to MaxScale
    dcbs       List all the DCBs active within MaxScale
    filters    List all the filters defined within MaxScale
    listeners  List all the listeners defined within MaxScale
    modules    List all currently loaded modules
    monitors   List all monitors
    services   List all the services defined within MaxScale
    servers    List all the servers defined within MaxScale
    sessions   List all the active sessions within MaxScale
    threads    List the status of the polling threads in MaxScale
[root@server ~]# maxadmin list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.10.1      |  3306 |           0 | Master, Running
server2            | 192.168.10.2     |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

三、高可用性

Maxscale默认只提供读的高可用性,要实现写的高可用性,可以使用两种途径:

1.需要使用Multi-MasterMonitor监控模块,不同于上文使用的mysqlmon模块,该模块是通过read_only参数来选举Master和Slave,结合脚本可以实现在Master fail的时候,取消slave的read_only属性,将slave提升为Master

2.使用高可用软件MMM

当前题目:使用Maxscale实现mysql读写分离
网址分享:https://www.cdcxhl.com/article42/igegec.html

成都网站建设公司_创新互联,为您提供Google软件开发企业建站网站策划电子商务网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设