Mysql 基於 Amoeba 的 讀寫分離

首先說明一下amoeba 跟 MySQL proxy在讀寫分離的使用上面的區別:

 

在MySQL proxy 6.0版本 上面如果想要讀寫分離並且 讀集羣、寫集羣 機器比較多情況下,用mysql proxy 需要相當大的工作量,目前mysql proxy沒有現成的 lua腳本。mysql proxy根本沒有配置文件, lua腳本就是它的全部,當然lua是相當方便的。那麼同樣這種東西需要編寫大量的腳本才能完成一 個複雜的配置。而Amoeba只需要進行相關的配置就可以滿足需求。



 

假設有這樣的使用場景,有三個數據庫節點分別命名爲Master、Slave1、Slave2如下:

 

Amoeba: Amoeba <192.168.14.129>

Master: Master <192.168.14.131> (可讀寫)

Slaves:Slave1 <192.168.14.132>、Slave2<192.168.14.133> (2個平等的數據庫。只讀/負載均衡)

 

在 主從數據庫 的複製的部分, 任然需要使用數據庫自己的複製機制。 Amoeba 不提供複製功能。

 

1. 起動數據庫的主從複製功能。

 

a. 修改配置文件

 

master.cnf

Cnf代碼  收藏代碼

  1. server-id = 1 #主數據庫標誌  

  2.   

  3. # 增加 日誌文件, 用於驗證讀寫分離  

  4. log = /home/mysql/mysql/log/mysql.log  

 

slave1.cnf

Cnf代碼  收藏代碼

  1. server-id = 2  

  2.   

  3. # 增加 日誌文件, 用於驗證讀寫分離  

  4. log = /home/mysql/mysql/log/mysql.log  

 

slave2.cnf

Cnf代碼  收藏代碼

  1. server-id = 3  

  2.   

  3. # 增加 日誌文件, 用於驗證讀寫分離  

  4. log = /home/mysql/mysql/log/mysql.log  

 

b. Master 中 創建兩個 只讀權限 的用戶。 用戶名均爲:repl_user   密碼均爲:copy  分別開放給 slave1, slave2

 

Sql代碼  收藏代碼

  1. mysql> grant replication slave on *.* to [email protected] identified by 'copy';  

  2.   

  3. mysql> grant replication slave on *.* to [email protected] identified by 'copy';  

 

c. 查看 Master 信息

Sql代碼  收藏代碼

  1. mysql> show master status;  

  2. +------------------+----------+--------------+------------------+  

  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  

  4. +------------------+----------+--------------+------------------+  

  5. | mysql-bin.000017 |     2009 |              |                  |  

  6. +------------------+----------+--------------+------------------+  

  7. 1 row in set (0.00 sec)  

 

 

d. Slave1 ,Slave2 中 啓動 Master - Slave 複製功能。

 

分別執行以下命令

 

Sql代碼  收藏代碼

  1. mysql> slave stop;  

  2. Query OK, 0 rows affected (0.02 sec)  

  3.   

  4. mysql> change master to  

  5.     -> master_host='192.168.14.131',  

  6.     -> master_user='repl_user',  

  7.     -> master_password='copy',  

  8.     -> master_log_file='mysql-bin.000017',  

  9.     -> master_log_pos=2009;  

  10. Query OK, 0 rows affected (0.03 sec)  

  11.   

  12. mysql> start slave;  

  13. Query OK, 0 rows affected (0.00 sec)  

 

2. Amoeba 讀寫分離的配置

 

a. Master , Slave1 ,Slave2 中開放權限給 Amoeba 訪問。

 

在 Master , Slave1 , Slave2 中分別執行

 

Sql代碼  收藏代碼

  1. mysql> grant all on test.* to [email protected] indentified by '1234';  

Amoeba 訪問三個數據庫的賬號密碼相同。

 

b. 修改 Amoeba 的配置文件

 

配置文件詳細說明請查看 官方文檔:http://docs.hexnova.com/amoeba/rw-splitting.html

 

dbServer.xml

