一個完整的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):
以上是proxy提供給client的連線配置
<dbServerList> <dbServer name="server1"> <!-- PoolableObjectFactory实现类 --> <factoryConfig class="com.meidusa.amoeba.mysql <a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a> .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 <a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.Net</a> .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資料庫伺服器設定信息,具體配置見註解很明白了
最後配置讀寫分離策略:
從以上配置不然發現,寫操作路由到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搭建和實戰得以生效
以上是mysql讀寫分離實戰-建構高效能web的程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!