MySQL Amoeba读写分离

Amoeba 简介

Amoeba for mysql可作为mysql的分布式数据库前端代理层,它主要在应用层访问mysql的时候充当query 路由功能,专注 分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性、query filter、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果。 在amoeba上面你能够完成多数据源的高可用、负载均衡、数据切片的功能。

Amoeba for mysql不足:

  • 目前还不支持事务
  • 暂时不支持存储过程
  • 不适合从amoeba导数据的场景或者对大数据量查询的query并不合适(比如一次请求返回10w以上甚至更多数据的场合)
  • 暂时不支持分库分表,amoeba目前只做到分数据库实例,每个被切分的节点需要保持库表结构一致

安装Amoeba

shell> tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /db/tool/amoeba
shell> vi /etc/profile
=========================================================
export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk   #使用系统自带JDK
export AMOEBA_HOME=/db/tool/amoeba
PATH=$AMOEBA_HOME/bin:$JAVA_HOME/bin:$PATH
=========================================================
shell> source /etc/profile

shell> java -version
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.4) (rhel-1.41.1.10.4.el6-i386)
OpenJDK Server VM (build 20.0-b11, mixed mode)

安装完成后,你可以通过命令确定Amoeba是否成功安装。
shell> amoeba
amoeba start|stop

配置Amoeba for mysql

$AMOEBA_HOME/conf/dbServers.xml:存储代理的数据库连接方式,如:主机IP、端口、Amoeba使用的用户名和密码等
$AMOEBA_HOME/conf/amoeba.xml:定义了Amoeba代理的相关配置
$AMOEBA_HOME/conf/functionMap.xml:数据库函数名和函数解析处理的关系,如:UNIX_TIMESTAMP()、SYSDATE()等等
$AMOEBA_HOME/conf/rule.xml:数据切分规则配置
$AMOEBA_HOME/conf/ruleFunctionMap.xml:rule.xml中自定义的函数
$AMOEBA_HOME/conf/access_list.conf:制定可访问以及拒绝访问的主机IP地址
$AMOEBA_HOME/conf/log4j.xml:Amoeba允许用户配置输出日志级别以及方式,配置方法使用log4j的文件格式

shell> vi /db/tool/amoeba/conf/dbServers.xml
========================================================================================



  



  
    
      ${defaultManager}
      64
      128
      
      db_test
      root
      password
    

    
      500
      500
      10
      600000
      600000
      true
      true
      true
    
  

  
    
      127.0.0.1
      3307
    
  

  
    
      127.0.0.1
      3308
    
  

  
    
      
      1
      slave,master,slave
    
  

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


shell> vi /db/tool/amoeba/conf/amoeba.xml
========================================================================================





  
    8066
    192.168.90.128
    ${clientConnectioneManager}
    
      
        128
        64
      
    

    
      
        root
        password
        
          
            ${amoeba.home}/conf/access_list.conf
          
        
      
    
  

  
    9066
    127.0.0.1
    true
    ${clientConnectioneManager}
    
      
    
  

 
    20
    30
    30
    500
    600
  



  
    com.meidusa.amoeba.net.ConnectionManager
    5
  

  
    com.meidusa.amoeba.net.AuthingableConnectionManager
    5
  



  ${amoeba.home}/conf/dbServers.xml



  
    
      ${amoeba.home}/conf/rule.xml
      ${amoeba.home}/conf/ruleFunctionMap.xml
    
  
  ${amoeba.home}/conf/functionMap.xml
  1500
  master
  master
  virtualdb
  true


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

验证Amoeba

shell> amoeba start
log4j:WARN log4j config load completed from file:/db/tool/amoeba/conf/log4j.xml
2015-01-15 01:28:21,450 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0
log4j:WARN ip access config load completed from file:/db/tool/amoeba/conf/access_list.conf
2015-01-15 01:28:21,831 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on /192.168.90.128:8066.
2015-01-15 01:28:21,838 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:9066.


shell> mysql -h 127.0.0.1 -u root -P 8066 -p
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          11 |
+-------------+
1 row in set (0.03 sec)

mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          12 |
+-------------+
1 row in set (0.02 sec)

mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          12 |
+-------------+
1 row in set (0.01 sec)


mysql> create table db_test.t (id int);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into t values (1),(2),(3),(4);
Query OK, 4 rows affected (0.05 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from db_test.t;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.04 sec)

【slave】
mysql> stop slave;
Query OK, 0 rows affected (0.02 sec)

【ameoba代理端】
mysql> insert into t values(5);
Query OK, 1 row affected (0.05 sec)

mysql> select * from db_test.t;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.01 sec)

mysql> select * from db_test.t;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.01 sec)

mysql> select * from db_test.t;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+
5 rows in set (0.01 sec)

详细文档参见:
http://docs.hexnova.com/amoeba

整理自网络

Svoid
2015-01-14

你可能感兴趣的:(MySQL高可用)