Xml代碼  收藏代碼

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

  2.   

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

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

  5.   

  6.         <!--   

  7.             Each dbServer needs to be configured into a Pool,  

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

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

  10.              such as 'multiPool' dbServer     

  11.         -->  

  12.   

  13.     <!-- 數據庫連接配置的公共部分 -->   

  14.     <dbServer name="abstractServer" abstractive="true">  

  15.         <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">  

  16.             <property name="manager">${defaultManager}</property>  

  17.             <property name="sendBufferSize">64</property>  

  18.             <property name="receiveBufferSize">128</property>  

  19.                   

  20.             <!-- mysql port -->  

  21.             <property name="port">3306</property>  

  22.               

  23.             <!-- mysql schema -->  

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

  25.               

  26.             <!-- mysql user -->  

  27.             <property name="user">test_user</property>  

  28.               

  29.             <!--  mysql password -->  

  30.             <property name="password">1234</property>  

  31.               

  32.         </factoryConfig>  

  33.   

  34.         <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">  

  35.             <property name="maxActive">500</property>  

  36.             <property name="maxIdle">500</property>  

  37.             <property name="minIdle">10</property>  

  38.             <property name="minEvictableIdleTimeMillis">600000</property>  

  39.             <property name="timeBetweenEvictionRunsMillis">600000</property>  

  40.             <property name="testOnBorrow">true</property>  

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

  42.         </poolConfig>  

  43.     </dbServer>  

  44.   

  45.     <!-- Master ,Slave1, Slave2 的獨立部分,也就只有 IP 了 -->  

  46.     <dbServer name="master"  parent="abstractServer">  

  47.         <factoryConfig>  

  48.             <!-- mysql ip -->  

  49.             <property name="ipAddress">192.168.14.131</property>  

  50.         </factoryConfig>  

  51.     </dbServer>  

  52.   

  53.     <dbServer name="slave1"  parent="abstractServer">  

  54.         <factoryConfig>  

  55.             <!-- mysql ip -->  

  56.             <property name="ipAddress">192.168.14.132</property>  

  57.         </factoryConfig>  

  58.     </dbServer>  

  59.   

  60.     <dbServer name="slave2"  parent="abstractServer">  

  61.         <factoryConfig>  

  62.             <!-- mysql ip -->  

  63.             <property name="ipAddress">192.168.14.133</property>  

  64.         </factoryConfig>  

  65.     </dbServer>     

  66.       

  67.     <!-- 數據庫池,虛擬服務器,實現讀取的負載均衡 -->  

  68.     <dbServer name="slaves" virtual="true">  

  69.         <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">  

  70.             <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->  

  71.             <property name="loadbalance">1</property>  

  72.               

  73.             <!-- Separated by commas,such as: server1,server2,server1 -->  

  74.             <property name="poolNames">slave1,slave2</property>  

  75.         </poolConfig>  

  76.     </dbServer>  

  77.           

  78. </amoeba:dbServers>  

 

amoeba.xml

Xml代碼  收藏代碼

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

  2.   

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

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

  5.   

  6.     <proxy>  

  7.       

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

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

  10.             <!-- Amoeba 端口號 -->  

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

  12.               

  13.             <!-- bind ipAddress -->  

  14.             <!--  

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

  16.              -->  

  17.               

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

  19.               

  20.             <property name="connectionFactory">  

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

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

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

  24.                 </bean>  

  25.             </property>  

  26.               

  27.             <property name="authenticator">  

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

  29.                     <!-- Amoeba 賬號,密碼 -->  

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

  31.                       

  32.                     <property name="password">root</property>  

  33.                       

  34.                     <property name="filter">  

  35.                         <bean class="com.meidusa.amoeba.server.IPAccessController">  

  36.                             <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>  

  37.                         </bean>  

  38.                     </property>  

  39.                 </bean>  

  40.             </property>  

  41.               

  42.         </service>  

  43.           

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

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

  46.             <!-- port -->  

  47.             <!--  default value: random number  

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

  49.             -->  

  50.             <!-- bind ipAddress -->  

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

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

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

  54.             <property name="connectionFactory">  

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

  56.             </property>  

  57.               

  58.         </service>  

  59.           

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

  61.             <!-- proxy server net IO Read thread size -->  

  62.             <property name="readThreadPoolSize">20</property>  

  63.               

  64.             <!-- proxy server client process thread size -->  

  65.             <property name="clientSideThreadPoolSize">30</property>  

  66.               

  67.             <!-- mysql server data packet process thread size -->  

  68.             <property name="serverSideThreadPoolSize">30</property>  

  69.               

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

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

  72.               

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

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

  75.         </runtime>  

  76.           

  77.     </proxy>  

  78.       

  79.     <!--   

  80.         Each ConnectionManager will start as thread  

  81.         manager responsible for the Connection IO read , Death Detection  

  82.     -->  

  83.     <connectionManagerList>  

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

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

  86.             <!--   

  87.               default value is avaliable Processors   

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

  89.              -->  

  90.         </connectionManager>  

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

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

  93.               

  94.             <!--   

  95.               default value is avaliable Processors   

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

  97.              -->  

  98.         </connectionManager>  

  99.     </connectionManagerList>  

  100.       

  101.         <!-- default using file loader -->  

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

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

  104.     </dbServerLoader>  

  105.       

  106.     <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">  

  107.           

  108.         <property name="ruleLoader">  

  109.             <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">  

  110.                 <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>  

  111.                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>  

  112.             </bean>  

  113.         </property>  

  114.         <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>  

  115.           

  116.         <property name="LRUMapSize">1500</property>  

  117.         <!-- 默認數據庫,主數據庫 -->  

  118.         <property name="defaultPool">master</property>  

  119.           

  120.         <!-- 寫數據庫 -->  

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

  122.         <!-- 讀數據庫,dbServer.xml 中配置的 虛擬數據庫,數據庫池 -->  

  123.         <property name="readPool">slaves</property>  

  124.           

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

  126.     </queryRouter>  

  127. </amoeba:configuration>  

 

