amoeba实现mysql读写分离
安装jdk
jdk7.tar.bz2
tar -jxvf jdk7.tar.bz2
./configure
make && make install
安装mysql
mkdir /usr/local/amoeba/
下载amoeba-mysql-1.3.1-BETA.zip
unzip amoeba-mysql-1.3.1-BETA.zip
配置文件如下
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
<server>
<property name="port">8066</property> //amoeba端口
<property name="ipAddress">10.130.6.29</property> //本机ip
<property name="readThreadPoolSize">20</property>
<property name="clientSideThreadPoolSize">30</property>
<property name="serverSideThreadPoolSize">30</property>
<property name="netBufferSize">128</property>
<property name="tcpNoDelay">true</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="queryTimeout">60</property>
</server>
<connectionManagerList>
<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
</connectionManagerList>
<dbServerList>
<dbServer name="server1"> //mysql主机
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">10.130.6.114</property>//主数据库
<property name="schema">test</property> //库名
<property name="user">pan</property> //代理用户
<property name="password">123456</property> //代理用户密码
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="server2"> //另一台数据库
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">10.130.6.115</property> //mysql
<property name="schema">test</property>
<property name="user">pan</property>
<property name="password">123456</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="master" virtual="true"> //mysql的master
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="loadbalance">1</property>
<property name="poolNames">server1</property> //主数据库
</poolConfig>
</dbServer>
<dbServer name="slave" virtual="true"> //mysql的slave
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="loadbalance">1</property>
<property name="poolNames">server1,server2,server2</property> //从数据库读俩次,主数据库读一次
</poolConfig>
</dbServer>
</dbServerList>
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<!-- <property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property> -->
<property name="LRUMapSize">1500</property>
<property name="defaultPool">master</property> //默认主库
<property name="writePool">master</property> //主库复制写请求
<property name="readPool">slave</property> //读请求
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
更改amoeba.xml配置文件 乱码和注释去掉 <!-- --> (有乱码起不来服务) (部分功能注释要去掉)
amoeba 启动 无报错信息后 后台启动
nohup /usr/local/amoeba/bin/amoeba &
grant all on test.* to pan@'192.168.1.%' identified by '123456';
flush privileges;
在aomeba上登陆
mysql -uroot -ppassword -h192.168.1.159 -P8066 amoeba-server的ip
先关掉主从配置 stop slave
向表插入数据 数据只可以在主上看到
执行select 第一次执行看到主的数据 ,第二次三次为从上的未变动数据 第四次循环为主的数据
此效果为配置成功
如有报错 (相关依赖包根据报错安装)
# vim amoeba
修改58行的Xss参数:
DEFAULT_OPTS="-server -Xms1024m -Xmx1024m -Xss128k"
修改为:
DEFAULT_OPTS="-server -Xms1024m -Xmx1024m -Xss256k"