首先說明一下amoeba 跟 MySQL proxy在讀寫分離的使用上面的區別:html
在MySQL proxy 6.0版本 上面若是想要讀寫分離而且 讀集羣、寫集羣 機器比較多狀況下,用mysql proxy 須要至關大的工做量,目前mysql proxy沒有現成的 lua腳本。mysql proxy根本沒有配置文件, lua腳本就是它的所有,固然lua是至關方便的。那麼一樣這種東西須要編寫大量的腳本才能完成一 個複雜的配置。而Amoeba只須要進行相關的配置就能夠知足需求。mysql
假設有這樣的使用場景,有三個數據庫節點分別命名爲Master、Slave一、Slave2以下:sql
Amoeba: Amoeba <192.168.14.129>數據庫
Master: Master <192.168.14.131> (可讀寫)服務器
Slaves:Slave1 <192.168.14.132>、Slave2<192.168.14.133> (2個平等的數據庫。只讀/負載均衡)app
在 主從數據庫 的複製的部分, 任然須要使用數據庫本身的複製機制。 Amoeba 不提供複製功能。負載均衡
1. 起動數據庫的主從複製功能。dom
a. 修改配置文件ide
master.cnf測試
server-id = 1 #主數據庫標誌
# 增長 日誌文件, 用於驗證讀寫分離
log = /home/mysql/mysql/log/mysql.log
slave1.cnf
server-id = 2
# 增長 日誌文件, 用於驗證讀寫分離
log = /home/mysql/mysql/log/mysql.log
slave2.cnf
server-id = 3
# 增長 日誌文件, 用於驗證讀寫分離
log = /home/mysql/mysql/log/mysql.log
b. Master 中 建立兩個 只讀權限 的用戶。 用戶名均爲:repl_user 密碼均爲:copy 分別開放給 slave1, slave2
mysql> grant replication slave on *.* to repl_user@192.168.14.132 identified by 'copy';
mysql> grant replication slave on *.* to repl_user@192.168.14.133 identified by 'copy';
c. 查看 Master 信息
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000017 | 2009 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
d. Slave1 ,Slave2 中 啓動 Master - Slave 複製功能。
分別執行如下命令
mysql> slave stop;
Query OK, 0 rows affected (0.02 sec)
mysql> change master to
-> master_host='192.168.14.131',
-> master_user='repl_user',
-> master_password='copy',
-> master_log_file='mysql-bin.000017',
-> master_log_pos=2009;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
2. Amoeba 讀寫分離的配置
a. Master , Slave1 ,Slave2 中開放權限給 Amoeba 訪問。
在 Master , Slave1 , Slave2 中分別執行
mysql> grant all on test.* to test_user@192.168.14.129 indentified by '1234';
Amoeba 訪問三個數據庫的帳號密碼相同。
b. 修改 Amoeba 的配置文件
配置文件詳細說明請查看 官方文檔:http://docs.hexnova.com/amoeba/rw-splitting.html
dbServer.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="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">test_user</property>
<!-- mysql password -->
<property name="password">1234</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="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<!-- Master ,Slave1, Slave2 的獨立部分,也就只有 IP 了 -->
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.14.131</property>
</factoryConfig>
</dbServer>
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.14.132</property>
</factoryConfig>
</dbServer>
<dbServer name="slave2" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.14.133</property>
</factoryConfig>
</dbServer>
<!-- 數據庫池,虛擬服務器,實現讀取的負載均衡 -->
<dbServer name="slaves" 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>
</amoeba:dbServers>
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">
<!-- Amoeba 端口號 -->
<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">
<!-- Amoeba 帳號,密碼 -->
<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>
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<property name="LRUMapSize">1500</property>
<!-- 默認數據庫,主數據庫 -->
<property name="defaultPool">master</property>
<!-- 寫數據庫 -->
<property name="writePool">master</property>
<!-- 讀數據庫,dbServer.xml 中配置的 虛擬數據庫,數據庫池 -->
<property name="readPool">slaves</property>
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
rule.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:rule SYSTEM "rule.dtd">
<amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/">
<tableRule name="message" schema="test" defaultPools="server1">
</tableRule>
</amoeba:rule>
不須要 數據庫分片時,不用配置。 可是不能沒有 tableRule 元素, 不然報錯。 隨便寫個空規則就好了。
3. 測試讀寫分離
a. 在 Master , Slave1 , Slave2 中分別查看 日誌文件: mysql.log
tail -f ./log/mysql.log
b. 啓動 Amoeba, 使用 Mysql GUI Tools 鏈接 Amoeba
執行以上幾個命令。 查看日誌內容。
Master mysql.log
[mysql@prx1 mysql]$ tail -f log/mysql.log
370 Query SET SESSION sql_mode=''
370 Query SET NAMES utf8
370 Query SHOW FULL TABLES
370 Query SHOW COLUMNS FROM `t_message`
370 Query SHOW COLUMNS FROM `t_user`
370 Query SHOW PROCEDURE STATUS
370 Query SHOW FUNCTION STATUS
110813 15:21:11 370 Query SHOW VARIABLES LIKE 'character_set_server'
370 Query SHOW FULL COLUMNS FROM `test`.`t_message`
110813 15:21:12 370 Query SHOW CREATE TABLE `test`.`t_message`
110813 15:22:40 374 Connect test_user@192.168.14.129 on test
375 Connect test_user@192.168.14.129 on test
376 Connect test_user@192.168.14.129 on test
110813 15:23:40 370 Query insert into t_message values(1, 'c1')
110813 15:24:07 377 Connect test_user@192.168.14.129 on test
378 Connect test_user@192.168.14.129 on test
379 Connect test_user@192.168.14.129 on test
110813 15:24:15 370 Query insert into t_user values(8, 'n8', 'p8')
110813 15:24:24 370 Query SHOW FULL COLUMNS FROM `test`.`t_user`
370 Query SHOW CREATE TABLE `test`.`t_user`
110813 15:24:35 370 Query SHOW FULL COLUMNS FROM `test`.`t_message`
370 Query SHOW CREATE TABLE `test`.`t_message`
Slave1 mysql.log
[mysql@prx2 mysql]$ tail -f log/mysql.log
317 Connect test_user@192.168.14.129 on test
318 Connect test_user@192.168.14.129 on test
110813 15:35:30 315 Query SELECT @@sql_mode
110813 15:35:32 315 Query SELECT @@sql_mode
110813 15:35:44 315 Query SELECT @@SQL_MODE
110813 15:35:46 315 Query SELECT @@SQL_MODE
110813 15:37:18 319 Connect test_user@192.168.14.129 on test
320 Connect test_user@192.168.14.129 on test
110813 15:37:19 321 Connect test_user@192.168.14.129 on test
110813 15:37:26 246 Quit
110813 15:38:21 315 Query SELECT @@SQL_MODE
110813 15:38:22 42 Query BEGIN
42 Query insert into t_message values(1, 'c1')
42 Query COMMIT /* implicit, from Xid_log_event */
110813 15:38:50 322 Connect test_user@192.168.14.129 on test
323 Connect test_user@192.168.14.129 on test
324 Connect test_user@192.168.14.129 on test
110813 15:38:58 42 Query BEGIN
42 Query insert into t_user values(8, 'n8', 'p8')
42 Query COMMIT /* implicit, from Xid_log_event */
110813 15:39:08 315 Query SELECT @@SQL_MODE
110813 15:39:19 315 Query SELECT @@SQL_MODE
110813 15:44:08 325 Connect test_user@192.168.14.129 on test
326 Connect test_user@192.168.14.129 on test
327 Connect test_user@192.168.14.129 on test
Slave2 mysql.log
[mysql@prx3 mysql]$ tail -f log/mysql.log
110813 15:35:08 305 Connect test_user@192.168.14.129 on test
306 Connect test_user@192.168.14.129 on test
307 Connect test_user@192.168.14.129 on test
110813 15:35:31 304 Query SELECT @@sql_mode
304 Query SELECT @@sql_mode
110813 15:35:44 304 Query SELECT @@SQL_MODE
110813 15:35:46 304 Query SELECT * FROM t_message t
110813 15:37:18 308 Connect test_user@192.168.14.129 on test
309 Connect test_user@192.168.14.129 on test
310 Connect test_user@192.168.14.129 on test
110813 15:38:21 8 Query BEGIN
8 Query insert into t_message values(1, 'c1')
8 Query COMMIT /* implicit, from Xid_log_event */
110813 15:38:50 311 Connect test_user@192.168.14.129 on test
312 Connect test_user@192.168.14.129 on test
313 Connect test_user@192.168.14.129 on test
110813 15:38:58 304 Query SELECT @@SQL_MODE
8 Query BEGIN
8 Query insert into t_user values(8, 'n8', 'p8')
8 Query COMMIT /* implicit, from Xid_log_event */
110813 15:39:08 304 Query select * from t_user
110813 15:39:19 304 Query select * from t_message
110813 15:44:08 314 Connect test_user@192.168.14.129 on test
315 Connect test_user@192.168.14.129 on test
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