利用mycat讀寫分離
主庫192.168.56.101,備庫192.168.56.25.
3個數據庫db1,db2,db3.主從同步見以前數據庫目錄。mysql
修改schema.xml
balance="0"
0:不開啓讀寫分離機制,全部讀操做都發送到當前可用的writeHost上
1:所有的readHost與stand by writeHost參與select語句的負載均衡,
2:全部讀操做都隨機在writeHost、readHost上分發
3:全部讀請求隨機分發到writeHost對應的readHost執行,writeHost不負擔讀壓力sql
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。ide
關閉主mysql服務
service mysqld stopurl
寫入數據成功,說明已經切換。
insert into travelrecord(id,user_id,traveldate,fee,days) values(1011,'ok',20160101,100,10);code