怎样实现Mycat读写分离与负载均衡,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
我们来学习读写分离与负载均衡
先看一下balance 属性
-
负载均衡类型,目前的取值有3种:
1. balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
2. balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。
3. balance="2",所有读操作都随机的在writeHost、readhost上分发。
4. balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3没有。
writeType属性
-
负载均衡类型,目前的取值有3种:
-
1. writeType="0", 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
-
2. writeType="1",所有写操作都随机的发送到配置的writeHost。
-
3. writeType="2",没实现。
所以我们根据第二种方式做负载均衡
-
<dataNode name="dn1" dataHost="localhost1" database="examdb" />
-
<dataNode name="dn2" dataHost="localhost2" database="examdb" />
-
<dataNode name="dn3" dataHost="localhost3" database="examdb" />
-
<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>
-
-
<dataHost name="localhost2" 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.120:3306" user="root"
-
password="ESBecs00">
-
<!– can have multi read hosts –>
-
</writeHost>
-
-
<writeHost host="hostS1" url="192.168.6.120:3307" user="root"
-
password="ESBecs00">
-
<!– can have multi read hosts –>
-
</writeHost>
-
</dataHost>
-
<dataHost name="localhost3" 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.119:3306" user="root"
-
password="ESBecs00">
-
<!– can have multi read hosts –>
-
</writeHost>
-
-
<writeHost host="hostM1" url="192.168.6.119:3307" user="root"
-
password="ESBecs00">
-
<!– can have multi read hosts –>
-
</writeHost>
-
</dataHost>
测试
-
#!/usr/bin/bash
for((i=1;i<=1000;i++))
do
mysql -uroot -p123456 -P8066 -h227.0.0.1 -e "select name from company" TESTDB;
done
-
root@localhost logs]# sort test-blance.log |more |uniq -c
-
30 this is 119
17 this is 119:3307
28 this is 120
23 this is 120:3307
36 this is 121
26 this is 121:3307
如上:6台机器全部都有读到,读负载均衡成功
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/database/204339.html