近期開發的系統中使用MySql作為資料庫,由於資料涉及Money,所以不得不謹慎。同時,用戶對最大訪問量也提出了要求。為了避免Mysql成為效能瓶頸並具備很好的容錯能力,特此實現主從熱備和讀寫分離。在此簡做紀要,以備日後所用!
一、設定主從
條件:兩台PC,IP分別為192.168.168.253,192.168.168.251。兩台PC上的Mysql版本為5.0。253上的Mysql為Master,251上的Mysql為Slave。
1、主資料庫伺服器設定
進入主資料庫伺服器安裝目錄,開啟my.ini,在檔案結尾增加以下設定:
#数据库ID号, 为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值; server-id = 1 #启用二进制日志; log-bin=mysql-bin #需要同步的二进制数据库名; binlog-do-db=minishop #不同步的二进制数据库名,如果不设置可以将其注释掉; binlog-ignore-db=information_schema binlog-ignore-db=mysql binlog-ignore-db=personalsite binlog-ignore-db=test #设定生成的log文件名; log-bin="D:/Database/materlog" #把更新的记录写到二进制文件中; log-slave-updates
儲存檔案。重啟Mysql服務。
進入從資料庫伺服器安裝目錄,開啟my.ini,在檔案末端增加以下設定:
#如果需要增加Slave库则,此id往后顺延; server-id = 2 log-bin=mysql-bin #主库host master-host = 192.168.168.253 #在主数据库服务器中建立的用于该从服务器备份使用的用户 master-user = forslave master-password = ****** master-port = 3306 #如果发现主服务器断线,重新连接的时间差; master-connect-retry=60 #不需要备份的数据库; replicate-ignore-db=mysql #需要备份的数据库 replicate-do-db=minishop log-slave-update
儲存檔案。重啟Mysql服務。
進入主資料庫伺服器,建立上面備份使用的使用者名稱和密碼,同時授權replication slave,super和reload
mysql>grant replication slave,super,reload on minishop.* to forslave@192.168.168.251 identified by '******';
進入從資料庫伺服器,啟動Slave。
mysql>slave start; mysql>show slave status\G;
測試:進入主資料庫伺服器,在Minishop中某張表中插入一條數據,然後到從資料庫伺服器中查看是否含有剛插入的資料。完畢!
備註:1)執行配置後的主資料庫伺服器先於從資料庫伺服器,這樣執行從資料庫伺服器時,主庫的 File 和 Position 與 從庫的上方設定Master_Log_File、Read_Master_Log_Pos 就會一致。否則,可能出現不一致的情況。這也可以透過命令調整。
2)如果發現主從複製失敗時,可以先關閉從資料庫伺服器,然後刪除從資料庫伺服器中data目錄下relay-log.info,hosname-relay-bin*,master.info等文件,重啟從伺服器。
二、讀寫分離配置
本想採用Mysql Proxy來實現讀寫分離,奈何其使用的lua腳本著實讓人頭痛,最後決定採用國人開發的開源資料庫代理中間件Amoeba。使用Amoeba,只需要簡單的xml配置,就可以輕鬆實現讀寫分離。
Amoeba處於應用程式和資料庫伺服器之間,充當一個中間代理層。其支援負載平衡、高可用性、Query過濾、讀寫分離、可路由相關的query到目標資料庫、可並發請求多台資料庫合併結果。功能很強大。
Amoeba預設的連接埠為8066,實作了Mysql協定。應用程式中只需要修改一個資料庫連線就可以實現採用Amoeba來代理資料庫存取。例如:java應用程式中,假如你原來的jdbc連接字串為:jdbc:mysql://192.168.168.42:3306/minishop,那麼現在,你想使用Amoeba作為資料庫存取代理,則只需將上面連接字串改為如下(假如Amoeba所在機子IP為192.168.168.88):jdbc:mysql://192.168.168.88:8066/minishop。 Amoeba透明性做的很讚。
主要還是配置Amoeda,但是配置也是相當的簡單。基本只需要設定兩個檔案:conf\dbServers.xml和conf\amoeba.xml。配置中各項的意義,可以參考amoeda中文指南,這裡不做過多解釋。僅記錄下配置。
dbServers.xml主要設定
<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">minishop</property> <!-- mysql user --> <property name="user">chenjie</property> <!-- mysql password --> <property name="password">chenjie</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> <dbServer name="master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.168.253</property> </factoryConfig> </dbServer> <dbServer name="slave1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.168.119</property> </factoryConfig> </dbServer> <dbServer name="slave2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.168.251</property> </factoryConfig> </dbServer> <dbServer name="multiPool" 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設定:
<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"> <!-- port --> <property name="port">8066</property> <!-- bind ipAddress --> <property name="ipAddress">192.168.168.253</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"> <property name="user">chenjie</property> <property name="password">chenjie</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> <property name="readPool">multiPool</property> <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
至此,Mysql主從熱備和讀寫分離配置完畢。但是,具體應用在生產環境究竟如何,還有待測試考察。後來測試一主多從,又加入了一台Mysql從資料庫伺服器,這就是為什麼上面amoeba配置中多了一個IP為119的原因。
以上是Mysql-主多從和讀寫分離配置的程式碼範例簡介的詳細內容。更多資訊請關注PHP中文網其他相關文章!