环境:

  1. 一台CentOS6.5-x64最小化安装(ip:10.0.0.226)

    软件:jdk-8u60-linux-x64.rpm, amoeba-mysql-binary-2.2.0.tar.gz

    下载地址:链接: http://pan.baidu.com/s/1c04Wmys 密码: 4tqe

2.一台CentOS6.5-x64最小化安装(ip:10.0.0.222)

    为了节省资源,本次使用的是mysql多实例。3306为主(master),3307为从(slave).

    主从同步这里就不在说明了,创建好主从同步后要先停止从库同步。进入mysql后slave stop;

3.测试机(ip:10.0.0.111)

    测试机应事先有装过mysql,不然登陆的amoeba服务器的时候会出现-bash: mysql: command not found.(如果不是这样的,还希望大侠能留下相关办法。谢谢!)

    3306主库(master)创建用户,这个用户是用来给amoeba写数据时候使用。语句如下:

    mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'amoeba'@'10.0.0.%' IDENTIFIED BY '123456';

    3307从库(slave)创建用户,这个用户是用来给amoeba查询数据时候使用。语句如下:

    mysql> GRANT SELECT ON *.* TO 'amoeba'@'10.0.0.%' IDENTIFIED BY '123456';


Amoeba安装和配置

    安装:

        [root@amoeba ~]# rpm -ivh jdk-8u60-linux-x64.rpm

        [root@amoeba ~]# ln -s /usr/java/jdk1.8.0_60/ /usr/java/jdk1.8   #做个软连接,方便后面操作

        [root@amoeba ~]# vi /etc/profile

        在最后面添加以下两行:

        

        export JAVA_HOME=/usr/java/jdk1.8

        export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

        [root@amoeba ~]# source /etc/profile    #使刚加入的两行生效

        [root@amoeba ~]# mkdir -p /usr/local/amoeba

        [root@amoeba ~]# cd /usr/local/amoeba

        [root@amoeba amoeba]# tar xf amoeba-mysql-binary-2.2.0.tar.gz

        [root@amoeba amoeba]# rm amoeba-mysql-binary-2.2.0.tar.gz

        [root@amoeba amoeba]# chmod 700 -R bin/

    配置:

    2.2.0和1.3.1有些不一样

    1.3.1是只要修改/usr/local/amoeba/conf/amoeba.xml这个配置文件就可以的

    2.2.0是把/usr/local/amoeba/conf/amoeba.xml这个配置文件分开了,一个是amoeba.xml,另一个是dbServers.xml

    下面贴出配置,尽供参考:

dbServers.xml

[root@amoeba conf]# cat dbServers.xml 

<?xml version="1.0" encoding="gbk"?>


<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">

<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">


                <!-- 

                        Each dbServer needs to be configured into a Pool,

                        If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:

                         add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig

                         such as 'multiPool' dbServer   

                -->


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


                        <!-- mysql user -->

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


                        <!--  mysql password -->

                        <property name="password">123456</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="testOnReturn">true</property>

                        <property name="testWhileIdle">true</property>

                </poolConfig>

        </dbServer>

        <dbServer name="abstractServer2" 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">3307</property>


                        <!-- mysql schema -->

                        <property name="schema">test</property>


                        <!-- mysql user -->

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


                        <!--  mysql password -->

                        <property name="password">123456</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="testOnReturn">true</property>

                        <property name="testWhileIdle">true</property>

                </poolConfig>

        </dbServer>

        <dbServer name="server1"  parent="abstractServer1">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">10.0.0.222</property>

                </factoryConfig>

        </dbServer>


        <dbServer name="server2"  parent="abstractServer2">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">10.0.0.222</property>

                </factoryConfig>

        </dbServer>


        <dbServer name="master" 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">server1</property>

                </poolConfig>

        </dbServer>

        <dbServer name="slave" 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</property>

                </poolConfig>

        </dbServer>

</amoeba:dbServers>


 amoeba.xml  

[root@amoeba conf]# cat amoeba.xml 

<?xml version="1.0" encoding="gbk"?>


<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">

