amoeba + mysql-mmm 实现读写分离

mysql-mmm配置请查看:http://lihuipeng.blog.51cto.com/3064864/689064

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的配置分成几个文件:

amoeba.xml      需要修改 主配置文件,配置所有数据源及Amoeba自身的参数
dbServer.xml     需要修改
access_list.conf    需要修改
rule.xml         数据切分时使用
log4j.xml         无需修改
functionMap.xml 无需修改        配置用于解析Query中的函数所对应的Java实现类nullFunctionMap.xml 无需修改     配置路由规则中需要使用到的特定函数的实现类。

 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 schema -->
                        <property name="schema">test</property>

                        <!-- mysql user -->
                        <property name="user">lihuipeng</property>

                        <!--  mysql password-->
                        <property name="password">lihuipeng</property>
                        
                </factoryConfig>

……

        <dbServer name="server1"  parent="abstractServer">
                <factoryConfig>
                        <!-- mysql ip -->
                        <property name="ipAddress">192.168.93.141</property>
                </factoryConfig>
        </dbServer>

        <dbServer name="server2"  parent="abstractServer">
                <factoryConfig>
                        <!-- mysql ip -->
                        <property name="ipAddress">192.168.93.142</property>
                </factoryConfig>
        </dbServer>

        <dbServer name="server3"  parent="abstractServer">
                <factoryConfig>
                        <!-- mysql ip -->
                        <property name="ipAddress">192.168.93.144</property>
                </factoryConfig>
        </dbServer>

        <dbServer name="ReadPool" 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">server2,server3</property>
                </poolConfig>
        </dbServer>

定义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">
          <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

                                        <property name="user">amoeba</property>

                                        <property name="password">amoeba</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>
 

……

      <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">server1</property>
                <property name="readPool">ReadPool</property>

                <property name="needParse">true</property>
        </queryRouter>

第一处修改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
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17244592
Server version: 5.1.45-mysql-amoeba-proxy-2.1.0-RC5 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.02 sec)
 

配置完了!想测试读写分离效果的童鞋,可以将三台db的主从先关掉,然后在各个库插入一条不同的数据,然后登陆amoeba进行多次搜索就可以看到结果了!

你可能感兴趣的:(mysql,数据库,读写分离,amoeba,mysql-mmm)