資料庫中介軟體-mycat快速入門
準備工作
下載以後的檔案目錄結構如下:
快速入門
配置mycat/conf的server.xml
<user name="root">
<property name="password">root</property>
<property name="schemas">TESTDB</property>
<!-- 表級 DML 許可權設定 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
說明:name=root,password=root,schemas=TESTDB表示mycat邏輯庫的使用者名稱是root,密碼是root,資料庫名是TESTDB
配置conf/schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<!-- auto sharding by id (long) -->
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<!-- global table is auto cloned to all defined data nodes ,so can join
with any table whose sharding node is in the same data node -->
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
rule="mod-long" />
<!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
rule="mod-long" /> -->
<table name="employee" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile" />
<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile">
<childTable name="orders" primaryKey="ID" joinKey="customer_id"
parentKey="id">
<childTable name="order_items" joinKey="order_id"
parentKey="id" />
</childTable>
<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
parentKey="id" />
</table>
<!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
/> -->
</schema>
這一段程式碼表示邏輯庫名稱是TESTDB,裡面有travelrecord、company、goods、hotnews、employee、customer等邏輯表
dataNode=”dn1,dn2,dn3”表示位於物理庫的dn1,dn2,dn3庫
下面的
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />
表示有三個物理庫,物理庫的名稱為db1,db2,db3
dataHost表示配置物理資料庫的連結資訊,改成你自己的配置即可
<writeHost host="hostM1" url="localhost:3306" user="root"
password="root">
儲存配置
使用客戶端工具連線物理庫建立相對應的資料庫:
create database db1;
create database db2;
create database db3;
啟動mycat
進入bin目錄,雙擊startup_nowrap.bat,出現一個視窗
客戶端工具邏輯庫
使用sqlyog或者navicat工具連線邏輯庫,
IP: localhost
port: 8066
user: root
password: root
連結以後的效果如下:
這個時候會有你在schema.xml裡面配置的表。查詢,報錯!
報錯是正常的,因為我們物理庫並沒有表
在mycat的8066連線中,執行一下SQL
create table employee (id int not null primary key,name varchar(100),sharding_id int not null);
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);
再去查詢select * from employee
發現有資料了
再去localhost3306物理庫裡面可以看到
物理庫裡面資料是分佈在db1和db2裡面的,邏輯庫裡面查詢到的資料則合併在一起,即可達到分庫的架構
分表架構同樣也可以通過mycat配置實現
Mycat暫時遇到的bug
1、使用客戶端工具查詢不到資料,控制檯報錯find no route,提示找不到路由
解決辦法:使用命令列查詢
DOS下面:
mysql -uroot -proot -P8066
use TESTDB;
select * from employee;