使用Amoeba for mysql实现mysql读写分离

时间:2022-09-20 14:59:09

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

它是一个虚拟的mysql,对外提供mysql协议。客户端连接amoeba就象连接mysql一样

使用Amoeba for mysql实现mysql读写分离

 

安装proxy amoeba

1.在amoeba上安装java环境

apt-get install openjdk-6-jre-headless

2.安装amoeba

Amoeba下载

把下载的压缩包解压完了之后放到 /usr/local/ 目录下即可

mkdir /usr/local/amoeba
wget http://softlayer.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz
tar xzf amoeba-mysql-binary-2.1.0-RC5.tar.gz -C /usr/local/amoeba

配置环境变量

vi ~/.bash_profile

 设置为 

PATH=$PATH:$HOME/bin:/usr/local/amoeba/bin
JAVA_HOME=/usr/java/jdk1.6.0_32

export JAVA_HOME
export PATH

 立即生效

source  ~/.bash_profile

  配置Amoeba for mysql的读写分离的文件

  (1)amoeba/conf/dbServers.xml

    定义由Amoeba代理的数据库如何连接

  (2)amoeba/conf/amoeba.xml

    定义了Amoeba代理的相关配置

dbServers.xml

abstractServer配置

    <dbServer name="abstractServer" abstractive="true">
<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 schema -->
<property name="schema">dbname</property>
<!-- mysql user -->
<property name="user">root</property>
<!-- mysql password -->
<property name="password">root</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">500</property>
<property name="maxIdle">500</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>

 真实mysql服务器的端口,数据库名称,mysql用户及密码

<dbServer name="Master"  parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.1</property>
</factoryConfig>
</dbServer>
<dbServer name="Slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.2</property>
</factoryConfig>
</dbServer>
<dbServer name="Slave2" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.3</property>
</factoryConfig>
</dbServer>
<dbServer name="virtualSlave" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">Slave1,Slave2</property>
</poolConfig>
</dbServer>

amoeba.xml配置

amoeba连接验证配置

         <property name="authenticator">
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<property name="user">root</property>
<property name="password">root</property>
<property name="filter">
<bean class="com.meidusa.amoeba.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>

  读写分离配置

    <property name="defaultPool">Master</property>
<property name="writePool">Master</property>
<property name="readPool">virtualSlave</property>

defaultPool:配置了默认的数据库节点,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行。
writePool :配置了数据库写库,通常配为Master
readPool :配置了数据库读库,通常配为Slave

启动

amoeba start

以前台的方式启动,会输出启动时的信息,检查没有错误信息后,中断,改为后台运行  

amoeba start &