參考:http://www.linuxidc.com/Linux/2015-10/124115.htmhtml
一個完整的MySQL讀寫分離環境包括如下幾個部分:mysql
在本次實戰中,應用程序client基於c3p0鏈接後端的database proxy。database proxy負責管理client實際訪問database的路由策略,MySQL Proxy有一項強大功能是實現「讀寫分離」,基本原理是讓主數據庫處理寫方面事務,讓從庫處理SELECT查詢。Amoeba for MySQL是一款優秀的中間件軟件,一樣能夠實現讀寫分離,負載均衡等功能,而且穩定性也高於MySQL Proxy,這裏採用開源框架Amoeba for MySQL,其使用指南。database集羣採用mysql的master-slave的replication方案。整個環境的結構圖以下所示:linux
實戰步驟與詳解sql
一.搭建mysql的master-slave環境數據庫
1)分別在host1(10.10.148.110)和host2(10.10.148.111)上安裝mysql(5.0.45),具體安裝方法可見官方文檔後端
2)配置master服務器
首先編輯/etc/my.cnf,添加如下配置:負載均衡
log-bin=mysql-bin #slave會基於此log-bin來作replication
server-id=1 #master的標示
binlog-do-db = amoeba_study #用於master-slave的具體數據庫框架
3)在Master MySQL上建立一個用戶‘repl’,並容許其餘Slave服務器能夠經過遠程訪問Master,經過該用戶讀取二進制日誌,實現數據同步socket
mysql> create user repl; //建立新用戶
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@10.10.148.111 IDENTIFIED BY '111111';
附加說明 :
執行 mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'mysql';
192.168.0.%,這個配置是指明repl用戶所在服務器,這裏%是通配符,表示192.168.0.0-192.168.0.255的Server均可以以repl用戶登錄主服務器。固然你也能夠指定固定Ip。
4)重啓mysql,使得配置生效:
/etc/init.d/mysqld restart
最後查看master狀態:
5)配置slave
首先編輯/etc/my.cnf,添加如下配置:
server-id=2 #slave的標示
配置生效後,配置與master的鏈接:
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.10.148.110',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='111111',
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=161261;
其中MASTER_HOST是master機的ip,MASTER_USER和MASTER_PASSWORD就是咱們剛纔在master上添加的用戶,MASTER_LOG_FILE和MASTER_LOG_POS對應與master status裏的信息
最後啓動slave:
mysql> start slave;
6)驗證master-slave搭建生效
經過查看slave機的log(/var/log/mysqld.log):
100703 07:31:12 [Note] Slave I/O thread: connected to master 'repl@10.10.148.110:3306', replication started in log 'mysql-bin.000003' at position 161261
如看到以上信息則證實搭建成功,若是有問題也可經過此log找緣由
二.搭建database proxy
這次實戰中database proxy採用amoeba ,它的相關信息能夠查閱官方文檔,不在此詳述,下面說說Windows for amoeba的安裝及配置
1)安裝amoeba
下載1 連接: https://sourceforge.net/projects/amoeba/?source=typ_redirect
or
下載2 amoeba-mysql-binary-2.2.0.tar.gz 後解壓到(D:/amoeba-mysql-binary-2.2.0),即完成安裝
2)配置amoeba
先配置proxy鏈接和與各後端mysql服務器鏈接信息(D:/amoeba-mysql-binary-2.2.0/conf/amoeba.xml):
<server> <!-- proxy server綁定的端口 --> <property name="port">8066</property> <!-- proxy server綁定的IP --> <!-- <property name="ipAddress">127.0.0.1</property> --> <!-- 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> <!-- socket Send and receive BufferSize(unit:K) --> <property name="netBufferSize">128</property> <!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). --> <property name="tcpNoDelay">true</property> <!-- 對外驗證的用戶名 --> <property name="user">root</property> <!-- 對外驗證的密碼 --> <property name="password">root</property> </server>
以上是proxy提供給client的鏈接配置
<dbServerList> <dbServer name="server1"> <!-- PoolableObjectFactory實現類 --> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="manager">defaultManager</property> <!-- 真實mysql數據庫端口 --> <property name="port">3306</property> <!-- 真實mysql數據庫IP --> <property name="ipAddress">10.10.148.110</property> <property name="schema">amoeba_study</property> <!-- 用於登錄mysql的用戶名 --> <property name="user">root</property> <!-- 用於登錄mysql的密碼 --> <property name="password"></property> </factoryConfig> <!-- ObjectPool實現類 --> <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> <property name="maxActive">200</property> <property name="maxIdle">200</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> <dbServer name="server2"> <!-- PoolableObjectFactory實現類 --> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="manager">defaultManager</property> <!-- 真實mysql數據庫端口 --> <property name="port">3306</property> <!-- 真實mysql數據庫IP --> <property name="ipAddress">10.10.148.111</property> <property name="schema">amoeba_study</property> <!-- 用於登錄mysql的用戶名 --> <property name="user">root</property> <!-- 用於登錄mysql的密碼 --> <property name="password"></property> </factoryConfig> <!-- ObjectPool實現類 --> <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> <property name="maxActive">200</property> <property name="maxIdle">200</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> </dbServerList>
以上是proxy與後端各mysql數據庫服務器配置信息,具體配置見註釋很明白了
最後配置讀寫分離策略:
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> <property name="LRUMapSize">1500</property> <property name="defaultPool">server1</property> <property name="writePool">server1</property> <property name="readPool">server2</property> <property name="needParse">true</property> </queryRouter>
從以上配置否則發現,寫操做路由到server1(master),讀操做路由到server2(slave)
3)啓動amoeba
在命令行裏運行D:/amoeba-mysql-binary-2.2.0/amoeba.bat便可:
log4j:WARN log4j config load completed from file:D:/amoeba-mysql-binary-2.2.0/conf/log4j.xml
log4j:WARN ip access config load completed from file:D:/amoeba-mysql-binary-2.2.0/conf/access_list.conf
2010-07-03 09:55:33,821 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.
三.client端調用與測試
1)編寫client調用程序
具體程序細節就不詳述了,只是一個最普通的基於mysql driver的jdbc的數據庫操做程序
2)配置數據庫鏈接
本client基於c3p0,具體數據源配置以下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:8066/amoeba_study" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="1" /> <property name="maxIdleTime" value="1800" /> <property name="acquireIncrement" value="1" /> <property name="maxStatements" value="0" /> <property name="initialPoolSize" value="1" /> <property name="idleConnectionTestPeriod" value="1800" /> <property name="acquireRetryAttempts" value="6" /> <property name="acquireRetryDelay" value="1000" /> <property name="breakAfterAcquireFailure" value="false" /> <property name="testConnectionOnCheckout" value="true" /> <property name="testConnectionOnCheckin" value="false" /> </bean>
值得注意是,client端只需連到proxy,與實際的數據庫沒有任何關係,所以jdbcUrl、user、password配置都對應於amoeba暴露出來的配置信息
3)調用與測試
首先插入一條數據:insert into test(id,name) values(10,'test10')
經過查看master機上的日誌/var/lib/mysql/mysql_log.log:
1 Query set names latin1 1 Query SET NAMES latin1 1 Query SET character_set_results = NULL
1 Query SHOW VARIABLES
1 Query SHOW COLLATION
1 Query SET autocommit=1
1 Query SET sql_mode='STRICT_TRANS_TABLES' 1 Query SHOW VARIABLES LIKE 'tx_isolation' 1 Query SHOW FULL TABLES FROM `amoeba_study` LIKE 'PROBABLYNOT' 1 Prepare [1] insert into test(id,name) values(?,?) 1 Prepare [2] insert into test(id,name) values(?,?) 1 Execute [2] insert into test(id,name) values(10,'test10')
得知寫操做發生在master機上
經過查看slave機上的日誌/var/lib/mysql/mysql_log.log:
112313 7:38:12 2 Query insert into test(id,name) values(10,'test10')
得知slave同步執行了這條語句
而後查一條數據:select t.name from test t where t.id = 10
經過查看slave機上的日誌/var/lib/mysql/mysql_log.log:
33 Query set names latin1 33 Prepare [1] select t.name from test t where t.id = ? 33 Prepare [2] select t.name from test t where t.id = ? 33 Execute [2] select t.name from test t where t.id = 10
得知讀操做發生在slave機上
而且經過查看slave機上的日誌/var/lib/mysql/mysql_log.log發現這條語句沒在master上執行
經過以上驗證得知簡單的master-slave搭建和實戰得以生效