mysql,mycat的demo

一直都是使用公司架構師提供的mycat,今天得空本身嘗試瞭如下:java

1.首先安裝jdk(官網建議1.7及以上版本),mysql(我安裝的是5.7.19-0ubuntu0.16.04.1,設置帳號密碼爲root,123456,與mycat保持一致,後面避免修改配置文件),mycat(我安裝的是Mycat-server-1.6-RELEASE-20161028204710-linux.tar);node

以上可直接去官網下載;mysql

2.建立三個數據庫,暫且名稱定爲db1,db2,db3,與mycat的配置一致,省去了修改被指文件的工做;linux

3.啓動mysql,啓動mycat,分別查看是否啓動成功,若是啓動mycat時報錯,java.net.MalformedURLException,應該是由於主機名沒有綁定,能夠去配置文件/etc/hosts種在127.0.0.1  後面增長  主機名,在啓動mycat應該就不會報錯了;sql

4.執行命令:mysql -uroot -p123456 -h127.0.0.1 -P8066 -DTESTDB,進入mycat命令行界面;數據庫

建立Travelrecord表:ubuntu

create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);

插入數據服務器

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10);
Query OK, 1 row affected (0.00 sec)

 

至於ID爲何取三個值,這個與conf目錄下autopartition-long.txt的定義有關,這個文件主要定義auto-sharding-long的規則。架構

# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2

下面來看看分片的效果:
mysql> select * from db1.travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee  | days |
+----+---------+------------+------+------+
|  1 | Victor  | 2016-01-01 |  100 |   10 |
+----+---------+------------+------+------+
row in set (0.00 sec)

mysql> select * from db2.travelrecord;
+---------+---------+------------+------+------+
| id      | user_id | traveldate | fee  | days |
+---------+---------+------------+------+------+
| 5000001 | Job     | 2016-01-02 |  100 |   10 |
+---------+---------+------------+------+------+
row in set (0.00 sec)

mysql> select * from db3.travelrecord;
+----------+---------+------------+------+------+
| id       | user_id | traveldate | fee  | days |
+----------+---------+------------+------+------+
| 10000001 | Slow    | 2016-01-03 |  100 |   10 |
+----------+---------+------------+------+------+
row in set (0.00 sec)

若是想看MyCAT具體會將數據分配到哪一個節點上,可經過路由分析。語法其實蠻簡單,就是SQL語句前加上explain語句。spa

mysql> explain create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                                                   |
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| dn1       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
| dn2       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
| dn3       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
+-----------+-----------------------------------------------------------------------------------------------------------------------+
rows in set (0.00 sec)

mysql>  explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10);
+-----------+----------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                          |
+-----------+----------------------------------------------------------------------------------------------+
| dn1       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10) |
+-----------+----------------------------------------------------------------------------------------------+
row in set (0.01 sec)

mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10);
+-----------+-------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                             |
+-----------+-------------------------------------------------------------------------------------------------+
| dn2       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10) |
+-----------+-------------------------------------------------------------------------------------------------+
row in set (0.00 sec)

mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10);
+-----------+---------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                               |
+-----------+---------------------------------------------------------------------------------------------------+
| dn3       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10) |
+-----------+---------------------------------------------------------------------------------------------------+
row in set (0.00 sec)

 

關於配置文件,conf目錄下主要如下三個須要熟悉。

server.xml是Mycat服務器參數調整和用戶受權的配置文件

schema.xml是邏輯庫定義和表以及分片定義的配置文件

rule.xml是分片規則的配置文件

 

以上爲我搭建demo,更多的使用限制和經驗須要你們本身慢慢積累,我在這裏就不贅述了

相關文章
相關標籤/搜索