Mycat故障轉移之writetype與switchtype


轉載於:http://blog.itpub.net/29096438/viewspace-2137743/ 

1.配置文件如下:

  1. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
  2.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  3.                 <heartbeat>select user()</heartbeat>
  4.                 <!-- can have multi write hosts -->
  5.                 <writeHost host="hostM1" url="192.168.6.121:3306" user="root"
  6.                                    password="ESBecs00">
  7.                         <!-- can have multi read hosts -->
  8.                 </writeHost>
  9.                 
  10.                 <writeHost host="hostS1" url="192.168.6.121:3307" user="root"
  11.                                    password="ESBecs00">
  12.                         <!-- can have multi read hosts -->
  13.                 </writeHost>
  14.         </dataHost>

writeType屬性
負載均衡類型,目前的取值有3種:
1. writeType="0", 所有寫操作發送到配置的第一個writeHost,第一個掛了切到還生存的第二個writeHost,重新啟動后已切換后的為准,切換記錄在配置文件中:dnindex.properties .
2. writeType="1",所有寫操作都隨機的發送到配置的writeHost。 
3. writeType="2",沒實現。

switchtype屬性如下(控制自動切換的 )


2 模擬數據庫down機

  1. 2.1 主庫down機
  1. [root@localhost ~]# /etc/init.d/mysql stop
    Shutting down MySQL....                                    [確定]

  1. 2.2 mycat插入數據
  1. mysql> insert into zs values (2),(3);
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0

  1. 2.3 查看備庫
  1. mysql> select * from zs;
    +------+
    | id   |
    +------+
    |    1 |
    |    2 |
    |    3 |
    +------+
    3 rows in set (0.00 sec)
    數據發送至備庫,可見已切換成功

  1. 2.4 主庫恢復
  1. [root@localhost ~]# /etc/init.d/mysql start
    Starting MySQL.                                            [確定]

  1. 2.5 再次插入數據,並查看從和主
  1. mycat> insert into zs values (4),(5);
  2. 從上:mysql> select * from zs;
    +------+
    | id   |
    +------+
    |    1 |
    |    2 |
    |    3 |
    |    4 |
    |    5 |
    +------+
  3. 主上:mysql> select * from zs;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)

  4. 通過上面的步驟可以發現,主庫down掉后 就以從庫為准了,就算主庫恢復了,還是以從庫為准,所以搭個雙主是最好的解決辦法
  5. 可以看下切換的記錄文件
  6. [root@localhost conf]# more dnindex.properties 
    #update
    #Fri Apr 21 13:40:09 CST 2017
    localhost3=0
    localhost2=0
    localhost1=1  ###使用第二個writehost

  7. 將新的主庫down機 再恢復,又切換回原主庫了
  8. [root@localhost conf]# more dnindex.properties 
    #update
    #Fri Apr 21 13:56:09 CST 2017
    localhost3=0
    localhost2=0
    localhost1=0  ###使用第一個writehost
    #這0和1代表的就是寫入時使用第幾個writehost的意思了
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM