使用Amoeba 实现MySQL DB 读写分离

系统 1896 0

Amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 Amoeba for Mysql软件;

这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发;位于 Client、DB Server(s)之间,对客户端透明;

===================================================================

1 简介

2 准备

   2.1 时间同步

   2.2 配置MySQL主从复制架构

3 ameoba安装配置

   3.1 安装配置JDK

   3.2 安装ameoba

   3.3 配置ameoba

   3.4 使用验证

   3.5 后期扩展

4 问题记录

===================================================================

 

1 简介

Amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 Amoeba for Mysql软件;

这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发;位于 Client、DB Server(s)之间,对客户端透明;

具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的请求到目标数据库、可并发请求多台数据库并合并结果;

通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前Amoeba已在很多企业的生产线上面使用;

 

2 准备

2.1 时间同步

            # crontab -
            
              e

# Dscrip: Time Sync

# CTime: 
            
            
              2014.03
            
            .
            
              23
            
            

*/
            
              5
            
             * * * * /usr/sbin/ntpdate 
            
              172.16
            
            .
            
              0.1
            
             &>/dev/
            
              null
            
          

2.2 配置MySQL主从复制架构

详见博文" MariaDB 主从复制 "

 

3 ameoba安装配置

3.1 安装配置JDK

            
              chmod
            
             +x jdk-6u31-linux-x64-
            
              rpm.bin


            
            
              vi
            
             /etc/profile.d/java.
            
              sh
            
            
               # 采用bin文件安装jdk

    export JAVA_HOME
            
            =/usr/java/
            
              latest

    export PATH
            
            =$JAVA_HOME/bin:$PATH
          

3.2 安装ameoba

        
          mkdir
        
         /usr/local/
        
          amoeba


        
        
          tar
        
         xf amoeba-mysql-binary-
        
          2.2
        
        .
        
          0
        
        .
        
          tar
        
        .gz -C /usr/local/
        
          amoeba # 使用二进制程序文件安装amoeba

cd 
        
        /usr/local/
        
          amoeba

bin
        
        /
        
          amoeba start # 前台运行

nohup 
        
        /usr/local/amoeba/bin/amoeba start &
        
           # 后台运行

mysql 
        
        -h127.
        
          0.0
        
        .
        
          1
        
         -uroot -p -P8066 # amoeba默认监听端口为8066
      

3.3 配置ameoba

        cd /usr/local/amoeba/
        
          conf


        
        
          vi
        
        
           ameoba.xml # 前端定义配置文件

# 修改ameoba前端监听端口


        
        <service name=
        
          "
        
        
          Amoeba for Mysql
        
        
          "
        
         class=
        
          "
        
        
          com.meidusa.amoeba.net.ServerableConnectionManager
        
        
          "
        
        >

    <property name=
        
          "
        
        
          port
        
        
          "
        
        >
        
          3306
        
        </property>
        
           # 默认端口是8066,修改为3306,便于实现前端程序连接数据库的透明性

# 修改连接amoeba接口的认证信息


        
        <property name=
        
          "
        
        
          authenticator
        
        
          "
        
        >

    <bean class=
        
          "
        
        
          com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator
        
        
          "
        
        >   

        <property name=
        
          "
        
        
          user
        
        
          "
        
        >root</property>   

        <property name=
        
          "
        
        
          password
        
        
          "
        
        >mypass</property>
        
           # 添加登录密码

# 查询路由设置


        
        <queryRouter class=
        
          "
        
        
          com.meidusa.amoeba.mysql.parser.MysqlQueryRouter
        
        
          "
        
        >

    <property name=
        
          "
        
        
          ruleLoader
        
        
          "
        
        >       

        <bean class=
        
          "
        
        
          com.meidusa.amoeba.route.TableRuleFileLoader
        
        
          "
        
        >

            <property name=
        
          "
        
        
          ruleFile
        
        
          "
        
        >${amoeba.home}/conf/rule.xml</property>

            <property name=
        
          "
        
        
          functionFile
        
        
          "
        
        >${amoeba.home}/conf/ruleFunctionMap.xml</property>

        </bean>

    </property>

    <property name=
        
          "
        
        
          sqlFunctionFile
        
        
          "
        
        >${amoeba.home}/conf/functionMap.xml</property>

    <property name=
        
          "
        
        
          LRUMapSize
        
        
          "
        
        >
        
          1500
        
        </property>

    <property name=
        
          "
        
        
          defaultPool
        
        
          "
        
        >master</property>
        
           # 设定默认节点

    
        
        <property name=
        
          "
        
        
          writePool
        
        
          "
        
        >master</property>
        
              # 设定可写节点,节点定义见dbServers.xml文件

    
        
        <property name=
        
          "
        
        
          readPool
        
        
          "
        
        >readservers</property>
        
           # 设定只读池,可配置多个slave节点

    
        
        <property name=
        
          "
        
        
          needParse
        
        
          "
        
        >
        
          true
        
        </property>

