Mycat 安装
- 准备Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz,官网下载即可
- 上传到/usr/local下 tar –zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
- 修改配置文件 /usr/local/mycat/conf/schema.xml 、 rule.xml、server.xml
- 启动mycat /usr/local/mycat/bin/mycat start
- 连接mycat mysql -uroot -pdigdeep -h127.0.0.1 -P8066 –DTESTDB
Mycat读写分离(一主一从):
1./usr/local/mycat/conf/schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> <!-- auto sharding by id (long) --> <!--splitTableNames 启用<table name 属性使用逗号分割配置多个表,即多个表使用这个配置--> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <dataNode name="dn1" dataHost="localhost1" database="testdb" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="douzi9:3306" user="root" password="123456"> <readHost host="hostS1" url="douzi10:3306" user="root" password="123456" /> </writeHost> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost> </mycat:schema> |
2.启动mycat ./usr/local/mycat/bin/mycat console
3.验证读写查询哪台数据库服务器
通过 mysql -uroot -p123456 -h douzi9 -P 8066 登录mycat服务器命令窗:
切换数据库: Use TESTDB;
Insert into mytbl(id,name) values(1,@@hostname); Select * from mytbl; 发现读取的时候是写主机douzi9 原因是:schema.xml中的dataHost 下 balance的值需要修改:
|
我们只有一主一从时,修改为2,看是否随机读取到两台数据库服务器;
双主双从:
1./usr/local/mycat/conf/schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> <!-- auto sharding by id (long) --> <!--splitTableNames 启用<table name 属性使用逗号分割配置多个表,即多个表使用这个配置--> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <dataNode name="dn1" dataHost="localhost1" database="testdb" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="douzi9:3306" user="root" password="123456"> <readHost host="hostS1" url="douzi10:3306" user="root" password="123456" /> </writeHost> <writeHost host="hostM2" url="douzi11:3306" user="root" password="123456"> <readHost host="hostS2" url="douzi12:3306" user="root" password="123456" /> </writeHost> </dataHost> </mycat:schema> |
Balance 需要使用1- 全部的readHost 和 stand by writeHost (M1 -> S1, M2->S2 双主双从时, M1 为写, S1 S2 M2都参与select的负载均衡)
writeType = 0 表示,M1挂了,M2自动变成写主机;
switchType="1" 表示做自动切换 -1表示不切换,2表示根据mysql主从状态切换
2.验证读写查询哪台数据库服务器
通过 mysql -uroot -p123456 -h douzi9 -P 8066 登录mycat服务器命令窗:
切换数据库:Use TESTDB;
Insert into mytbl(id,name) values(1,@@hostname); Select * from mytbl; 查询不到写主机M1 douzi9 |
3.抗风险验证
- 停止M1的mysql服务 systemctl stop mysqld
- 继续mycat插入数据
Insert into mytbl(id,name) values(2,@@hostname);
依然成功
- 启动M1 systemctl start mysqld,确认M2变成写主机,M1变为 stand by writeHost。
查询不到M2 douzi11,并且数据有少许延迟;