mycat部署实战

mycat部署实战

摘要

mycat部署实战

官方文档下载

http://pan.baidu.com/s/1dE8wP3r

mysql环境快速搭建

sudo docker run --name mysql1  -p 3316:3306 -e MYSQL_ROOT_PASSWORD=root -d daocloud.io/library/mysql:5.7.13
sudo docker run --name mysql2  -p 3326:3306 -e MYSQL_ROOT_PASSWORD=root -d daocloud.io/library/mysql:5.7.13


mysql -h127.0.0.1 -P3316 -uroot -proot
mysql -h127.0.0.1 -P3326 -uroot -proot
可以正常连接

mycat下载

下载目录
https://github.com/MyCATApache/Mycat-download/tree/master/1.6-RELEASE

wget -O mycat.tar.gz https://github.com/MyCATApache/Mycat-download/blob/master/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz?raw=true
tar  -zxvf  mycat.tar.gz

测试能否启动  cd  ./mycat  ;   ./bin/mycat console
启动   ./bin/mycat  restart

mycat 配置

用户 ( user ) conf/server.xml

<user name="root">
    <property name="password">test123</property>
    <property name="schemas">testdb</property>
    <!-- <property name="readOnly">true</property> -->
</user>

测试db配置记得删掉

逻辑库(schema) conf/schema.xml

<schema name="xenderdb" checkSQLschema="false" sqlMaxLimit="100">
</schema>

逻辑表(table) conf/schema.xml

<schema name="xenderdb" checkSQLschema="false" sqlMaxLimit="100">
    <table name="order" dataNode="dn1,dn2" rule="mod-long" />
</schema>

分片节点(dataNode) conf/schema.xml

<dataNode name="dn1" dataHost="dockerhost1" database="order_db" />            //database指的是真实的物理数据库
<dataNode name="dn2" dataHost="dockerhost2" database="order_db" />

节点主机(dataHost) conf/schema.xml

<dataHost name="dockerhost1" maxCon="1000" minCon="10" balance="0"
    writeType="0" dbType="mysql" dbDriver="native" switchType="1">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="localhost:3316" user="root" password="root">
    </writeHost>
</dataHost>
<dataHost name="dockerhost2" maxCon="1000" minCon="10" balance="0"
    writeType="0" dbType="mysql" dbDriver="native" switchType="1">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostS1" url="localhost:3326" user="root" password="root">
    </writeHost>
</dataHost>

分片规则(rule) conf/rule.xml

<tableRule name="mod-long">
    <rule>
        <columns>id</columns>
        <algorithm>mod-long</algorithm>
    </rule>
</tableRule>
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
    <!-- how many data nodes -->
    <property name="count">2</property>
</function>

全局序列号配置

conf/server.xml

<system>
    <property name="sequnceHandlerType">0</property>
</system>

0 为本地文件方式,1 为数据库方式,2 为时间戳序列方式,3 为分布式
ZK ID 生成器,4 为 zk 递增 id 生成。

conf/sequence.properties
表示使用过的历史分段,一般无特殊需要课不设置

ORDER.HISIDS=
#最小ID值
ORDER.MINID=1
#最大ID值
ORDER.MAXID=1000000000
#当前ID值
ORDER.CURID=0

./bin/mycat restart

访问逻辑库

mysql -h127.0.0.1 -P8066 -uroot -ptest123
use xenderdb;
CREATE TABLE `order` (
  `id` int(11) NOT NULL,
  `status` tinyint(4) NOT NULL,
  `price` int(11) NOT NULL,
  `create_time` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

insert into `order`(id, status, price, create_time)  values (next value for MYCATSEQ_ORDER, 0 , 10,NOW()) ;
insert into `order`(id, status, price, create_time)  values (next value for MYCATSEQ_ORDER, 1 , 123,NOW()) ;
insert into `order`(id, status, price, create_time)  values (next value for MYCATSEQ_ORDER, 1 , 333,NOW()) ;
insert into `order`(id, status, price, create_time)  values (next value for MYCATSEQ_ORDER, 2 , 234,NOW()) ;