</queryRouter>


        
          vi
        
        
           dbServers.xml # 后端节点配置文件

# 定义抽象服务器,为每个后端MySQL服务器提供默认连接配置


        
        <dbServer name=
        
          "
        
        
          abstractServer
        
        
          "
        
         abstractive=
        
          "
        
        
          true
        
        
          "
        
        >

    <factoryConfig class=
        
          "
        
        
          com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory
        
        
          "
        
        >

        <property name=
        
          "
        
        
          manager
        
        
          "
        
        >${defaultManager}</property>

        <property name=
        
          "
        
        
          sendBufferSize
        
        
          "
        
        >
        
          64
        
        </property>

        <property name=
        
          "
        
        
          receiveBufferSize
        
        
          "
        
        >
        
          128
        
        </property>

        <property name=
        
          "
        
        
          port
        
        
          "
        
        >
        
          3406
        
        </property>

        <property name=
        
          "
        
        
          schema
        
        
          "
        
        >test</property>

        <property name=
        
          "
        
        
          user
        
        
          "
        
        >root</property>

        
        
          
            <property name="password">
            
              magedu
            
            </property>
          
        
        

    </factoryConfig>
        
          

# 定义后端MySQL的IP地址,一个master,一个slave


        
        <dbServer name=
        
          "
        
        
          master
        
        
          "
        
          parent=
        
          "
        
        
          abstractServer
        
        
          "
        
        >

    <factoryConfig>

        <property name=
        
          "
        
        
          ipAddress
        
        
          "
        
        >
        
          192.168
        
        .
        
          0.45
        
        </property>

    </factoryConfig>

</dbServer>

<dbServer name=
        
          "
        
        
          slave
        
        
          "
        
          parent=
        
          "
        
        
          abstractServer
        
        
          "
        
        >

    <factoryConfig>

        <property name=
        
          "
        
        
          ipAddress
        
        
          "
        
        >
        
          192.168
        
        .
        
          0.46
        
        </property>

    </factoryConfig>

</dbServer>
        
          

# 定义虚拟服务器组,即只读池readservers


        
        <dbServer name=
        
          "
        
        
          readservers
        
        
          "
        
         virtual=
        
          "
        
        
          true
        
        
          "
        
        >

    <poolConfig class=
        
          "
        
        
          com.meidusa.amoeba.server.MultipleServerPool
        
        
          "
        
        >

        <property name=
        
          "
        
        
          loadbalance
        
        
          "
        
        >
        
          1
        
        </property>

        <property name=
        
          "
        
        
          poolNames
        
        
          "
        
        >master,slave</property>

    </poolConfig>

</dbServer>
      
 

3.4 使用验证

在主库上授权:

        MariaDB [(none)]> grant all on *.* to
        
          '
        
        
          root
        
        
          '
        
        @
        
          '
        
        
          172.16.%.%
        
        
          '
        
         identified by 
        
          '
        
        
          magedu
        
        
          '
        
        
          ;

Query OK, 
        
        
          0
        
         rows affected (
        
          0.00
        
        
           sec)

MariaDB [(none)]
        
        > grant all on *.* to
        
          '
        
        
          root
        
        
          '
        
        @
        
          '
        
        
          %mysql.com
        
        
          '
        
         identified by 
        
          '
        
        
          magedu
        
        
          '
        
        
          ; # 这里的密码应该与dbServer.xml中的数据库密码一致

Query OK, 
        
        
          0
        
         rows affected (
        
          0.00
        
        
           sec)



MariaDB [(none)]
        
        >
        
           flush privileges;

Query OK, 
        
        
          0
        
         rows affected (
        
          0.00
        
         sec)
      
        
          # 登录验证

[root@mysql conf]# mysql 
        
        -h127.
        
          0.0
        
        .
        
          1
        
         -uroot -p -
        
          P3306

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection 
        
        
          id
        
         is 
        
          2097086015
        
        
          

