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]#mkdir /usr/local/amoeba [root@mysql-proxy src]#cd /usr/local/amoeba/ [root@mysql-proxy amoeba]#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 "^$"
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
;