关于Amoeba
Amoeba(变形虫)致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能.

环境:
1.服务器
Master mysql:192.168.0.100
Slave mysql:192.168.0.101
Amoeba  server:192.168.0.103
2.安装主动数据库。
3.安装JDK环境 java -version
官网下载jdk安装包,也可以参考
# chmod 755 jdk-6u25-linux-i586.bin 
# ./jdk-6u25-linux-i586.bin 
# mv jdk1.6.0_25/ /usr/local/jdk 
声明路径,修改/etc/profile,在末尾加上以下代码 
 
export AMOEBA_HOME=/usr/local/amoeba 
export JAVA_HOME=/usr/local/jdk 
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$AMOEBA_HOME/bin 
=====================================
一、安装amoeba
1.wget http://nchc.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz 
2.tar zxf amoeba-mysql-binary-2.1.0-RC5.tar.gz -C /opt/app/amoeba
二、配置amoeba
/opt/app/amoeba/conf下的 
dbServers.xml  ##定义数据库连接信息
amoeba.xml   ##定义读写分离节点管理信息
1.配置dbServers.xml
  1. xml version="1.0" encoding="gbk"?> 
  2.  
  3. > 
  4. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> 
  5.  
  6.                  
  7.                         <property name="port">3306property>         __ ** ##后端数据库端口**__ 
  8.  
  9.                          
  10.                         <property name="schema">tongbuproperty>        __ ** ##后端数据库默认库**__ 
  11.                          
  12.                          
  13.                          <property name="port">6666property>                   __ ** ##定义amoeba读写分离proxy对外代理的端口**__ 
  14.                          
  15.                        
  16.                         <property name="ipAddress">192.168.0.103
  17.                         
  18.  
  19.                         <property name="manager">${clientConnectioneManager}property> 
  20.  
  21.                         <property name="connectionFactory"> 
  22.                                         <property name="sendBufferSize">128property> 
  23.                                         <property name="receiveBufferSize">64property> 
  24.                                 bean> 
  25.                         property> 
  26.  
  27.                         <property name="authenticator"> 
  28.                                          <property name="user">dbproxy_userproperty>     __ ** ##定义proxy的管理帐号密码,客户端和程序只需要连接proxy的帐号密码即可,相当于中间接封装**__ 
  29.                                         <property name="password">dbproxy_pwproperty> 
  30.  
  31.                                         <property name="filter"> 
  32.                                                 bean> 
  33.                                         property> 
  34.                                 bean> 
  35.                         property> 
  36.  
  37.                 service> 
  38.  
  39.                  
  40.                          
  41.                          
  42.                         <property name="ipAddress">127.0.0.1property> 
  43.                         <property name="daemon">trueproperty> 
  44.                         <property name="manager">${clientConnectioneManager}property> 
  45.                         <property name="connectionFactory"> 
  46.                 <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> 
  47.                          
  48.                         <property name="readThreadPoolSize">20property> 
  49.  
  50.                          
  51.  
  52.                          
  53.                         <property name="statementCacheSize">500property> 
  54.  
  55.                          
  56.                         <property name="queryTimeout">60property> 
  57.                 runtime> 
  58.  
  59.         proxy> 
  60.  
  61.          
  62.         <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader"> 
  63.                 <property name="configFile">${amoeba.home}/conf/dbServers.xmlproperty> 
  64.         dbServerLoader> 
  65.  
  66.         <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> 
  67.                 <property name="ruleLoader"> 
  68.                         <bean class="com.meidusa.amoeba.route.TableRuleFileLoader"> 
  69.                                 <property name="ruleFile">${amoeba.home}/conf/rule.xmlproperty> 
  70.                                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xmlproperty> 
  71.                         bean> 
  72.                 property> 
  73.                 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xmlproperty> 
  74.                 <property name="LRUMapSize">1500property> 
  75.                 <property name="defaultPool">server1property>                   __ ** ##定义默认的池,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行。**__ 
  76.                 <property name="writePool">server1property>                      __ ** ##定义写的池**__ 
  77.                 <property name="readPool">server2property>                      __ ** ##定义读的池**__ 
  78.                 <property name="needParse">trueproperty> 
  79.         queryRouter> 
  80. amoeba:configuration> 

附:1.在主、从数据库上授权 amoeba连接权限。
      mysql> grant all privileges on tongbu.* to 'tongbu'@'%' identified by 'tongbu' with grant option;
      mysql> flush privileges;
   2.程序连接使用 amoeba服务器上代理用户名、密码、IP、端口连接(由amoeba.xml设置
      连接之前,可以通过web服务器(或其他服务器)上远程连接测试一下是否能正常通过amoeba进入数据库服务器
      # mysql -udbproxy_user -p tongbu -hproxy_db -P6666

六、启动Amoeba

  1. /usr/local/amoeba/bin/amoeba start & 
  2. 开机自动启动可加入到 /etc/rc.local内 
  3. echo "/usr/local/amoeba/bin/amoeba start &" >> /etc/rc.local 

七、日志排错    日志文件在/usr/local/amoeba/log下  

 PS:amoeba虽然是JAVA写的,看似效率不高,但功能异常强大,支持读写分离,表和库级别的读写分离,数据库水平分割,垂直分割,还有集群。是淘宝的得力作品。喜欢的童鞋可以尝试下。mysql-proxy 只是轻量级的读写分离程序,虽然C写的,但是驱动是需要lua的脚本跑,而且在高并发下经常挂掉。程序还忽略了一些字符设定,如果数据库不是同一编码还会出现乱码,amoeba就不存在。就简单介绍到这里吧。