mysql-mmm配置请查看:http://blog.csdn.net/xyz846/article/details/7451774
mysql-mmm只是把数据库的读写功能分离出来,对于web服务器不可能说我要执行读的功能就连接哪个IP,要执行写的功能就连接哪个IP,这是不可能的,解决这一问题我使用amoeba(变形虫)数据库透明代理,根据执行的sql可自动路由至相应的Ip,很好地结合mysql-mmm真正实现读写分离。
整个拓扑如下:
amoeba服务器我也安装在server4上,下面是我的安装步骤:
一、安装java
wget http://download.oracle.com/otn-pub/java/jdk/6u25-b06/jdk-6u25-linux-i586.bin chmod 755 jdk-6u25-linux-i586.bin ./jdk-6u25-linux-i586.bin mv jdk1.6.0_25/ /usr/local/jdk |
配置环境变量vi /etc/profile
export JAVA_HOME=/usr/local/jdk/ export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$JAVA_HOME/bin:$PATH |
使文件即时生效
source /etc/profile |
检查
[root@localhost opt]# java -version java version "1.6.0_25" Java(TM) SE Runtime Environment (build 1.6.0_25-b06) Java HotSpot(TM) Client VM (build 20.0-b11, mixed mode, sharing) |
二、安装amoeba
mkdir /usr/local/amoeba mv amoeba-mysql-binary-2.1.0-RC5.tar.gz /usr/local/amoeba/ cd /usr/local/amoeba/ tar zxvf amoeba-mysql-binary-2.1.0-RC5.tar.gz chmod 755 bin/* cd bin/ ./amoeba |
运行amoeba会显现如下信息
[root@localhost bin]# ./amoeba amoeba start|stop |
三、配置读写分离
Amoeba的配置分成几个文件:
dbServer.xml
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <!-- mysql port --> <!-- mysql schema --> <!-- mysql user --> <!-- mysql password--> …… <dbServer name="server1" parent="abstractServer"> <dbServer name="server2" parent="abstractServer"> <dbServer name="server3" parent="abstractServer"> <dbServer name="ReadPool" virtual="true"> <!-- Separated by commas,such as: server1,server2,server1 --> |
定义dbserver的时候,记得参考mmm_control show的信息,关键是write角色的机器一定要用write的ip
[root@localhost ~]# mmm_control show db1(192.168.93.137) master/ONLINE. Roles: reader(192.168.93.143), writer(192.168.93.141) db2(192.168.93.138) master/ONLINE. Roles: reader(192.168.93.142) db3(192.168.93.139) slave/ONLINE. Roles: reader(192.168.93.144) |
amoeba.xml
<property name="authenticator"> <property name="user">amoeba</property> <property name="password">amoeba</property> <property name="filter"> …… <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> <property name="needParse">true</property> |
第一处修改web数据库可以连接使用的帐号密码,第二处修改amoeba读 写对应的server,该server就是我们之前定义的。
启动amoeba
nohup /usr/local/amoeba/bin/amoeba start & |
查看进程
[root@localhost conf]# netstat -tunlp | grep java tcp 0 0 :::8066 :::* LISTEN 28426/java tcp 0 0 ::ffff:127.0.0.1:19356 :::* LISTEN 28426/java |
测试登陆
[root@localhost conf]# mysql -uamoeba -pamoeba -P8066 -h192.168.93.140 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; |
配置完了!想测试读写分离效果的童鞋,可以将三台db的主从先关掉,然后在各个库插入一条不同的数据,然后登陆amoeba进行多次搜索就可以看到结果了!