准備工作:
服務器192.168.96.12,centos7, jdk,mysql5.7,mycat1.6.x,navicat
搭建步驟:
1.在服務器192.168.96.12服務器上安裝mysql數據庫,並且使用GRANT REPLICATION SLAVE ON *.* to 'root'@'%' identified by '1328174';FLUSH PRIVILEGES;命令給mysql賦權,這樣子才可遠程訪問mysql服務器. 為了保險,修改mysql配置文件,設置Mysql大小寫不敏感,vi /etc/my.cnf,在[mysqld]后面添加:lower_case_table_names=1
2.在mysql服務器上安裝三個分片數據庫;
CREATE database db1;
CREATE database db2;
CREATE database db3;
3.修改mycat的server.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<!--設置四個線程跑程序,不是必須--><system>
<property name="processors">4</property>
</system><!--配置一個root賬號,此賬號必須要能訪問mysql服務器-->
<user name="root">
<property name="password">1328174</property><!--設置一個邏輯庫,名稱叫TESTDB,它必須要與schema.xml中名稱一致-->
<property name="schemas">TESTDB</property>
</user>
</mycat:server>
4. 修改mycat的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">
<!--表名,sharding-by-intfile是分庫分表策略,詳細信息要去rule.xml中去查看,然后就會發現它是按照employee表中的sharding_id字段來進行分片,而且體如何分片,則可在conf/partition-hash-int.txt配置--><table name="employee" primaryKey="ID" dataNode="dn1,dn2,dn3" rule="sharding-by-intfile" />
</schema><!---dataNode是節點,database是真實的mysql庫->
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />
<dataHost name="localhost1" maxCon="500" minCon="10" balance="0"
dbType="mysql" dbDriver="native"><!--心跳檢測-->
<heartbeat>select user()</heartbeat><!--mysql服務器連接信息-->
<writeHost host="hostMaster" url="localhost:3306" user="root" password="1328174"></writeHost>
</dataHost>
</mycat:schema>
5. 啟動mycat
bin/startup_nowrap.sh, 查看日志可以logs文件下.
6. 登錄mycat
mysql -uroot -p1328174 -h192.168.96.12 -P8066 -DTESTDB
7. 創建employee表,然后插入數據
create table employee (id int not null primary key,name varchar(100),sharding_id int not null);
使用explain命令查看創建employee的信息:

插入數據:
insert into employee(id,name,sharding_id) values(1,'leader us',10000);
insert into employee(id,name,sharding_id) values(2, 'me',10010);
insert into employee(id,name,sharding_id) values(3, 'mycat',10000);
insert into employee(id,name,sharding_id) values(4, 'mydog',10010);
insert into employee(id,name,sharding_id) values(5, 'mimi',10020);
insert into employee(id,name,sharding_id) values(6, 'jetty',10020);
然后也可以使用explain查看insert的信息.
8.使用navicat查看數據
db1中有兩條數據;

同樣可以發現db2,db3中都有數據.達到了分庫的目的.
同時,我們在mycat的TESTDB邏輯庫中,卻可以查詢出employee表中的所有數據來.

最后補上hash-int分區策略的配置文件partition-hash-int.txt

