溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎樣實(shí)現(xiàn)Mycat讀寫(xiě)分離與負(fù)載均衡

發(fā)布時(shí)間:2021-11-16 13:54:56 來(lái)源:億速云 閱讀:219 作者:柒染 欄目:MySQL數(shù)據(jù)庫(kù)

怎樣實(shí)現(xiàn)Mycat讀寫(xiě)分離與負(fù)載均衡,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

我們來(lái)學(xué)習(xí)讀寫(xiě)分離與負(fù)載均衡
先看一下balance 屬性

  1. 負(fù)載均衡類(lèi)型,目前的取值有3種:
    1. balance="0", 
    不開(kāi)啟讀寫(xiě)分離機(jī)制,所有讀操作都發(fā)送到當(dāng)前可用的writeHost上。

    2. balance="1",全部的readHoststand by writeHost參與select語(yǔ)句的負(fù)載均衡,簡(jiǎn)單的說(shuō),當(dāng)雙主雙從模式(M1->S1,M2->S2,并且M1 M2互為主備),正常情況下,M2,S1,S2都參與select語(yǔ)句的負(fù)載均衡。

    3. balance="2",所有讀操作都隨機(jī)的在writeHost、readhost上分發(fā)。

    4. balance="3",所有讀請(qǐng)求隨機(jī)的分發(fā)到wiriterHost對(duì)應(yīng)的readhost執(zhí)行,writerHost不負(fù)擔(dān)讀壓力,注意balance=3只在1.4及其以后版本有,1.3沒(méi)有。



writeType屬性

  1. 負(fù)載均衡類(lèi)型,目前的取值有3種:

  2. 1. writeType="0", 所有寫(xiě)操作發(fā)送到配置的第一個(gè)writeHost,第一個(gè)掛了切到還生存的第二個(gè)writeHost,重新啟動(dòng)后已切換后的為準(zhǔn),切換記錄在配置文件中:dnindex.properties .

  3. 2. writeType="1",所有寫(xiě)操作都隨機(jī)的發(fā)送到配置的writeHost。 

  4. 3. writeType="2",沒(méi)實(shí)現(xiàn)。


怎樣實(shí)現(xiàn)Mycat讀寫(xiě)分離與負(fù)載均衡
怎樣實(shí)現(xiàn)Mycat讀寫(xiě)分離與負(fù)載均衡


所以我們根據(jù)第二種方式做負(fù)載均衡
  1. <dataNode name="dn1" dataHost="localhost1" database="examdb" />

  2.         <dataNode name="dn2" dataHost="localhost2" database="examdb" />

  3.         <dataNode name="dn3" dataHost="localhost3" database="examdb" />




  4.         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"

  5.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  6.                 <heartbeat>select user()</heartbeat>

  7.                 <!-- can have multi write hosts -->

  8.                 <writeHost host="hostM1" url="192.168.6.121:3306" user="root"

  9.                                    password="ESBecs00">

  10.                         <!-- can have multi read hosts -->

  11.                 </writeHost>

  12.                 


  13.                 <writeHost host="hostS1" url="192.168.6.121:3307" user="root"

  14.                                    password="ESBecs00">

  15.                         <!-- can have multi read hosts -->

  16.                 </writeHost>

  17.         </dataHost>

  18.         


  19.         <dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"

  20.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  21.                 <heartbeat>select user()</heartbeat>

  22.                 <!-- can have multi write hosts -->

  23.                 <writeHost host="hostM1" url="192.168.6.120:3306" user="root"

  24.                                    password="ESBecs00">

  25.                         <!-- can have multi read hosts -->

  26.                 </writeHost>

  27.                 

  28.                 <writeHost host="hostS1" url="192.168.6.120:3307" user="root"

  29.                                    password="ESBecs00">

  30.                         <!-- can have multi read hosts -->

  31.                 </writeHost>


  32.         </dataHost>


  33.         <dataHost name="localhost3" maxCon="1000" minCon="10" balance="2"

  34.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  35.                 <heartbeat>select user()</heartbeat>

  36.                 <!-- can have multi write hosts -->

  37.                 <writeHost host="hostM1" url="192.168.6.119:3306" user="root"

  38.                                    password="ESBecs00">

  39.                         <!-- can have multi read hosts -->

  40.                 </writeHost>

  41.                 

  42.                 <writeHost host="hostM1" url="192.168.6.119:3307" user="root"

  43.                                    password="ESBecs00">

  44.                         <!-- can have multi read hosts -->

  45.                 </writeHost>


  46.         </dataHost>

測(cè)試
  1.   #!/usr/bin/bash
    for((i=1;i<=1000;i++))
    do
    mysql -uroot -p123456 -P8066 -h227.0.0.1  -e "select name from company" TESTDB;
    done
       

  2. root@localhost logs]# sort test-blance.log |more |uniq -c 

  3.      30 this is 119
         17 this is 119:3307
         28 this is 120
         23 this is 120:3307
         36 this is 121
         26 this is 121:3307

如上:6臺(tái)機(jī)器全部都有讀到,讀負(fù)載均衡成功

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI