ITPub博客

首页 > 数据库 > MySQL > 【Mycat】Mycat故障转移之writetype与switchtype

【Mycat】Mycat故障转移之writetype与switchtype

原创 MySQL 作者:小亮520cl 时间:2017-04-21 13:54:37 0 删除 编辑

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属性
  1. 负载均衡类型,目前的取值有3种:
  2. 1. writeType="0", 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
  3. 2. writeType="1",所有写操作都随机的发送到配置的writeHost。 
  4. 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的意思了

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-2137743/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126951