讀寫分離(負載平衡)(讀寫分離確定要用到主從複製) 若是數據庫壓力很大,一臺機器支撐不了,那麼能夠用mysql複製實現多臺機器同步,將數據庫的壓力分散。 分表不能解決併發量大的問題。 Sql語句發過來,首先負載均衡器進行判斷,若是是insetdeleteupdate語句就發給主數據庫, Master是主服務器(insert,delete,update),若是是select語句就發給Slavel(多個從數據庫,執行select語句),負載平衡器會定時查看多個從服務器的狀態,根據cpu和內存判斷哪一個從數據庫處於空閒狀態。 主服務器的增刪改要同步到從服務器,從數據庫每隔必定時間就根據主服務的日誌進行更新操做。
要實現這種方式,須要程序特別設計,寫都操做master,讀都操做 slave,給程序開發帶來了額外負擔。固然目前已經有中間件來實現這個 代理,對程 序來讀寫哪些數據庫是透明的。官方有個mysql-proxy,可是 仍是alpha版本的。新浪有個amobe for mysql,也可達到這個目的,結構 以下
replications是主從複製mysql
一個完整的mysql讀寫分離環境包括如下幾個部分: 應用程序client database proxy database集羣 在本次實戰中,應用程序client基於c3p0鏈接後端的database proxy。database proxy負責管理client實際訪問database的路由策略,採用開源框架amoeba。database集羣採用mysql的master-slave的replication方案。整個環境的結構圖如上所示:
實戰步驟與詳解 一.搭建mysql的master-slave環境 1)分別在host1(10.20.147.110)和host2(10.20.147.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的具體數據庫 而後添加專門用於replication的用戶: mysql> GRANT REPLICATION SLAVE ON *.* TO repl@10.20.147.111 IDENTIFIED BY '111111'; 重啓mysql,使得配置生效: /etc/init.d/mysqld restart 最後查看master狀態:
3)配置slave 首先編輯/etc/my.cnf,添加如下配置: server-id=2 #slave的標示 配置生效後,配置與master的鏈接: mysql> CHANGE MASTER TO -> MASTER_HOST='10.20.147.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;
4)驗證master-slave搭建生效 經過查看slave機的log(/var/log/mysqld.log): 100703 10:51:42 [Note] Slave I/O thread: connected to master 'repl@10.20.147.110:3306', replication started in log 'mysql-bin.000003' at position 161261 如看到以上信息則證實搭建成功,若是有問題也可經過此log找緣由
二.搭建database proxy 這次實戰中database proxy採用amoeba ,它的相關信息能夠查閱官方文檔,不在此詳述 1)安裝amoeba 下載amoeba(1.2.0-GA)後解壓到本地(D:\openSource\amoeba-mysql-1.2.0-GA),即完成安裝 2)配置amoeba 先配置proxy鏈接和與各後端mysql服務器鏈接信息(D:\openSource\amoeba-mysql-1.2.0-GA\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.20.147.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.20.147.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:\openSource\amoeba-mysql-1.2.0-GA\amoeba.bat便可: log4j:WARN log4j config load completed from file:D:\openSource\amoeba-mysql-1.2.0-GA\conf\log4j.xml log4j:WARN ip access config load completed from file:D:\openSource\amoeba-mysql-1.2.0-GA/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 zone_by_id(id,name) values(20003,'name_20003') 經過查看master機上的日誌/var/lib/mysql/mysql_log.log:
100703 11:58:42 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 zone_by_id(id,name) values(?,?) 1 Prepare [2] insert into zone_by_id(id,name) values(?,?) 1 Execute [2] insert into zone_by_id(id,name) values(20003,'name_20003') 得知寫操做發生在master機上 經過查看slave機上的日誌/var/lib/mysql/mysql_log.log: 100703 11:58:42 2 Query insert into zone_by_id(id,name) values(20003,'name_20003') 得知slave同步執行了這條語句 而後查一條數據:select t.name from zone_by_id t where t.id = 20003 經過查看slave機上的日誌/var/lib/mysql/mysql_log.log: 100703 12:02:00 33 Query set names latin1 33 Prepare [1] select t.name from zone_by_id t where t.id = ? 33 Prepare [2] select t.name from zone_by_id t where t.id = ? 33 Execute [2] select t.name from zone_by_id t where t.id = 20003 得知讀操做發生在slave機上 而且經過查看slave機上的日誌/var/lib/mysql/mysql_log.log發現這條語句沒在master上執行 經過以上驗證得知簡單的master-slave搭建和實戰得以生效