Server version: 
        
        
          5.1
        
        .
        
          45
        
        -mysql-amoeba-proxy-
        
          2.2
        
        .
        
          0
        
        
           Source distribution

Copyright (c) 
        
        
          2000
        
        , 
        
          2014
        
        
          , Oracle, SkySQL Ab and others.

Type 
        
        
          '
        
        
          help;
        
        
          '
        
         or 
        
          '
        
        
          \h
        
        
          '
        
        
          for
        
         help. Type 
        
          '
        
        
          \c
        
        
          '
        
         to 
        
          clear
        
        
           the current input statement.

MySQL [(none)]
        
        >
        
           show master status;


        
        +------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.
        
          000030
        
         |      
        
          326
        
         |              |                  |

+------------------+----------+--------------+------------------+


        
          1
        
         row 
        
          in
        
         set (
        
          0.00
        
        
           sec)

MySQL [(none)]
        
        >
        
          

# 读写验证

[root@mysql conf]# mysql 
        
        -h127.
        
          0.0
        
        .
        
          1
        
         -uroot -p -
        
          P3306

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection 
        
        
          id
        
         is 
        
          2097086015
        
        
          

Server version: 
        
        
          5.1
        
        .
        
          45
        
        -mysql-amoeba-proxy-
        
          2.2
        
        .
        
          0
        
        
           Source distribution

Copyright (c) 
        
        
          2000
        
        , 
        
          2014
        
        
          , Oracle, SkySQL Ab and others.

Type 
        
        
          '
        
        
          help;
        
        
          '
        
         or 
        
          '
        
        
          \h
        
        
          '
        
        
          for
        
         help. Type 
        
          '
        
        
          \c
        
        
          '
        
         to 
        
          clear
        
        
           the current input statement.

MySQL [(none)]
        
        >
        
           create database amoeba_test;

Query OK, 
        
        
          1
        
         row affected (
        
          0.04
        
        
           sec)

MySQL [(none)]
        
        >
        
          

[root@mysql bin]# mysql 
        
        -h127.
        
          0.0
        
        .
        
          1
        
         -uroot -p -
        
          P3406

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection 
        
        
          id
        
         is 
        
          33
        
        
          

Server version: 
        
        
          10.0
        
        .
        
          10
        
        -MariaDB-
        
          log Source distribution

Copyright (c) 
        
        
          2000
        
        , 
        
          2014
        
        
          , Oracle, SkySQL Ab and others.

Type 
        
        
          '
        
        
          help;
        
        
          '
        
         or 
        
          '
        
        
          \h
        
        
          '
        
        
          for
        
         help. Type 
        
          '
        
        
          \c
        
        
          '
        
         to 
        
          clear
        
        
           the current input statement.

MariaDB [(none)]
        
        >
        
           show databases;


        
        +--------------------+

| Database           |

+--------------------+

| amoeba_test        |

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+


        
          9
        
         rows 
        
          in
        
         set (
        
          0.01
        
        
           sec)

MariaDB [(none)]
        
        >
        
          

# 从amoeba接口登录创建数据库amoeba_test后,再从主库的接口中去查询数据库已创建,说明写入确实是落在了主库节点上;

# 若要验证ameoba对于读操作的调度,则需要暂时停止从库的复制操作,然后在主库上更新数据,这样从ameoba读取数据将出现不一致的情况;
        
      
 

3.5 后期扩展

利用MMM双主复制架构+Amoeba代理,可以实现对MySQL的高可用性和高性能;

关于MMM的内容参加博文" MySQL Scale Out "

4 问题记录

现象 :使用mysql -uroot -p -P8066命令始终无法连接进入ameoba的配置接口,一直都是进入mysql数据库的配置接口

原因 :在测试环境下,ameoba和mysql的主库都部署在同一台主机上,当启动ameoba服务后,即使指定-P8066连接,mysql客户端还是默认采用可被识别的socket文件(/tmp/mysql.sock)连接,同样指定-hlocalhost也是一样的;

当使用mysql命令连接mysqld时:

  • 连接主机为localhost或不指定时,mysql会采用Unix Socket的连接方式;

  • 连接主机为127.0.0.1时,mysql会采用TCP的方式连接;

解决方法 :指定-h127.0.0.1连接即可,即mysql -h127.0.0.1 -uroot -p -P8066

使用Amoeba 实现MySQL DB 读写分离


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论