利用mycat读写分离
主库192.168.56.101,备库192.168.56.25.
3个数据库db1,db2,db3.主从同步见之前数据库目录。
修改schema.xml
balance="0"
0:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上
1:全部的readHost与stand by writeHost参与select语句的负载均衡,
2:所有读操作都随机在writeHost、readHost上分发
3:所有读请求随机分发到writeHost对应的readHost执行,writeHost不负担读压力
writeType="0"
0:所有写操作发送到配置的第一个writeHost,当第一个writeHost宕机时,切换到第二个writeHost,重新启动后以切换后的为准,切换记录在配置文件:dnindex.properties中
1:所有写操作都随发送到配置的writeHost
switchType:切换方式
-1:不自动切换
1:自动切换(默认)
2:基于MySql主从同步的状态来决定是否切换
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="localhost:3306" user="root"
password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.56.25:3306" user="root" password="123456" />
</writeHost>
<writeHost host="hostS2" url="192.168.56.25:3306" user="root"
password="123456" />
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
关闭mycat
./mycat stop
开启mycat
./mycat start
mysql -uroot -p123456 -P8066 -h127.0.0.1 -DTESTDB
查看会写到哪个库
explain insert into travelrecord(id,user_id,traveldate,fee,days) values(101,'lmkzxh',20160101,100,10);
insert into travelrecord(id,user_id,traveldate,fee,days) values(101,'lmkzxh',20160101,100,10);
然后在从mysql看已经同步了。
more /root/mycat/logs/mycat.log 可以看到是写入在主mysql。
select * from travelrecord;
more /root/mycat/logs/mycat.log 可以看到是从备mysql。
关闭主mysql服务
service mysqld stop
写入数据成功,说明已经切换。
insert into travelrecord(id,user_id,traveldate,fee,days) values(1011,'ok',20160101,100,10);