天天看點

MySQL資料庫讀寫分離

1、安裝jdk

root@proxy:~# java -version

java version "1.6.0_45"

Java(TM) SE Runtime Environment (build 1.6.0_45-b06)

Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

2、配置dbServer.xml

root@proxy:/usr/local/amoeba/conf# cat dbServers.xml

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

<!-- 
                    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="abstractServer" 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">root</property>

                    <!-- mysql password -->
                    <property name="password">111</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="master1" parent="abstractServer">
            <factoryConfig>
                    <!-- mysql ip -->
                    <property name="ipAddress">10.10.10.40</property>
            </factoryConfig>
    </dbServer>

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

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

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

rp池配置的是salve1和slave2節點

<dbServer name="rp" 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">slave1,slave2</property>
            </poolConfig>
    </dbServer>           

wp池配置的是master1和master2節點

<dbServer name="wp" 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">master1,master2</property>
            </poolConfig>
    </dbServer>
           

3、配置amoeba.xml

root@proxy:/usr/local/amoeba/conf# cat amoeba.xml

<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">127.0.0.1</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">root</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>           

default池隻配置了master1節點

<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
            <property name="LRUMapSize">1500</property>
            <property name="defaultPool">master1</property>

            <property name="writePool">wp</property>
            <property name="readPool">rp</property>

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

4、啟動amoeba

root@proxy:/usr/local/amoeba# bin/amoeba start &

[1] 2511

root@proxy:/usr/local/amoeba#

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

Could not create the Java virtual machine.

[1]+ Exit 1 bin/amoeba start

更改bin/amoeba檔案

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"

更改為

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k"

啟動正常之後,連接配接amoeba

root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066

Enter password:

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

Your MySQL connection id is 307988519

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

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

Oracle is a registered trademark of Oracle Corporation and/or its

  1. Other names may be trademarks of their respective

    owners.

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

java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed

[email protected]:(none) MySQL-> at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1191)

at com.meidusa.amoeba.net.poolable.GenericObjectPool.borrowObject(GenericObjectPool.java:381)
    at com.meidusa.amoeba.mysql.handler.CommandMessageHandler.startSession(CommandMessageHandler.java:633)
    at com.meidusa.amoeba.mysql.handler.MySqlCommandDispatcher.handleMessage(MySqlCommandDispatcher.java:123)
    at com.meidusa.amoeba.mysql.net.MysqlClientConnection$2.run(MysqlClientConnection.java:291)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
               

連接配接報錯,檢視root.log的輸出

2015-08-29 16:01:03,299 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.40:3306]

2015-08-29 16:01:03,303 ERROR net.MysqlServerConnection - handShake with /10.10.10.40:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=661272757

2015-08-29 16:01:03,324 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.50:3306]

2015-08-29 16:01:03,327 ERROR net.MysqlServerConnection - handShake with /10.10.10.50:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=505588567

2015-08-29 16:01:03,331 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.41:3306]

2015-08-29 16:01:03,334 ERROR net.MysqlServerConnection - handShake with /10.10.10.41:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=530654357

2015-08-29 16:01:03,335 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.42:3306]

2015-08-29 16:01:03,338 ERROR net.MysqlServerConnection - handShake with /10.10.10.42:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=848649429

檢視資料庫連接配接權限

root@localhost:mysql MySQL-> select host,user from user;
host user
% rep_user
root
127.0.0.1
::1
localhost
ubuntu

8 rows in set (0.00 sec)

資料庫權限正常,使用用戶端連接配接測試

root@proxy:~# mysql -uroot -p -h10.10.10.40

Enter password:

Your MySQL connection id is 417

Server version: 5.6.19-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

[email protected]:(none) MySQL-> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

[email protected]:test MySQL-> show tables;
Tables_in_test
t1

1 row in set (0.00 sec)

用戶端連接配接也正常,猜想應該是amoeba配置有問題

<!-- mysql password
                    <property name="password">111</property>
                    -->           

發現密碼被注釋了,更改

<!-- mysql password -->
                    <property name="password">111</property>           

啟動一切正常

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1113757163
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Enterprise Server - Advanced Edition (Commercial)

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

[[email protected]:(none)][12:06:15am] MySQL-> use test;
Database changed
[[email protected]:test][12:06:18am] MySQL->
[[email protected]:test][12:06:18am] MySQL-> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
           

5、測試讀寫分離

停止master2,slave1,slave2節點的複制

root@localhost:(none) MySQL-> stop slave;

Query OK, 0 rows affected (0.04 sec)

root@localhost:(none) MySQL-> show slave status G

1. row **

Slave_IO_State: 
              Master_Host: 10.10.10.40
              Master_User: rep_user
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: master1-bin.000007
      Read_Master_Log_Pos: 442
           Relay_Log_File: slave-relay-bin.000008
            Relay_Log_Pos: 607
    Relay_Master_Log_File: master1-bin.000007
         Slave_IO_Running: No
        Slave_SQL_Running: No
                   

使用amoeba連接配接,往測試表t1中插入測試資料

Your MySQL connection id is 750410715

Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Enterprise Server - Advanced Edition (Commercial)

[email protected]:(none) MySQL-> use test;

[email protected]:test MySQL-> insert into t1 values (null,'aaaaa');

Query OK, 1 row affected (0.05 sec)

master1檢視表t1

root@localhost:mysql MySQL-> select * from test.t1;
id name
1 aaaaa

在master2,slave1,slave2上檢視表t1

root@localhost:(none) MySQL-> use test;

root@localhost:test MySQL-> select * from t1;

Empty set (0.00 sec)

在proxy上檢視表t1表的資料

[email protected]:test MySQL-> select * from t1;

Empty set (0.05 sec)

在proxy上無法檢視插入的資料,說明讀是在rp池裡,而rp池隻有slave1和slave2,資料沒有同步到從節點。