下载:

wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
tar zvxf  Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz && rm -rf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

配置:

/etc/profile新增配置

export PATH=/data/mycat/bin:
source /etc/profile

/opt/mycat/conf/server.xml

--定义读写用户
        <user name="test">
                <property name="password">test</property>
                <property name="schemas">testdb</property>
        </user>
--定义只读用户
        <user name="user">
                <property name="password">user</property>
                <property name="schemas">testdb</property>
                <property name="readOnly">true</property>
        </user>

/opt/mycat/conf/schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">


    <schema name="testdb" checkSQLschema="false" sqlMaxLimit="100">
        <!-- 取模分片 -->
        <table name="student" primaryKey="id" dataNode="dn1,dn2,dn3" rule="mod-long" />
        <table name="grade" primaryKey="id" dataNode="dn4"/>
    </schema>

    <!-- 申明节点对应的database -->
    <dataNode name="dn1" dataHost="172.16.0.8" database="beijing" />
    <dataNode name="dn2" dataHost="172.16.0.8" database="shanghai" />
    <dataNode name="dn3" dataHost="172.16.0.8" database="guangzhou" />
    <dataNode name="dn4" dataHost="172.16.0.8" database="basic" />

    <dataHost name="172.16.0.8" maxCon="1000" minCon="10" balance="0"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <!-- 可读写的数据库实例 -->
        <writeHost host="172.16.0.8" url="172.16.0.8:3306" user="root" password="vY7JU_EgCrY_kSi">
        </writeHost>
    </dataHost>

</mycat:schema>

测试mycat

create database beijing;
use beijing;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
);

create database shanghai;
use shanghai;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
);

create database guangzhou;
use guangzhou;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
);

create database basic;
use basic;
create table grade(
    id    int primary key,
    name   varchar(8) not null
);
insert into grade (id,name) values (1,'一年级');
insert into grade (id,name) values (2,'二年级');
insert into grade (id,name) values (3,'三年级');
insert into student (id,name,grade) values (1,'张三',2);
insert into student (id,name,grade) values (2,'李四',1);
insert into student (id,name,grade) values (3,'王五',3);
insert into student (id,name,grade) values (4,'甲',3);
insert into student (id,name,grade) values (5,'乙',2);
insert into student (id,name,grade) values (6,'丙',1);

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code