纯笔记, 不翻译,请绕道。。。
官网:http://www.mycat.org.cn/
环境:linux、java8
安装:
一、下载
地址:http://dl.mycat.org.cn/1.6.7.6/Mycat-server-1.6.7.6-release-20201102172919-unix.tar.gz
二、解压,位置:/usr/local/Mycat
三、授权
1、执行:vi /etc/profile
2、结束加入:MYCAT_HOME=/usr/local/Mycat
3、保存后执行:source /etc/profile
4、可执行:chmod +x /usr/local/Mycat/bin
5、启动:../bin/mycat start 或 ../bin/mycat console
分库分表
环境:2个mysql实例(docker),分别为3307、3308端口
1、mysql实例分别创建数据库db0和表tb0
CREATE DATABASE `db0` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE db0;
CREATE TABLE IF NOT EXISTS `tb0`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB;
修改conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn3307">
<table name="tb0" dataNode="dn3307,dn3308" rule="xxx"/>
</schema>
<dataNode name="dn3307" dataHost="localhost1" database="db0" />
<dataNode name="dn3308" dataHost="localhost2" database="db0" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="127.0.0.1:3307" user="root" password="12345678">
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="127.0.0.1:3308" user="root" password="12345678">
</writeHost>
</dataHost>
</mycat:schema>
读写分离
<?xml version="1.0" encoding="utf-8"?>
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
<!-- 可指定表 -->
</schema>
<!-- hello:真实数据库 -->
<dataNode name="dn1" dataHost="localhost1" database="hello"/>
<!-- balance=1:writeType:0, 写主机,读从机 -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- 写 -->
<writeHost host="hostM1" url="localhost:3306" user="root" password="12345678">
<!-- 读 -->
<readHost host="hostS1" url="localhost:3307" user="root" password="12345678"/>
</writeHost>
</dataHost>
</mycat:schema>
ER表
<table name="o" dataNode="dn1,dn2" rule="ddd">
<childTable name="od" primaryKey="id" joinKey="oid" parentKey="id"/>
</table>
或
<table name="f" primaryKey="id" dataNode="dn1,dn2" rule="ddd">
<childTable name="s" primaryKey="id" joinKey="pno" parentKey="no"/>
</table>
全局表
<table name="g" primaryKey="id" type="global" dataNode="dn1,dn2"/>
规则策略
网址:https://blog.csdn.net/tototuzuoquan/article/details/80362892
扩容
分别复制rule.xml,schema.xml为newRule.xml和newSchema.xml
修改newRule.xml和newSchema.xml,对应相关的节点和规则
修改:conf/migrateTables.properties
#schema1=tb1,tb2,...
#schema2=all(写all或者不写将对此schema下拆分节点变化的拆分表全部进行重新路由)
#...
#sample
#TESTDB=travelrecord,company,goods
# TESTDB是schema.xml中schema标签中的name, t0是table标签中的name
TESTDB=t0
修改bin下面的 dataMigrate.sh
RUN_CMD="$RUN_CMD -deleteTempFileDir=false
可看到日志
修改相关的newSchema.xml,加入相关的datanode和dataHost, 修改相关的newRule.xml,count=3
整理脚本:sed -i -e 's/\r$//' dataMigrate.sh
运行dataMigrate.sh即可,最后重启
缩容
同上,把数据调而已