zoukankan      html  css  js  c++  java
  • MaxScale初探

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://mrcto.blog.51cto.com/1923168/1437287

    内容预览:

    1、MaxScale简介

    2、MaxScale安装

    3、MaxScale配置

    4、MaxScale简单测试

    5、MaxScale Debug Interface和CLI

    6、MaxScale调试

    7、可用资源

    (文中图片普遍偏小,请点击看高清大图)


    1、MaxScale简介

        项目地址:https://github.com/skysql/MaxScale/

        MaxScale是由http://www.skysql.com/开发的一款MySQL数据库中间件。

    • MaxScale是什么(摘自Github/MaxScale):

    The SkySQL MaxScale is an intelligent proxy that allows forwarding of
    database statements to one or more database servers using complex rules,
    a semantic understanding of the database statements and the roles of
    the various servers within the backend cluster of databases.
    
    MaxScale is designed to provide load balancing and high availability
    functionality transparantly to the applications. In addition it provides
    a highly scalable and flexibile architecture, with plugin components to
    support different protocols and routing decissions.
    
    MaxScale is implemented in C and makes entensive use of the
    asynchronous I/O capabilities of the Linux operating system. The epoll
    system is used to provide the event driven framework for the input and
    output via sockets.

    简单的说就是:MaxScale是一个支持高可用,负载均衡,具有良好的可扩展性,高性能的基于事件驱动的同时具有代理和管理功能的中间件。

    • Architecture:

    如图所示,MaxScale由以下几个模块组成:

    Protocol,Router,Monitor,Authentication,Filter and logging.

    QQ20140711-8@2x

    MaxScale运行在clinet与server之间,起到一个”Exchange”的作用.

    QQ20140711-7@2x

    需要知道的是:client并不会直接与server建立连接,而是通过MaxScale来做连接创建和数据交换,MaxScale接收client的请求,经过(或不经过)Filter处理当前的Request,然后通过Router将client发送的request发送到后端server,server返回的数据经过(或不经过)处理之后再返回给client。MaxScale同时维护client请求的连接和到server的连接。

    Client与Server、MaxScale之间的关系如图:

    QQ20140711-6@2x

    1)Protocol:分为client protocol和back-end protocol.

    client目前尽支持MySQL以及MariaDB,

    back-end目前支持以下几种类型的后端集群:

    MySQL Replication
    MariaDB Replication
    Galera Cluster on both MySQL and MariaDB
    MySQL Server with NDB storage engine

    2)Router:Connection-based Routing和Statement-based Routing.

    Connection-based Routing:client连接到maxscale之后,会为该client维护一个路由,同一个连接会始终通过这条路由,并且不检查当前连接下的产生 的不同的请求,该路由一旦创建便会一直存在直到客户端连接断开。适合读请求和写请求使用不同的连接的场景。

    QQ20140711-1@2x

    Statement-based Routing:对同一个连接产生的不同的请求动态的路由到一个或多个后端的数据库做处理。此类型路由可使得应用程序端操作非常简单,读写分离以及负载均衡均对应用透明。

    QQ20140711-2@2x

    当MaxScale发现一个正在被客户端使用的连接在连接到后端server时发生错误导致连接失败的时候,MaxScale会自动重建一个到后端server的新的连接用来代替失效的连接,使得让客户端无感。

    3)Authentication:

    MaxScale 用从后端服务器获取到的user表作为客户端授权的依据,为了能验证用户连接,MaxScale从后端数据库中读取mysql.user表信息,并将此用 户信息缓存在MaxScale端,当用户连接进来会根据缓存的信息判断是否通过验证,如果当前缓存无此用户,会从后端数据库中更新用户信息,并作验证。

    QQ20140710-27@2x

    4)Filter & Logging:

    Filter可比作database-level firewall,它可用于对某些特殊statement进行过滤或者Rewrite。适用于简单类型的语句容错,以及语句的自动转换。

    Rewrite:

    QQ20140711-3@2x

    Fault Tolerance:

    QQ20140711-4@2x

    5)Monitor:

    用于对后端数据库池的实时监控,目前只支持MySQL master/slave replication和Galera cluster。


    2、MaxScale安装

    • 安装准备工作

    测试环境准备:

    CentOS 6.5 x86_64:172.16.238.128 Master MySQL-5.6.19

    CentOS 6.5 x86_64:172.16.238.129 Slave MySQL-5.6.19

    CentOS 6.5 x86_64:172.16.238.130 Slave MySQL-5.6.19

    CentOS 6.5 x86_64:172.16.238.131 Slave MySQL-5.6.19

    CentOS 6.5 x86_64:172.16.238.132 MaxScale

    配置好主从,并开启半同步。

    安装包下载地址:

    Binary package:https://downloads.skysql.com/files/SkySQL/MaxScale
    Source package:http://github.com/skysql/MaxScale

    • 从binary包安装

    1)解压:

    tar -zxvf maxscale.1.0.0-beta.tar.gz

    2)将解压的文件夹拷贝到安装目录即可:

    mv maxscale-1.0.0-beta /usr/local/maxscale

    3)maxscale程序启动需要读取两个环境变量MAXSCALE_HOME和LD_LIBRARY_PATH

    export MAXSCALE_HOME=/usr/local/maxscale

    export LD_LIBRARY_PATH=/usr/local/maxscale/lib

    如果需要永久生效还需将这两行添加到profile或者.bashrc

    4)拷贝MySQL配置文件 my.cnf 到 $MAXSCALE_HOME/mysql/.

    cp /etc/my.cnf /usr/local/maxscale/mysql/my.cnf

    5)拷贝默认配置文件

    cd /usr/local/maxscale/etc/

    cp MaxScale_template.cnf MaxScale.cnf

    默认配置使用了3000,3001,3002三个mysql监听端口,根据实际情况修改即可。

    6)启动maxscale

    MaxScale默认会根据$MAXSCALE_HOME变量从$MAXSCALE_HOME/etc目录中查找配置文件MaxScale.cnf。
    如果变量$MAXSCALE_HOME没有设置,则查找/etc/MaxScale.cnf文件。

    以下三种启动方式均可:

    export MAXSCALE_HOME=/usr/local/maxscale

    /usr/local/maxscale/bin/maxscale

    或者

    /usr/local/maxscale/bin/maxscale -c /usr/local/maxscale/

    或者

    /usr/local/maxscale/bin/maxscale -f /usr/local/maxscale/etc/MaxScale.cnf

    • 从源码安装

    作者原文:https://groups.google.com/forum/#!topic/maxscale/1NA_0Ni58X4

    下文根据实际情况有所修改:
    1)获取源码原文:
    我不用git工具下载,而是直接从Github获取源码zip包

    wget https://github.com/skysql/MaxScale/archive/master.zip -O MaxScale-src-1.0.zip

    2)配置MariaDB安装源(由于只用于编译maxscale故不用安装MariaDB-server)

    vi /etc/yum.repos.d/mariadb.repo

    写入 :
    [mariadb]
    name = MariaDB
    baseurl = http://yum.mariadb.org/5.5/centos6-amd64
    gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
    gpgcheck=1

    保存退出。

    3)安装MariaDB-devel

    (请直接安装mariadb-devel,没必要尝试mysql-devel,因为,我尝试过了,原因,你试试就知道)

    yum install MariaDB-devel

    如果网络不给力就直接下载rpm包(比如我)
    wget https://downloads.skysql.com/files/MariaDB/yum/5.5/centos6-amd64/rpms/MariaDB-5.5.38-centos6-x86_64-devel.rpm
    如果安装过程中发现提示冲突,那是因为已有mysql相关的包,请直接卸载即可rpm -e 或者yum remove。

    4)安装编译MaxScale所需必要依赖:
    yum -y install make gcc gcc-c++ ncurses-devel bison glibc-devel openssl-devel libaio-devel telnet

    5)修改源码包中的配置文件

    unzip MaxScale-src-1.0.zip
    cd /root/MaxScale-master/

    vi 修改根目录下 build_gateway.inc 文件中以下几项

    ROOT_PATH := $(HOME)/MaxScale-master (此处需注意,$HOME指的是当前用户环境变量HOME,我用root运行,解压后文件夹为/root/MaxScale-master,如果是其他位置直接用绝对路径)

    INC_PATH := /usr/include

    MYSQL_ROOT := $(INC_PATH)/mysql

    EMBEDDED_LIB := /usr/lib64  (mariadb-devel安装后libmysqld.a在这个目录下)

    ERRMSG := /usr/share/mysql/english

    保存退出

    6)开始编译

    make depend
    make all
    make DEST=/usr/local/maxscale/ install

    7)拷贝启动脚本和配置文件

    cp etc/init.d/maxscale /etc/init.d/

    cp etc/MaxScale_template.cnf /usr/local/maxscale/etc/MaxScale.cnf

    8)maxscale程序启动需要读取两个环境变量MAXSCALE_HOME和LD_LIBRARY_PATH

    export MAXSCALE_HOME=/usr/local/maxscale

    export LD_LIBRARY_PATH=/usr/local/maxscale/lib

    如果需要永久生效还需将这两行添加到profile或者.bashrc

    9)这时启动maxscale和binary安装方式类似,也可以使用service maxscale start启动

    可以任选一种方式安装使用,安装完成后各个文件位置如下图:

    QQ20140710-16@2x

    需要注意的就是etc目录下的passwd文件是在debug interface创建用户之后自动生成的。


    3、MaxScale配置和启动

    针对各项配置详细解读请看安装包里面的文档MaxScale Configuration And Usage Scenarios.pdf

    配置文件主要结构如下图所示:

    QQ20140710-24@2x

    1)详细配置如下:

    配置文件为/usr/local/maxscale/etc/MaxScale.cnf

    grep -v "^#" /usr/local/maxscale/etc/MaxScale.cnf | grep -v "^$"
     [maxscale]
     threads=1
    [MySQL Monitor]
     type=monitor
     module=mysqlmon
     servers=server1,server2,server3,server4
     user=maxmonuser
     passwd=C891A19D7EFAF3B59AF7948E5AE3B998
    [RW Split Router]
     type=service
     router=readwritesplit
     servers=server1,server2,server3,server4
     max_slave_replication_lag=5
     user=maxuser
     passwd=A71E58147EBD73986D3D074E80F00DB9
    [Read Connection Router]
     type=service
     router=readconnroute
     router_options=slave
     servers=server1,server2,server3,server3
     user=maxuser
     passwd=A71E58147EBD73986D3D074E80F00DB9
    [HTTPD Router]
     type=service
     router=testroute
     servers=server1,server2,server3,server4
    [Debug Interface]
     type=service
     router=debugcli
     servers=server1
    [CLI]
     type=service
     router=cli
    [RW Split Listener]
     type=listener
     service=RW Split Router
     protocol=MySQLClient
     port=4006
     enable_root_user=1
    [Read Connection Listener]
     type=listener
     service=Read Connection Router
     protocol=MySQLClient
     port=4008
     enable_root_user=1
    [Debug Listener]
     type=listener
     service=Debug Interface
     protocol=telnetd
     port=4442
     address=127.0.0.1
    [HTTPD Listener]
     type=listener
     service=HTTPD Router
     protocol=HTTPD
     port=6444
    [CLI Listener]
     type=listener
     service=CLI
     protocol=maxscaled
     address=127.0.0.1
     port=6603
    [server1]
     type=server
     address=172.16.238.128
     port=3306
     protocol=MySQLBackend
    [server2]
     type=server
     address=172.16.238.129
     port=3306
     protocol=MySQLBackend
    [server3]
     type=server
     address=172.16.238.130
     port=3306
     protocol=MySQLBackend
    [server4]
     type=server
     address=172.16.238.131
     port=3306
     protocol=MySQLBackend

    2)配置里面用到的user需要在后端的各个mysql server里面建立用户并授权。

    create user 'maxuser'@'172.16.238.132' identified by 'maxpwd';
     grant SELECT on mysql.user to 'maxuser'@'172.16.238.132';

    maxmonuser用于监控后端服务器状态

    create user 'maxmonuser'@'172.16.238.132' identified by 'maxmonpwd';
     grant REPLICATION SLAVE on *.* to 'maxmonuser'@'172.16.238.132';
     grant REPLICATION CLIENT on *.* to 'maxmonuser'@'172.16.238.132';

    另 外新建一个maxscale用于测试,此用户不在配置文件中,且尽量不用root用户,因为maxscale默认不允许root连接后端数据库,需要在管 理界面enable root “some service” 才能开放root登录,目前maxscale还不支持类似172.16.238.%以%作模糊匹配的主机地址,会报错

    create user 'maxscale'@'%' identified by 'maxscalepass';
     grant ALL PRIVILEGES on *.* to 'maxscale'@'172.16.238.132';

    为了使得配置文件中不以明文方式存放密码,maxscale自带了加密工具用于获取加密后的字符串,maxscale在读取加密字符串之后会自动解密。

    进入/usr/local/maxscale/bin/目录

    ./maxkeys $MAXSCALE_HOME/etc/.secrets
    加密maxuser密码
     ./maxpasswd maxpwd
     A71E58147EBD73986D3D074E80F00DB9
    加密maxmonuser密码
     ./maxpasswd maxmonpwd
     C891A19D7EFAF3B59AF7948E5AE3B998

    至此,MaxScale配置完成,现在启动MaxScale。

    3)启动MaxScale

    [root@db132 bin]# /usr/local/maxscale/bin/maxscale

    SkySQL MaxScale 0.7.0 Mon Jun 30 23:03:54 2014
    ——————————————————
    Info : MaxScale will be run in a daemon process.
    See the log from the following log files :

    Error log : /usr/local/maxscale/log/skygw_err1.log
    Message log : /usr/local/maxscale/log/skygw_msg1.log
    Trace log : /usr/local/maxscale/log/skygw_trace1.log
    Debug log : /usr/local/maxscale/log/skygw_debug1.log

    Listening MySQL connections at 0.0.0.0:4006
    Listening MySQL connections at 0.0.0.0:4008
    Listening http connections at 0.0.0.0:6444
    Listening telnet connections at 127.0.0.1:4442

    或直接启动服务

    [root@db132 ~]# service maxscale start
    Starting MaxScale: maxscale (pid 54649) is running… [ OK ]

    启动服务后,观察启动日志:

    QQ20140710-7@2x

    1.0beta版还会加载libmaxscaled.so模块。


    4、MaxScale简单测试

    1)连接测试,通过maxscale监听的端口4006或者4008连接后端数据库,默认会route到master

    [root@db132 bin]# mysql -umaxscale -pmaxscalepass -h172.16.238.132 -P4006
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 6385
    Server version: 5.5.35-MariaDB MySQL Community Server (GPL)

    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

    mysql>

    准备工作:

    创建一个库d,并简单的表t,并在每个server中开启general log。

    2)测试查询

    mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 
     --create-schema=d --query=select.sql 
     --concurrency=6 --number-of-queries 1 --iterations=1 --debug-info
    查询语句:
     cat select.sql
     select * from t;
     select * from t;
     select * from t;

    执行结果:

    QQ20140710-1@2x

    可以看到查询被均匀的路由到三个从库。

    3)测试插入

    mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 
     --create-schema=d --query=insert.sql 
     --concurrency=1 --number-of-queries 4 --iterations=1 --debug-info
    cat insert.sql
     insert into t values(20,'x');
     insert into t values(30,'xx');
     insert into t values(40,'xxx');
     insert into t values(50,'xxxx');

    执行结果:

    QQ20140710-3@2x

    QQ20140710-4@2x

    可以看到,主库接收了所有写操作,并且全部同步到了从库,从库可看到更新操作记录。

    4)测试更新

    mysqlslap -umaxscale -pmaxscalepass -h172.16.238.132 -P4006 
     --create-schema=d --query=update.sql 
     --concurrency=1 --number-of-queries 1 --iterations=1 --debug-info
    cat update.sql
     update t set str='yy' where id=2;

    执行结果:

    QQ20140710-5@2x

    同样,更新操作的操作记录跟插入类似,主库上执行,然后同步到从库。

    5)测试事务

    [root@db129 mysql]# mysql -umaxscale -pmaxscalepass -h172.16.238.132 -P4006

    测试两次事务操作,一次提交,一次不提交。

    mysql> begin;
     mysql> select * from t where id=1 for update;
     mysql> update t set str='zz' where id=1;
     mysql> q
    mysql> begin;
     mysql> select * from t where id=1 for update;
     mysql> update t set str='zz' where id=1;
     mysql> commit;
     mysql> q

    执行结果:

    QQ20140710-6@2x

    可以看到,第一次开启事务,执行操作,不提交,在主库上看到更新操作记录,从库无记录,第二次开始事务并提交,从库会更新生效的一条记录。

    读写测试基本完成,目前未发现任何问题,还需在物理机上测试看看高并发下的处理能力。

    maxscale对每个后端server作定期检查,检查的时间间隔由参数monitor_interval控制,默认10000ms。

    QQ20140710-8@2x

    MaxScale会根据语句类型作路由,在trace日志中可以看到如下记录:

    QQ20140710-9@2x

     


    5、MaxScale Debug Interface 和 CLI

     前面的配置中可以看到,定义了一个Debug Interface和一个CLI service,MaxScale可以通过telnet连接到Debug Interface进行一系列的管理操作,同时也可以通过CLI做相关操作。

    • Debug Interface

    1)使用telnet登录管理界面:

    #telnet 127.0.0.1 4442

    默认用户名是admin,密码是skysql

    可以使用add user username password 覆盖默认的用户名密码

    使用show users可以看到当前的所有用户

    QQ20140710-10@2x

    2)控制monitor

    monitor可以定义多个,如果需要停用monitor可以使用shutdown命令。

    show,shutdown,restart monitor

    QQ20140710-11@2x

    3)控制service

    maxscale可以同时开启多个Router并监听端口,如果需要关闭其中某个类型的router,可以使用shutdown命令,重新启用使用restart命令。(下图重启失败,应该是bug,尝试多次)

    show,shutdown,restart service

    QQ20140710-12@2x

    4)控制server

    如果需要临时对某个主机进行维护,可以设置server为maintenance状态,maxscale将不会再派发请求到该server,维护完成后,清楚此状态即可。

    show,set,clear server

    QQ20140710-13@2x

    5)控制log

    enable启用

    disable停用

    MaxScale> enable log debug
    MaxScale> enable log trace

    6)停机维护

    对于service down的mysql服务器,可以手动设置维护模式,以不影响其他server,但是mascale会不停的探测该server是否重新运行。

    QQ20140710-14@2x

    server down之后master会发出如下两种提示,设置maintenance之后err信息将停止。

    #msg
     Jul 8 23:06:02 db132 MaxScale[53927]: Backend server 172.16.238.131:3306 state : NO STATUS
    #err
     Jul 8 23:06:12 db132 MaxScale[53927]: Error : Monitor was unable to connect to server 172.16.238.131:3306 : "Can't connect to MySQL server on '172.16.238.131' (111)"

    其他命令可自由发挥。

    • CLI(1.0beta)

    CLI模式即可以在shell中直接使用maxadmin工具查看maxscale的状态,需要配置CLI 以及 CLI Listener两个Section。

    QQ20140710-15@2x

    QQ20140710-17@2x

    可能你要问这个工具跟debug interface有什么区别?

    区别就在于跟交互式的debug interface相比,maxadmin可以用于监控任何一个需要监控的对象,servers,service,dcbs,sessions,users等等。你可以把要检查的内容列出来像写shell脚本那样写入脚本中一起执行。


    6、MaxScale调试模式

    maxscale支持debug模式

    #gdb maxscale

    (gdb) run --d

    QQ20140710-18@2x

    此时ctl+c即可进入调试模式,可以调用一些function。

    call printAllServers()

    QQ20140710-19@2x

    call printAllSessions()

    QQ20140710-20@2x

    call printServer(xxx)和call printSession(xxx)

    QQ20140710-21@2x

    结果跟debug interface和maxadmin输出结果一样。

    MaxScale里面还有一个很重要的概念,那就是DCB(Descriptor Control Block )

    QQ20140710-22@2x

    一 个DCB(Descriptor Control Block)表示一个在MaxScale内部的连接的状态,每个来自client的连接、到后端server的连接、以及每个listener都会被分配 一个DCB,这些连接的状态统计由这些DCB来完成。每个DCB并不会有特定的名字用于查询,而是直接使用具有唯一性的内存地址。

    一个DCB的生命周期:

    QQ20140711-9@2x

    同样可以通过Debug Interface查看DCB相关信息:

    QQ20140710-23@2x

    压测的时候可以看到DCB状态变化:

    QQ20140711-10@2x


    7、可用资源

    几个非常直观的介绍:

    http://www.slideshare.net/izoratti/140116-max-scale-for-effectivemysql
    http://www.slideshare.net/skysql/maxscale-the-pluggibale-router-mariadb-roadshow-2014-paris
    http://www.slideshare.net/skysql/maxscale-the-pluggable-router-36239961

    http://files.meetup.com/107604/MaxScale_Overview_DBA_Hangout20140122.pdf
    https://mariadb.com/resources/maxscale(Demo)

    较老版本的一个安装配置文档:

    http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-1

    http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-2

    http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-3

    http://www.skysql.com/blogs/anderskarlsson/maxscale-rest-us-part-4

    Google Group:https://groups.google.com/forum/#!forum/maxscale

    Bug Report:http://bugs.skysql.com/enter_bug.cgi?product=Maxscale

    以及安装包里自带的非常有用的几个Document:

    Documentation/
    ├── Debug And Diagnostic Support.pdf
    ├── filters
    │   ├── QLA Filter.pdf
    │   ├── Regex Filter.pdf
    │   ├── Tee Filter.pdf
    │   └── Top Filter.pdf
    ├── history
    │   ├── MaxScale 0.5 Release Notes.pdf
    │   ├── MaxScale 0.6 Release Notes.pdf
    │   └── MaxScale 0.7 Release Notes.pdf
    ├── internal
    │   └── DCB states.pdf
    ├── MaxAdmin The MaxScale Administration And Monitoring Client.pdf
    ├── MaxScale 1.0beta Release Notes.pdf
    ├── MaxScale Configuration And Usage Scenarios.pdf
    └── MaxScale HA with Corosync and Pacemaker.pdf


    Coming Soon:

    • MaxScale结合MHA

    • MaxScale结合Pacemaker+Corosync

    • 更详细的测试(Filter)

    • 压力测试

    以上内容完全基于本人理解得来,如有理解有误的地方还请指出,以便及时改正。

    部分bug已通过Google Group反馈给该软件的作者,当前的各个版本,包括最新版1.0beta版仍存在很多运行不顺畅的地方,所以遇到问题,要从多方面去考虑,去排查,如果能确定是bug,直接反馈就可以了。

    本文出自 “张跃的部落格” 博客,请务必保留此出处http://mrcto.blog.51cto.com/1923168/1437287

  • 相关阅读:
    VirtualBox 创建com对象失败
    大数据(十)
    HITCON 2014 已開始征求投稿计划书
    CSS
    工具
    工具
    Linux
    Python
    JavaScript
    JavaScript
  • 原文地址:https://www.cnblogs.com/seasonzone/p/3897655.html
Copyright © 2011-2022 走看看