- 下载Mycat-server-1.6.7.1-release-20200209222254-linux.tar
下载地址:Mycat-server-1.6.7.1-release-20200209222254-linux.tar - 解压 进入bin 启动
./mycat start 启动
./mycat stop 停止
./mycat restart 重启
- mycat分库分表
server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="defaultSqlParser">druidparser</property>
<property name="mutiNodeLimitType">1</property>
<property name="sequnceHandlerType">1</property>
<!-- mycat服务的端口号 -->
<property name="serverPort">8066</property>
<!-- mycat管理的端口号 -->
<property name="managerPort">9066</property>
</system>
<!-- 配置连接Mycat的用户名, 密码, 逻辑数据库名称 -->
<user name="hikari">
<property name="password">hikari123</property>
<property name="schemas">HIKARI_CLOUD</property>
</user>
</mycat:server>
schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="HIKARI_CLOUD" checkSQLschema="false" sqlMaxLimit="100">
<table name="tb_admin" dataNode="dataNode0,dataNode1,dataNode2,dataNode3,dataNode4,dataNode5,dataNode6,dataNode7,dataNode8,dataNode9" rule="mod-long"/>
<table name="tb_user" dataNode="dataNode0,dataNode1,dataNode2,dataNode3,dataNode4,dataNode5,dataNode6,dataNode7,dataNode8,dataNode9" rule="mod-long"/>
</schema>
<dataNode name="dataNode0" dataHost="localhost" database="hikari_cloud_0" />
<dataNode name="dataNode1" dataHost="localhost" database="hikari_cloud_1" />
<dataNode name="dataNode2" dataHost="localhost" database="hikari_cloud_2" />
<dataNode name="dataNode3" dataHost="localhost" database="hikari_cloud_3" />
<dataNode name="dataNode4" dataHost="localhost" database="hikari_cloud_4" />
<dataNode name="dataNode5" dataHost="localhost" database="hikari_cloud_5" />
<dataNode name="dataNode6" dataHost="localhost" database="hikari_cloud_6" />
<dataNode name="dataNode7" dataHost="localhost" database="hikari_cloud_7" />
<dataNode name="dataNode8" dataHost="localhost" database="hikari_cloud_8" />
<dataNode name="dataNode9" dataHost="localhost" database="hikari_cloud_9" />
<dataHost name="localhost" 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="hikari"
password="hikari123">
</writeHost>
</dataHost>
</mycat:schema>
rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- dataNode数量(根据实际情况进行修改此配置) -->
<property name="count">10</property>
</function>
</mycat:rule>
- id 自增长
在同一个节点任何一个数据库上
新建MYCAT_SEQUENCE 表
CREATE TABLE MYCAT_SEQUENCE(
name VARCHAR(50) NOT NULL COMMENT 'sequence名称',
current_value INT NOT NULL COMMENT '当前value',
increment INT NOT NULL DEFAULT 100 COMMENT '增长步长',
PRIMARY KEY(name)
) ENGINE=InnoDB;
INSERT INTO MYCAT_SEQUENCE(NAME,current_value,increment) VALUES ('ADMIN', 3, 1);
sql 创建三个sql函数
DROP FUNCTION IF EXISTS mycat_seq_currval;
DELIMITER $$
CREATE FUNCTION mycat_seq_currval(seq_name VARCHAR(50))RETURNS VARCHAR(64) CHARSET 'utf8'
BEGIN
DECLARE retval VARCHAR(64);
SET retval='-999999999,NULL';
SELECT CONCAT(CAST(current_value AS CHAR),',',CAST(increment AS CHAR)) INTO retval FROM
MYCAT_SEQUENCE WHERE NAME = seq_name;
RETURN retval;
END$$
DELIMITER ;
DROP FUNCTION IF EXISTS mycat_seq_setval;
DELIMITER $$
CREATE FUNCTION mycat_seq_setval(seq_name VARCHAR(50),VALUE INTEGER) RETURNS VARCHAR(64) CHARSET 'utf8'
BEGIN
UPDATE MYCAT_SEQUENCE SET current_value = VALUE WHERE NAME = seq_name;
RETURN mycat_seq_currval(seq_name);
END$$
DELIMITER ;
DROP FUNCTION IF EXISTS mycat_seq_nextval;
DELIMITER $$
CREATE FUNCTION mycat_seq_nextval(seq_name VARCHAR(50)) RETURNS VARCHAR(64) CHARSET 'utf8'
BEGIN
UPDATE MYCAT_SEQUENCE SET current_value = current_value + increment
WHERE NAME = seq_name;
RETURN mycat_seq_currval(seq_name);
END$$
DELIMITER ;
修改
插入测试
INSERT INTO tb_admin (id,user_name) VALUES (NEXT VALUE FOR MYCATSEQ_ADMIN,"xxx")