-

MariaDB MaxScale读写分离

MySQL

MaxScale是MariaDB开发的一个数据库智能代理服务,一直看好的开源数据库MariaDB的又一利器。MaxScale允许根据数据库 SQL 语句将请求转向目标一个到多个服务器,可设定各种复杂程度的转向规则。MaxScale 设计用于透明的提供数据库的负载均衡和高可用性,同时提供高度可伸缩和灵活的架构,支持不同的协议和路由决策。简单的讲就是使用MaxScale可以轻松解决mariaDB读写分离的问题。下面雷雪松就详细的带大家一起看看MariaDB MaxScale实现读写分离的具体步骤。

MaxScale有五类插件:身份验证(Authentication)、协议(Protocol)、监控(Monitoring)、路由器(Router)、过滤(Filter),他们彼此相互配合一起工作来实现读写分离的。Protocol负责接口或从外部系统和maxscale协议方面,Monitoring来确定系统内的服务器发送请求的当前状态,Router则基于路由算法、服务器路由权重、服务器状态、可选的查询类型(读与写)和数据库模式的查询进行路由选择请求发送到后端哪里。Filter则可以自己编写一些小程序检查和修改SQL请求。通过一张图来让我们看一下MaxScale的工作原理。
MaxScaleHowWorks

MaxScale安装和配置:
MariaDB master为192.168.1.150
MariaDB slave为192.168.1.145
MariaDB的版本是10.1.16
[cc lang=”bash” ][root@MariaDB soft]# rpm -ivh configure-maxscale-repo-0.1.2.rpm
Preparing… ########################################### [100%]
1:configure-maxscale-repo########################################### [100%]
[root@MariaDB soft]# yum install maxscale[/cc]
#拷贝配置文件
[cc lang=”bash” ][root@MariaDB etc]# pwd
/usr/local/mariadb-maxscale/etc
[root@MariaDB etc]# cp MaxScale_template.cnf maxscale.cnf
#定义MAXSCALE_HOME环境变量,或是加到/etc/profile文件中
[root@MariaDB etc]# export MAXSCALE_HOME=/usr/local/mariadb-maxscale[/cc]
[cc lang=”bash” ][root@MariaDB etc]# vi maxscale.cnf
#编辑配置文件maxscale.cnf
#定义读写路由器
[RW Split Router]
type=service
router=readwritesplit
servers=server2,server1
user=root
passwd=50514A05CE2C0909BA630B29A29D620D
enable_root_user=1
#use_sql_variables_in=
#max_slave_connections=100%
#max_slave_replication_lag=21
#router_options=slave_selection_criteria=
#filters=fetch|qla
#为该路由器定义一个listener
[RW Split Listener]
type=listener
service=RW Split Router
protocol=MySQLClient
port=4007
#socket=/tmp/rwsplit.sock

#定义服务器成员
[server1]
type=server
address=192.168.1.145
port=3307
protocol=MySQLBackend
[server2]
type=server
address=192.168.1.150
port=3306
protocol=MySQLBackend[/cc]

password是一个加密的密文,maxscal使用该用户连接数据库
#生成密码文件,再加密
[cc lang=”bash” ][root@MariaDB bin]# /usr/local/mariadb-maxscale/bin/maxkeys /usr/local/mariadb-maxscale/etc/.secrets
[root@MariaDB bin]# /usr/local/mariadb-maxscale/bin/maxpasswd root
50514A05CE2C0909BA630B29A29D620D[/cc]
#启动maxscale,并以后台方式运行,查看端口是否正常打开,4007是连接 MaxScale 时使用的端口,6603是 MaxScale 管理器的端口,4442是MaxScale debug测试的端口
[cc lang=”bash” ][root@MariaDB bin]# /usr/local/mariadb-maxscale/bin/maxscale –config=/usr/local/mariadb-maxscale/etc/maxscale.cnf
[root@MariaDB etc]# netstat -nltp | grep max
tcp 0 0 0.0.0.0:4007 0.0.0.0:* LISTEN 15153/maxscale
tcp 0 0 0.0.0.0:6603 0.0.0.0:* LISTEN 15153/maxscale
tcp 0 0 0.0.0.0:4442 0.0.0.0:* LISTEN 15153/maxscale[/cc]

#使用管理工具连接,设置服务器状态
[cc lang=”bash” ][root@MariaDB bin]# /usr/local/mariadb-maxscale/bin/maxadmin –user=admin –password=mariadb –host=127.0.0.1
#手动设定手服务器的状态
MaxScale> set server server1 slave
MaxScale> set server server2 master
MaxScale> list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.1.145 | 3307 | 0 | Slave, Running
server2 | 192.168.1.150 | 3306 | 0 | Master, Running
——————-+—————–+——-+————-+——————–[/cc]
这样就配置成功了可以实现读写分离,可以写一个select查询和update更新测试。

来源:MariaDB MaxScale读写分离

7 评论 “MariaDB MaxScale读写分离

    贝蒂斯初榨橄榄油 评论:
    2016年8月30日 下午6:17

    很不错哦,还占了个沙发哈哈哈~~·

    365免单网 评论:
    2016年9月1日 下午2:21

    好深奥的感觉 。

    小小温母婴店 评论:
    2016年9月1日 下午3:03

    布吉岛。。。。

    老姚 评论:
    2016年9月1日 下午6:37

    博主,程序员呵,

    webkv 评论:
    2018年8月28日 下午3:27

    正在研究这个,不过很深奥。

    ultius 评论:
    2018年12月24日 上午1:27

    你如何编码,因为这部分对我来说真的很难。我尝试了很多,但我面临着错误。

    okkeep 评论:
    2019年5月5日 下午8:25

    哇塞,居然是沙发?留个名

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注