标签:mysql读写分离 amoeba for mysql amoeba-mysql
http://onlyoulinux.blog.51cto.com/7941460/1570472
以上blog己经实现了mysql的主从,本文在主从的基础上用amoeba-mysql实现负载均衡,读写分离 .
amoeba:采用java NTO框架无阻塞模式;
amoeba for mysql是amoeba项目的子项目;
1、amoeba要有java环境支持:安装jdk
root@mysql-proxy src]# rpm -ivh jdk-7u67-linux-i586.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... plugin.jar... javaws.jar... deploy.jar...
[root@mysql-proxy src]# cat /etc/profile.d/jdk.sh export JAVA_HOME=/usr/java/jdk1.7.0_67 export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre [root@mysql-proxy src]# source /etc/profile
2、下载amoeba包:
[root@mysql-proxy src]#wget http://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/amoeba-mysql-1.3.1-BETA.zip [root@mysql-proxy amoeba]# unzip amoeba-mysql-1.3.1-BETA.zip [root@mysql-proxy amoeba]# ls amoeba-mysql-1.3.1-BETA.zip bin build.properties build.xml conf doc lib LICENSE.txt logs README.html src
3、以下是我的配置文件(标红的是 需要注意或改动的地方)
[root@mysql-proxy amoeba]# cat conf/amoeba.xml |grep -v "^$"
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
<server>
<!-- proxy server绑定的端口 -->
<property name="port">8066</property>
<!-- proxy server绑定的IP -->
<property name="ipAddress">192.168.0.120</property>
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">20</property>
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>
<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</property>
<!-- socket Send and receive BufferSize(unit:K) -->
<property name="netBufferSize">128</property>
<!-- Enable/disable TCP_NODELAY (disable/enable Nagle‘s algorithm). -->
<property name="tcpNoDelay">true</property>
<!-- 对外验证的用户名 -->
<property name="user">root</property>
<!-- 对外验证的密码 -->
<property name="password">password</property>
<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</server>
<!--
每个ConnectionManager都将作为一个线程启动。
manager负责Connection IO读写/死亡检测
-->
<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>
<!--
一台mysqlServer 需要配置一个pool,
如果多台 平等的mysql需要进行loadBalance,
平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
简单的配置是属性加上 virtual="true",该Pool 不允许配置factoryConfig
或者自己写一个ObjectPool。
-->
<dbServer name="server1">
<!-- PoolableObjectFactory实现类 -->
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<!-- 真实mysql数据库端口 -->
<property name="port">3306</property>
<!-- 真实mysql数据库IP -->
<property name="ipAddress">192.168.0.117</property>
<property name="schema">test</property>
<!-- 用于登陆mysql的用户名和密码 -->
<property name="user">proxy</property>
<!-- 用于登陆mysql的密码 -->
<property name="password">proxy</property>
</factoryConfig>
<!-- ObjectPool实现类 -->
<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">
<!-- PoolableObjectFactory实现类 -->
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<!-- 真实mysql数据库端口 -->
<property name="port">3306</property>
<!-- 真实mysql数据库IP -->
<property name="ipAddress">192.168.0.118</property>
<property name="schema">test</property>
<!-- 用于登陆mysql的用户名 -->
<property name="user">proxy</property>
<!-- 用于登陆mysql的密码 -->
<property name="password">proxy</property>
</factoryConfig>
<!-- ObjectPool实现类 -->
<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">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
<property name="poolNames">server1</property>
</poolConfig>
</dbServer>
<dbServer name="slave" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
<property name="poolNames">server1,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>
4、在主库上对amoeba代理进行授权(只授权主库就可以,从库会自动 同步的)
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘proxy‘@‘192.168.0.120‘ IDENTIFIED BY PASSWORD ‘proxy‘ WITH GRANT OPTION1;
row in set (0.00 sec)
mysql>flush privileges;
5、启动amoeba
[root@mysql-proxy amoeba]# /usr/local/amoeba/bin/amoeba & 先要给amoeba加可执行权限
[root@mysql-proxy amoeba]# ps -ef |grep amoeba
root 7686 1604 0 06:26 pts/1 00:00:22 /usr/java/jdk1.7.0_67/bin/java -server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba -Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher
root 7977 7949 0 07:25 tty1 00:00:00 vim /usr/local/amoeba/conf/amoeba.xml
root 8004 1456 0 07:38 pts/0 00:00:00 grep amoeba
[root@mysql-proxy amoeba]# netstat -tunlp |grep java
tcp 0 0 ::ffff:192.168.0.120:8066 :::* LISTEN 7686/java
6、测试
测试方法:单独建一个test库,在库中建一个表jaychou,表中加一行数据,然后 mysql>slave stop;让数据不再同步,然后在主库中再加入一行数据;在amoeba上反复查询看不同;这是实现了读的测试,写的测试只要在amoeba上插入一行数据就可以了;
# mysql -uroot -ppassword -h192.168.0.120 -P8066 注意分清哪个用户名哪个密码;
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.01 sec)
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> insert into jaychou value(3);
Query OK, 1 row affected (0.01 sec)
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select * from jaychou;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.01 sec)
mysql>
貌似达到这个效果就可以了,
amoeba还可以做负载均衡,不过对于主从两个节点的mysql cluster来说,使用roundrobin机制就可以了 ;对于多节点的mysql集群来说可以用轮循、权重、甚至可以做HA, 各人感觉amoeba for mysql比 mysql-proxy简单好用 多了,
;
本文出自 “Linux_蚂蚁” 博客,请务必保留此出处http://onlyoulinux.blog.51cto.com/7941460/1570816
amoeba for mysql实现 mysql负载均衡,读写分离;
标签:mysql读写分离 amoeba for mysql amoeba-mysql
原文地址:http://onlyoulinux.blog.51cto.com/7941460/1570816