生活随笔
收集整理的這篇文章主要介紹了
Mysql一主多从和读写分离配置简记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mysql一主多從和讀寫分離配置簡記
標簽:?mysql數據庫服務器class數據庫servermanager 2012-05-30 16:44?14981人閱讀? ?分類: ? 數據庫
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
近期開發的系統中使用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,在文件末尾增加如下配置:
?
[java]?view plain
?copy #數據庫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,在文件末尾增加如下配置:
?
[java]?view plain
?copy #如果需要增加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
?
[java]?view plain
?copy mysql>grant?replication?slave,super,reload?on?minishop.*?to?forslave@192.168.168.251?identified?by?'******';?? 進入從數據庫服務器,啟動Slave。
[java]?view plain
?copy 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主要配置
?
[html]?view plain
?copy <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>????????????????????????????????????????????<property?name="port">3306</property>????????????????????????????????????????<property?name="schema">minishop</property>????????????????????????????????????????<property?name="user">chenjie</property>????????????????????????????????????????<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>??????????????????????????<property?name="ipAddress">192.168.168.253</property>??????????</factoryConfig>??????</dbServer>????????<dbServer?name="slave1"??parent="abstractServer">??????????<factoryConfig>??????????????????????????<property?name="ipAddress">192.168.168.119</property>??????????</factoryConfig>??????</dbServer>????????<dbServer?name="slave2"??parent="abstractServer">??????????<factoryConfig>??????????????????????????<property?name="ipAddress">192.168.168.251</property>????????????</factoryConfig>??????</dbServer>????????????<dbServer?name="multiPool"?virtual="true">??????????<poolConfig?class="com.meidusa.amoeba.server.MultipleServerPool">??????????????????????????<property?name="loadbalance">1</property>????????????????????????????????????????<property?name="poolNames">slave1,slave2</property>??????????</poolConfig>??????</dbServer>????????????</amoeba:dbServers>?? amoeba.xml配置:
?
?
[html]?view plain
?copy <amoeba:configuration?xmlns:amoeba="http://amoeba.meidusa.com/">????????<proxy>????????????????????????<service?name="Amoeba?for?Mysql"?class="com.meidusa.amoeba.net.ServerableConnectionManager">??????????????????????????<property?name="port">8066</property>??????????????????????????????????????????<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>????????????????????????????<service?name="Amoeba?Monitor?Server"?class="com.meidusa.amoeba.monitor.MonitorServer">??????????????????????????<!--??default?value:?random?number??????????????<property?name="port">9066</property>??????????????-->??????????????????????????<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">??????????????????????????<property?name="readThreadPoolSize">20</property>????????????????????????????????????????<property?name="clientSideThreadPoolSize">30</property>????????????????????????????????????????<property?name="serverSideThreadPoolSize">30</property>????????????????????????????????????????<property?name="statementCacheSize">500</property>????????????????????????????????????????<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>????????????????????<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的原因。
轉載于:https://www.cnblogs.com/firstdream/p/5222664.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Mysql一主多从和读写分离配置简记的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。