1、Mycat 環境搭建

下載mycat.linux 版本 linux

安裝vmware,centos mini版本 git

vmware centos設置固定IP:http://my.oschina.net/wjgood/blog/391552 github

安裝JDK,本身百度搜索吧 sql

安裝MySql5.6: http://my.oschina.net/wjgood/blog/665189 數據庫

注:能夠下載XSHELL、XFTP做爲遠程鏈接工具,下載HeidiSQL做爲Mysql客戶端工具 centos

修改%MycatHOME%/conf/server.xml、%MycatHOME%/conf/schema.xml 工具

重啓MyCAT,使用MySQL客戶端鏈接MyCAT,須要注意的是,默認數據端口爲8066,管理端口爲9066,在MySQL客戶端鏈接MyCAT時,注意填寫端口爲8066,用戶名/密碼根據server.xml中的配置進行填寫。 spa

 使用客戶端工具鏈接Mycat,配置schema.xmlMycat鏈接具體的數據庫。咱們的數據庫操做就使用mycat的鏈接操做。 .net

Mycat官網地址:https://github.com/MyCATApache code

Mycat文檔地址:https://github.com/MyCATApache/Mycat-doc

Mycat安裝數據庫文件:

 

數據庫序列建立

https://github.com/MyCATApache/Mycat-doc/blob/master/MyCat_In_Action_CN.doc

DROP TABLE IF EXISTS MYCAT_SEQUENCE;  
CREATE TABLE MYCAT_SEQUENCE (  name VARCHAR(50) NOT NULL,  current_value INT NOT NULL,  increment INT NOT NULL DEFAULT 100, PRIMARY KEY (name) ) ENGINE=InnoDB;

-- ----------------------------
-- Function structure for `mycat_seq_currval`
-- ----------------------------
DROP FUNCTION IF EXISTS `mycat_seq_currval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `mycat_seq_currval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET latin1
    DETERMINISTIC
BEGIN
    DECLARE retval VARCHAR(64);
    SET retval="-1,0";
    SELECT concat(CAST(current_value AS CHAR),",",CAST(increment AS CHAR) ) INTO retval FROM MYCAT_SEQUENCE  WHERE name = seq_name;
    RETURN retval ;
END
;;
DELIMITER ;

-- ----------------------------
-- Function structure for `mycat_seq_nextval`
-- ----------------------------
DROP FUNCTION IF EXISTS `mycat_seq_nextval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `mycat_seq_nextval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET latin1
    DETERMINISTIC
BEGIN
    DECLARE retval VARCHAR(64);
    DECLARE val BIGINT;
    DECLARE inc INT;
    DECLARE seq_lock INT;
    set val = -1;
    set inc = 0;
    SET seq_lock = -1;
    SELECT GET_LOCK(seq_name, 15) into seq_lock;
    if seq_lock = 1 then
      SELECT current_value + increment, increment INTO val, inc FROM MYCAT_SEQUENCE WHERE name = seq_name for update;
      if val != -1 then
          UPDATE MYCAT_SEQUENCE SET current_value = val WHERE name = seq_name;
      end if;
      SELECT RELEASE_LOCK(seq_name) into seq_lock;
    end if;
    SELECT concat(CAST((val - inc + 1) as CHAR),",",CAST(inc as CHAR)) INTO retval;
    RETURN retval;
END
;;
DELIMITER ;

-- ----------------------------
-- Function structure for `mycat_seq_setval`
-- ----------------------------
DROP FUNCTION IF EXISTS `mycat_seq_setval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `mycat_seq_setval`(seq_name VARCHAR(50), value INTEGER) RETURNS varchar(64) CHARSET latin1
    DETERMINISTIC
BEGIN
    DECLARE retval VARCHAR(64);
    DECLARE inc INT;
    SET inc = 0;
    SELECT increment INTO inc FROM MYCAT_SEQUENCE WHERE name = seq_name;
    UPDATE MYCAT_SEQUENCE SET current_value = value WHERE name = seq_name;
    SELECT concat(CAST(value as CHAR),",",CAST(inc as CHAR)) INTO retval;
    RETURN retval;
END
;;
DELIMITER ;

INSERT INTO MYCAT_SEQUENCE VALUES ('GLOBAL', 0, 100);
SELECT MYCAT_SEQ_SETVAL('GLOBAL', 1);
SELECT MYCAT_SEQ_CURRVAL('GLOBAL');
SELECT MYCAT_SEQ_NEXTVAL('GLOBAL');
相關文章
相關標籤/搜索