1.配置文件如下:
-
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
-
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
-
<heartbeat>select user()</heartbeat>
-
<!-- can have multi write hosts -->
-
<writeHost host="hostM1" url="192.168.6.121:3306" user="root"
-
password="ESBecs00">
-
<!-- can have multi read hosts -->
-
</writeHost>
-
-
-
<writeHost host="hostS1" url="192.168.6.121:3307" user="root"
-
password="ESBecs00">
-
<!-- can have multi read hosts -->
-
</writeHost>
-
</dataHost>
writeType属性
-
负载均衡类型,目前的取值有3种:
-
1. writeType="0", 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
-
2. writeType="1",所有写操作都随机的发送到配置的writeHost。
-
3. writeType="2",没实现。
-
switchtype属性如下(控制自动切换的)
2 模拟数据库down机
-
2.1 主库down机
-
[root@localhost ~]# /etc/init.d/mysql stop
Shutting down MySQL.... [确定]
-
-
2.2 mycat插入数据
-
mysql> insert into zs values (2),(3);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
-
-
2.3 查看备库
-
mysql> select * from zs;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
数据发送至备库,可见已切换成功
-
-
2.4 主库恢复
-
[root@localhost ~]# /etc/init.d/mysql start
Starting MySQL. [确定]
-
2.5 再次插入数据,并查看从和主
-
mycat> insert into zs values (4),(5);
-
-
从上:mysql> select * from zs;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
-
主上:mysql> select * from zs;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
-
通过上面的步骤可以发现,主库down掉后 就以从库为准了,就算主库恢复了,还是以从库为准,所以搭个双主是最好的解决办法
-
可以看下切换的记录文件
-
[root@localhost conf]# more dnindex.properties
#update
#Fri Apr 21 13:40:09 CST 2017
localhost3=0
localhost2=0
localhost1=1 ###使用第二个writehost
-
将新的主库down机 再恢复,又切换回原主库了
-
[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/
,如需转载,请注明出处,否则将追究法律责任。