rule.xml

Java代碼  收藏代碼

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

  2. <!DOCTYPE amoeba:rule SYSTEM "rule.dtd">  

  3.   

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

  5.     <tableRule name="message" schema="test" defaultPools="server1">  

  6.     </tableRule>  

  7. </amoeba:rule>  

 

不需要 數據庫分片時,不用配置。 但是不能沒有 tableRule 元素, 否則報錯。 隨便寫個空規則就行了。

 

3. 測試讀寫分離

 

a. 在 Master , Slave1 , Slave2  中分別查看 日誌文件: mysql.log

Shell代碼  收藏代碼

  1. tail -f ./log/mysql.log  

 

 

b. 啓動 Amoeba, 使用 Mysql GUI Tools 連接 Amoeba

 


執行以上幾個命令。 查看日誌內容。

 

Master  mysql.log

Shell代碼  收藏代碼

  1. [mysql@prx1 mysql]$ tail -f log/mysql.log  

  2.                   370 Query     SET SESSION sql_mode=''  

  3.                   370 Query     SET NAMES utf8  

  4.                   370 Query     SHOW FULL TABLES  

  5.                   370 Query     SHOW COLUMNS FROM `t_message`  

  6.                   370 Query     SHOW COLUMNS FROM `t_user`  

  7.                   370 Query     SHOW PROCEDURE STATUS  

  8.                   370 Query     SHOW FUNCTION STATUS  

  9. 110813 15:21:11   370 Query     SHOW VARIABLES LIKE 'character_set_server'  

  10.                   370 Query     SHOW FULL COLUMNS FROM `test`.`t_message`  

  11. 110813 15:21:12   370 Query     SHOW CREATE TABLE `test`.`t_message`  

  12. 110813 15:22:40   374 Connect   test_user@192.168.14.129 on test  

  13.                   375 Connect   test_user@192.168.14.129 on test  

  14.                   376 Connect   test_user@192.168.14.129 on test  

  15. 110813 15:23:40   370 Query     insert into t_message values(1'c1')  

  16. 110813 15:24:07   377 Connect   test_user@192.168.14.129 on test  

  17.                   378 Connect   test_user@192.168.14.129 on test  

  18.                   379 Connect   test_user@192.168.14.129 on test  

  19. 110813 15:24:15   370 Query     insert into t_user values(8'n8''p8')  

  20. 110813 15:24:24   370 Query     SHOW FULL COLUMNS FROM `test`.`t_user`  

  21.                   370 Query     SHOW CREATE TABLE `test`.`t_user`  

  22. 110813 15:24:35   370 Query     SHOW FULL COLUMNS FROM `test`.`t_message`  

  23.                   370 Query     SHOW CREATE TABLE `test`.`t_message`  

 Slave1  mysql.log

