Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的 上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。与MySQL官方的MySQL Proxy相比,作者强调的是amoeba配置的方便(基于XML的配置文件,用SQLJEP语法书写规则,比基于lua脚本的MySQL Proxy简单)。本文就来实现一个简单的amoeba的配置 实验环境: 192.168.1.121为amoeba服务器,提供读写分离 192.168.1.141为mysql的主服务器 192.168.1.142为mysql的从服务器 1.为mysql主服务器提供配置 编辑/etc/my.cnf,提供以下的配置 log_bin=index server_id=1 在主服务器上授权 mysql> grant replication slave,replication client on user@'192.168.1.142' identified by "123456"; mysql> flush privileges; 2.为mysql从服务提供配置 编辑/etc/my.cnf,提供以下的配置 server_id=10 relay_log=relay 进入mysql命令行接口 mysql > change master to MASTER_HOST="192.168.1.141",MASTER_USER="user",MASTER_PASSWORD="123456",MASTER_LOG_FILE="index.000004",MASTER_LOG_POS=429; mysql > start slave; 如果能够看到Slave_IO_Running: Yes和Slave_SQL_Running:Yes两行信息的话,证明主从配置已经成功。 3.在192.168.1.121上安装amoeba amoeba是java开发的,所以首先需要装jdk. [root@localhost ~]# chmod +x jdk-6u43-linux-x64.bin [root@localhost ~]# ./jdk-6u43-linux-x64.bin # vi /etc/profile.d/java.sh export JAVA_HOME=/root/jdk1.6.0_43/bin export PATH=$JAVA_HOME/bin:$PATH # source /etc/profile.d/java.sh 安装amoeba # wget http://sourceforge.net/projects/amoeba/files/Amoeba%20for%20mysql/2.2.x/amoeba-mysql-binary-2.2.0.tar.gz/download # mkdir /usr/local/amoeba # tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba 进入amoeba目录下会看到一下几个目录 bechmark:压力测试 bin:脚本文件 conf:配置文件 lib:库 在conf文件下有许多配置文件,这里实现读写分离的效果,只需要两个文件即可amoeba.xml和dbserver.xml。在amoeba.xml文件下需要修改的配置为: 在dbserver.xml文件中需要修改的配置为: 在后端代理mysql上给amoeba授权,在192.168.1.141上执行如下操作: grant all on *.* to 'root'@'192.168.1.121' identified by 'amoebapass';(不用在备库执行,会自动同步过去) 所有的东西配置好以后就可以开始启动amoeba了,执行:/usr/local/amoeba/bin/amoeba可能会遇到以下错误: The stack size specified is too small, Specify at least 160k Could not create the virtual machine. 修改 amoeba 文件,vi /usr/local/amoeba/bin/amoeba,找到如下的文件: DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k" 将其修改为: DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k 再次执行/usr/local/amoeba/bin/amoeba,如果出现amoeba start|stop 就可以启动amoeba了 # /usr/local/amoeba/bin/amoeba start 启动成功以后,在安装amoeba的服务器上装一个 的客户端来测试 # yum install mysql # mysql -uroot -pamoeba -h192.168.1.121 如果连接成功,并且创建成功的语句可以在后端的代理mysql上显示出来,证明amoeba配置成功。当然如果想看是否已经实现了完全的主从分离,可以自己抓包分析查看。
< service name ="Amoeba for Mysql" class ="com.meidusa.amoeba.net.ServerableConnectionManager" > <!-- port --> < property name ="port" >3306 </ property > #连接amoeba时所使用的端口号 <!-- bind ipAddress --> <!-- <property name="ipAddress">127.0.0.1</property> # --> < property name ="ipAddress" >0.0.0.0 </ property > #监听地址 < property name ="manager" >${clientConnectioneManager} </ property > < property name ="connectionFactory" > < bean class ="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory" > < property name ="sendBufferSize" >128 </ property > < property name ="receiveBufferSize" >64 </ property > </ bean > </ property > < property name ="authenticator" > < bean class ="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator" > < property name ="user" >root </ property > #连接amoeba时候的账户 < property name ="password" >amoeba </ property > #连接amoeba时候的密码 < property name ="filter" > < bean class ="com.meidusa.amoeba.server.IPAccessController" > < property name ="ipFile" >${amoeba.home}/conf/access_list.conf </ property > </ bean > </ property > </ bean > </ property > </ service > < 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">server1</property> --> #不需要默认路由 < property name ="writePool" >test1 </ property > #写路由到test1上去 < property name ="readPool" >test2 </ property > #读路由到test2上去 < property name ="needParse" >true </ property > </ queryRouter >
< factoryConfig class ="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory" > < property name ="manager" >${defaultManager} </ property > < property name ="sendBufferSize" >64 </ property > < property name ="receiveBufferSize" >128 </ property > <!-- mysql port --> < property name ="port" >3306 </ property > #后端mysql的端口 <!-- mysql schema --> < property name ="schema" >amoeba </ property > #后端mysql的默认连接数据库 <!-- mysql user --> < property name ="user" >root </ property > #连接后端mysql的账户 <!-- mysql password<property name="password">password</property> --> < property name ="password" >amoebapass </ property > #连接后端mysql使用的密码 </ factoryConfig > < dbServer name ="test1" parent ="abstractServer" > < factoryConfig > <!-- mysql ip --> < property name ="ipAddress" >192.168.1.141 </ property > #后端MySQL的ip </ factoryConfig > </ dbServer > < dbServer name ="test2" parent ="abstractServer" > < factoryConfig > <!-- mysql ip --> < property name ="ipAddress" >192.168.1.142 </ property > </ factoryConfig > </ dbServer >