Amoeba是一个以MySQL为底层数据存储,并相应用提供MySQL协议接口的proxy。它集中地响应应用的请求,根据用户事先设置的规则。将SQL请求发送到特定的数据库上运行。基于此能够实现负载均衡、读写分离、高可用性等需求。与MySQL官方的MySQL Proxy相比,作者强调的是amoeba配置的方便(基于XML的配置文件,用SQLJEP语法书写规则,比基于lua脚本的MySQL Proxy简单)。本文就来实现一个简单的amoeba的配置
实验环境:
192.168.1.121为amoebaserver,提供读写分离
192.168.1.141为mysql的主server
192.168.1.142为mysql的从server
1.为mysql主server提供配置
编辑/etc/my.cnf,提供下面的配置
log_bin=index
server_id=1
在主server上授权
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文件下须要改动的配置为:
<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>
在dbserver.xml文件里须要改动的配置为:
<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>
在后端代理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 Java 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的server上装一个mysql的client来測试
# yum install mysql
# mysql -uroot -pamoeba -h192.168.1.121
假设连接成功。而且创建成功的语句能够在后端的代理mysql上显示出来,证明amoeba配置成功。当然假设想看是否已经实现了全然的主从分离。能够自己抓包分析查看。
实验环境:
192.168.1.121为amoebaserver,提供读写分离
192.168.1.141为mysql的主server
192.168.1.142为mysql的从server
1.为mysql主server提供配置
编辑/etc/my.cnf,提供下面的配置
log_bin=index
server_id=1
在主server上授权
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文件下须要改动的配置为:
<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>
在dbserver.xml文件里须要改动的配置为:
<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>
在后端代理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 Java 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的server上装一个mysql的client来測试
# yum install mysql
# mysql -uroot -pamoeba -h192.168.1.121
假设连接成功。而且创建成功的语句能够在后端的代理mysql上显示出来,证明amoeba配置成功。当然假设想看是否已经实现了全然的主从分离。能够自己抓包分析查看。