Shell代碼  收藏代碼

  1. [mysql@prx2 mysql]$ tail -f log/mysql.log  

  2.                   317 Connect   test_user@192.168.14.129 on test  

  3.                   318 Connect   test_user@192.168.14.129 on test  

  4. 110813 15:35:30   315 Query     SELECT @@sql_mode  

  5. 110813 15:35:32   315 Query     SELECT @@sql_mode  

  6. 110813 15:35:44   315 Query     SELECT @@SQL_MODE  

  7. 110813 15:35:46   315 Query     SELECT @@SQL_MODE  

  8. 110813 15:37:18   319 Connect   test_user@192.168.14.129 on test  

  9.                   320 Connect   test_user@192.168.14.129 on test  

  10. 110813 15:37:19   321 Connect   test_user@192.168.14.129 on test  

  11. 110813 15:37:26   246 Quit  

  12. 110813 15:38:21   315 Query     SELECT @@SQL_MODE  

  13. 110813 15:38:22    42 Query     BEGIN  

  14.                    42 Query     insert into t_message values(1'c1')  

  15.                    42 Query     COMMIT /* implicit, from Xid_log_event */  

  16. 110813 15:38:50   322 Connect   test_user@192.168.14.129 on test  

  17.                   323 Connect   test_user@192.168.14.129 on test  

  18.                   324 Connect   test_user@192.168.14.129 on test  

  19. 110813 15:38:58    42 Query     BEGIN  

  20.                    42 Query     insert into t_user values(8'n8''p8')  

  21.                    42 Query     COMMIT /* implicit, from Xid_log_event */  

  22. 110813 15:39:08   315 Query     SELECT @@SQL_MODE  

  23. 110813 15:39:19   315 Query     SELECT @@SQL_MODE  

  24. 110813 15:44:08   325 Connect   test_user@192.168.14.129 on test  

  25.                   326 Connect   test_user@192.168.14.129 on test  

  26.                   327 Connect   test_user@192.168.14.129 on test  

 Slave2  mysql.log

Shell代碼  收藏代碼

  1. [mysql@prx3 mysql]$ tail -f log/mysql.log  

  2. 110813 15:35:08   305 Connect   test_user@192.168.14.129 on test  

  3.                   306 Connect   test_user@192.168.14.129 on test  

  4.                   307 Connect   test_user@192.168.14.129 on test  

  5. 110813 15:35:31   304 Query     SELECT @@sql_mode  

  6.                   304 Query     SELECT @@sql_mode  

  7. 110813 15:35:44   304 Query     SELECT @@SQL_MODE  

  8. 110813 15:35:46   304 Query     SELECT * FROM t_message t  

  9. 110813 15:37:18   308 Connect   test_user@192.168.14.129 on test  

  10.                   309 Connect   test_user@192.168.14.129 on test  

  11.                   310 Connect   test_user@192.168.14.129 on test  

  12. 110813 15:38:21     8 Query     BEGIN  

  13.                     8 Query     insert into t_message values(1'c1')  

  14.                     8 Query     COMMIT /* implicit, from Xid_log_event */  

  15. 110813 15:38:50   311 Connect   test_user@192.168.14.129 on test  

  16.                   312 Connect   test_user@192.168.14.129 on test  

  17.                   313 Connect   test_user@192.168.14.129 on test  

  18. 110813 15:38:58   304 Query     SELECT @@SQL_MODE  

  19.                     8 Query     BEGIN  

  20.                     8 Query     insert into t_user values(8'n8''p8')  

  21.                     8 Query     COMMIT /* implicit, from Xid_log_event */  

  22. 110813 15:39:08   304 Query     select * from t_user  

  23. 110813 15:39:19   304 Query     select * from t_message  

  24. 110813 15:44:08   314 Connect   test_user@192.168.14.129 on test  

  25.                   315 Connect   test_user@192.168.14.129 on test  

  26.                   316 Connect   test_user@192.168.14.129 on test  

 

從日誌中可以看出。

在 Master  mysql.log 中,只執行了 insert into 命令。

在 Slave1 中,只是複製了 insert into 命令, 是主從複製的結果

在 Slave2 中, 複製了 insert into 命令,同時還執行了 select 命令。

 

說明,主從分離已經成功。

轉自:http://www.iteye.com/topic/1113437

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章