Mysql的读写分离amoeba

关于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. <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"> 
  4. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> 
  5.  
  6.                 <!--  
  7.                         Each dbServer needs to be configured into a Pool, 
  8.                          such as 'multiPool' dbServer    
  9.                 --> 
  10.  
  11.         <dbServer name="abstractServer" abstractive="true"> 
  12.                 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> 
  13.                         <property name="manager">${defaultManager}</property> 
  14.                         <property name="sendBufferSize">64</property> 
  15.                         <property name="receiveBufferSize">128</property> 
  16.  
  17.                         <!-- mysql port --> 
  18.                         <property name="port">3306</property>         __ ** ##后端数据库端口**__ 
  19.  
  20.                         <!-- mysql schema --> 
  21.                         <property name="schema">tongbu</property>        __ ** ##后端数据库默认库**__ 
  22.                         <!--  mysql password 
  23.                         <property name="password">password</property> 
  24.                         --> 
  25.                 </factoryConfig> 
  26.  
  27.                 <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> 
  28.                         <property name="maxActive">500</property> 
  29.                         <property name="maxIdle">500</property> 
  30.                         <property name="minIdle">10</property> 
  31.                         <property name="minEvictableIdleTimeMillis">600000</property> 
  32.                         <property name="timeBetweenEvictionRunsMillis">600000</property> 
  33.                         <property name="testOnBorrow">true</property> 
  34.                         <property name="testWhileIdle">true</property> 
  35.                 </poolConfig> 
  36.         </dbServer> 
  37.   
  38.         <dbServer name="master"  parent="abstractServer">                          __ ** ##定义主的写的节点**__ 
  39.                 <factoryConfig> 
  40.                         <property name="ipAddress">192.168.0.100</property>      __ ** ##主masterIP**__ 
  41.                         <property name="user">user</property>                 __ ** ##与主mysql通信,连接数据库的帐号,以下是密码**__ 
  42.                         <property name="password">password</property> 
  43.                 </factoryConfig> 
  44.         </dbServer> 
  45.  
  46.         <dbServer name="slave"  parent="abstractServer"> 
  47.                 <factoryConfig> 
  48.                         <property name="ipAddress">192.168.0.101</property> 
  49.                         <property name="user">user</property>                 __ ** ##与从mysql通信,连接数据库的帐号,以下是密码**__ 
  50.                         <property name="password">password</property> 
  51.                 </factoryConfig> 
  52.         </dbServer> 
  53.  
  54.         <dbServer name="server1" virtual="true"> 
  55.                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">    __ ** ##定义写的池,把master节点加入**__ 
  56.                         <property name="loadbalance">1</property> 
  57.                         <property name="poolNames">master</property> 
  58.                 </poolConfig> 
  59.         </dbServer> 
  60.  
  61.         <dbServer name="server2" virtual="true"> 
  62.                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">    __ ** ##定义读的池,把slave节点加入**__ 
  63.                         <property name="loadbalance">1</property> 
  64.                         <property name="poolNames">slave</property> 
  65.                 </poolConfig> 
  66.         </dbServer> 
  67. </amoeba:dbServers> 

5.2 配置amoeba.xml

  1. <?xml version="1.0" encoding="gbk"?> 
  2. <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/"> 
  3.  
  4.         <proxy> 
  5.  
  6.                 <!-- service class must implements com.meidusa.amoeba.service.Service --> 
  7.                         <!-- port --> 
  8.                          <property name="port">6666</property>                   __ ** ##定义amoeba读写分离proxy对外代理的端口**__ 
  9.                         <!-- bind ipAddress --> 
  10.                        
  11.                         <property name="ipAddress">192.168.0.103</property>
  12.                         
  13.  
  14.                         <property name="manager">${clientConnectioneManager}</property> 
  15.  
  16.                         <property name="connectionFactory"> 
  17.                                         <property name="sendBufferSize">128</property> 
  18.                                         <property name="receiveBufferSize">64</property> 
  19.                                 </bean> 
  20.                         </property> 
  21.  
  22.                         <property name="authenticator"> 
  23.                                          <property name="user">dbproxy_user</property>     __ ** ##定义proxy的管理帐号密码,客户端和程序只需要连接proxy的帐号密码即可,相当于中间接封装**__ 
  24.                                         <property name="password">dbproxy_pw</property> 
  25.  
  26.                                         <property name="filter"> 
  27.                                                 </bean> 
  28.                                         </property> 
  29.                                 </bean> 
  30.                         </property> 
  31.  
  32.                 </service> 
  33.  
  34.                 <!-- server class must implements com.meidusa.amoeba.service.Service --> 
  35.                         <!-- port --> 
  36.                         <!--  default value: random number 
  37.                         <property name="port">9066</property> 
  38.                         --> 
  39.                         <!-- bind ipAddress --> 
  40.                         <property name="ipAddress">127.0.0.1</property> 
  41.                         <property name="daemon">true</property> 
  42.                         <property name="manager">${clientConnectioneManager}</property> 
  43.                         <property name="connectionFactory"> 
  44.                 <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> 
  45.                         <!-- proxy server net IO Read thread size --> 
  46.                         <property name="readThreadPoolSize">20</property> 
  47.  
  48.                         <!-- proxy server client process thread size --> 
  49.  
  50.                         <!-- per connection cache prepared statement size  --> 
  51.                         <property name="statementCacheSize">500</property> 
  52.  
  53.                         <!-- query timeout( default: 60 second , TimeUnit:second) --> 
  54.                         <property name="queryTimeout">60</property> 
  55.                 </runtime> 
  56.  
  57.         </proxy> 
  58.  
  59.         <!--  
  60.                 Each ConnectionManager will start as thread 
  61.                 manager responsible for the Connection IO read , Death Detection 
  62.         --> 
  63.         <connectionManagerList> 
  64.                         <!--  
  65.                           default value is avaliable Processors  
  66.                         <property name="processors">5</property> 
  67.                          --> 
  68.                 </connectionManager> 
  69.  
  70.                         <!--  
  71.                           default value is avaliable Processors  
  72.                         <property name="processors">5</property> 
  73.                          --> 
  74.                 </connectionManager> 
  75.         </connectionManagerList> 
  76.  
  77.                 <!-- default using file loader --> 
  78.         <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader"> 
  79.                 <property name="configFile">${amoeba.home}/conf/dbServers.xml</property> 
  80.         </dbServerLoader> 
  81.  
  82.         <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> 
  83.                 <property name="ruleLoader"> 
  84.                         <bean class="com.meidusa.amoeba.route.TableRuleFileLoader"> 
  85.                                 <property name="ruleFile">${amoeba.home}/conf/rule.xml</property> 
  86.                                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property> 
  87.                         </bean> 
  88.                 </property> 
  89.                 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> 
  90.                 <property name="LRUMapSize">1500</property> 
  91.                 <property name="defaultPool">server1</property>                   __ ** ##定义默认的池,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行。**__ 
  92.                 <property name="writePool">server1</property>                      __ ** ##定义写的池**__ 
  93.                 <property name="readPool">server2</property>                      __ ** ##定义读的池**__ 
  94.                 <property name="needParse">true</property> 
  95.         </queryRouter> 
  96. </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就不存在。就简单介绍到这里吧。




你可能感兴趣的:(Mysql的读写分离amoeba)