<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">


        <proxy>

        

                <!-- service class must implements com.meidusa.amoeba.service.Service -->

                <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">

                        <!-- port -->

                        <property name="port">8066</property>

                        

                        <!-- bind ipAddress -->

                        <!--  -->

                        <property name="ipAddress">10.0.0.226</property>

                        

                        

                        <property name="manager">${clientConnectioneManager}</property>

                        

                        <property name="connectionFactory">

                                <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">

                                        <property name="sendBufferSize">128</property>

                                        <property name="receiveBufferSize">64</property>

                                </bean>

                        </property>

                        

                        <property name="authenticator">

                                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

                                        

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

                                        

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

                        

                </service>

                

                <!-- server class must implements com.meidusa.amoeba.service.Service -->

                <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">

                        <!-- port -->

                        <!--  default value: random number

                        <property name="port">9066</property>

                        -->

                        <!-- bind ipAddress -->

                        <property name="ipAddress">127.0.0.1</property>

                        <property name="daemon">true</property>

                        <property name="manager">${clientConnectioneManager}</property>

                        <property name="connectionFactory">

                                <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>

                        </property>

                        

                </service>

                

                <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">

                        <!-- 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>

                        

                        <!-- per connection cache prepared statement size  -->

                        <property name="statementCacheSize">500</property>

                        

                        <!-- query timeout( default: 60 second , TimeUnit:second) -->

                        <property name="queryTimeout">60</property>

                </runtime>

                

        </proxy>

        

        <!-- 

                Each ConnectionManager will start as thread

                manager responsible for the Connection IO read , Death Detection

        -->

        <connectionManagerList>

                <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">

                        <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>

                        <!-- 

                          default value is avaliable Processors 

                        <property name="processors">5</property>

                         -->

                </connectionManager>

                <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>

        

                <!-- default using file loader -->

        <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">

                <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>

        </dbServerLoader>

        

        <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">master</property>

                

                <!---->

                <property name="writePool">master</property>

                <property name="readPool">slave</property>

                

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

        </queryRouter>

</amoeba:configuration>


    启动:

    

[root@amoeba bin]# ./amoeba start

log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml

2015-10-20 21:12:18,932 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0

log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf

2015-10-20 21:12:19,593 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on /10.0.0.226:8066.

2015-10-20 21:12:19,608 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /10.0.0.226:17877.



[root@amoeba bin]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      932/sshd            

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1008/master         

tcp        0      0 ::ffff:10.0.0.226:17877     :::*                        LISTEN      1673/java           

tcp        0      0 :::22                       :::*                        LISTEN      932/sshd            

tcp        0      0 ::1:25                      :::*                        LISTEN      1008/master         

tcp        0      0 ::ffff:10.0.0.226:8066      :::*                        LISTEN      1673/java      

看到以上相关数据,说明已经起动成功。

===========================================================================================

    验证:

    为了能明显看出效果,这里先暂停主从同步。使用命令slave stop结束。

    先进入主库3306[master],查看测试数据库tudou的test表,内容如下

    

        mysql> select * from test;

        +--------+

        | name   |

        +--------+

        | shanqi |

        | shanqi |

        +--------+

     再进入从库3307[slave],查看测试数据库tudou的test表,内容如下

    

        mysql> select * from test;

         +--------+

         | name   |

         +--------+

         | shanqi |

         | shanqi |

         | green  |

         +--------+

        
    这里已事先在从库插入一条主库没有的记录,方便接下来的测试。

    使用一台测试机(IP:10.0.0.111)

    登陆

         [root@mail amoeba]# mysql -uroot -p123456 -P8066 -h10.0.0.226

        Welcome to the MySQL monitor.  Commands end with ; or \g.

        Your MySQL connection id is 1093642553

        Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution

        

        Copyright (c) 2000, 2013, 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 |

        | ccmate             |

        | mysql              |

        | performance_schema |

        | test               |

        | tudou              |

        +--------------------+

        6 rows in set (0.00 sec)

       查询的时候,amoeba会自动去从库查询。

        mysql> select * from tudou.test;

        +--------+

        | name   |

        +--------+

        | shanqi |

        | shanqi |

        | green  |

        +--------+

        3 rows in set (0.01 sec)

        插入一条记录到测试表tudou.test,然后去主库[master]查询下

        mysql> insert into tudou.test values("test");

        Query OK, 1 row affected (0.04 sec)

        然后去主库[master]查询下

        

        mysql> select * from test;

        +--------+

        | name   |

        +--------+

        | shanqi |

        | shanqi |

        | test   |

        +--------+

        3 rows in set (0.00 sec)

        到此说明amoeba读写分离成功。

            

        问题:
        启动的时候会出现以下问题:

        

        [root@amoeba bin]# ./amoeba start

        

        The stack size specified is too small, Specify at least 228k

        Error: Could not create the Java Virtual Machine.

        Error: A fatal exception has occurred. Program will exit.

        

        这里是amoeba 启动脚本判断时有个地方设置过小,需要修改下

        [root@amoeba bin]# vi amoeba

        找到DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"这一行,把128改成256即可