這篇文章將為大家詳細(xì)講解有關(guān)Mycat如何部署與使用,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站的客戶來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜希瑥膭?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站制作、做網(wǎng)站、電商網(wǎng)站開(kāi)發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開(kāi)發(fā)。
MyCAT 可以視為“MySQL”集群的企業(yè)級(jí)數(shù)據(jù)庫(kù),用來(lái)替代昂貴的Oracle集群,其背后是阿里曾經(jīng)開(kāi)源的知名產(chǎn)品Cobar。MyCAT的目標(biāo)是:低成本的將現(xiàn)有的單機(jī)數(shù)據(jù)庫(kù)和應(yīng)用平滑遷移到“云”端,解決數(shù)據(jù)存儲(chǔ)和業(yè)務(wù)規(guī)模迅速增長(zhǎng)情況下的數(shù)據(jù)瓶頸問(wèn)題。
1 應(yīng)用場(chǎng)景
Mycat 有很多數(shù)據(jù)分庫(kù)規(guī)則,接下來(lái)幾篇就相關(guān)覺(jué)得常用的規(guī)則進(jìn)行試用與總結(jié)。
一般來(lái)說(shuō),按自然月份或按日期來(lái)進(jìn)行數(shù)據(jù)分片的規(guī)則比較適用于商城訂單查詢,類似最近1周、2周、3個(gè)月內(nèi)的數(shù)據(jù)?;蚴菆?bào)表類應(yīng)用。
這樣的數(shù)據(jù)放在一個(gè)片區(qū)內(nèi)省去了數(shù)據(jù)合并的時(shí)間。
當(dāng)然按月數(shù)據(jù)量不要過(guò)大就OK。
一、部署步驟詳解
(1) 用命令行工具或圖形化客戶端,連接MYSQL,創(chuàng)建DEMO所用三個(gè)分片數(shù)據(jù)庫(kù);
CREATE SCHEMA `range_db_01` DEFAULT CHARACTER SET utf8 ; CREATE SCHEMA `range_db_02` DEFAULT CHARACTER SET utf8 ; CREATE SCHEMA `range_db_03` DEFAULT CHARACTER SET utf8 ; CREATE SCHEMA `range_db_04` DEFAULT CHARACTER SET utf8 ;
(2) 修改配置my.cnf新增以下語(yǔ)句, 一般會(huì)放在/etc/my.cnf 或 /etc/mysql/my.cnf,設(shè)置為Mysql表名大小寫不敏感,否則可能會(huì)發(fā)生表找不到的問(wèn)題。
lower_case_table_names = 1
(3) 解壓Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 到安裝目錄下,會(huì)生成mycat目錄
(4) 安裝jdk-7u79-linux-x64
(5) 修改mycat/conf/wrap.conf 修改wrapper.Java.command=java為上一步存放路徑
wrapper.java.command=/usr/java/jdk1.7.0_79/
(6) 創(chuàng)建mycat 用戶,改變目錄權(quán)限為mycat
useradd mycat chown -R mycat.mycat mycat
(7) 修改用戶密碼
passwd mycat 輸入:
(8) 修改mycat/conf/schema.xml,URL、用戶名、密碼修改,其余不變
二、運(yùn)行步驟詳解
(1) 進(jìn)入 mycat/bin (默認(rèn)數(shù)據(jù)端口為8066,管理端口為9066)
執(zhí)行./mycat start
(2) 進(jìn)入logs目錄,查看日志,如果wrapper.log 報(bào)錯(cuò) java.NET.BindException: Address already in use 殺掉正在執(zhí)行的相關(guān)java進(jìn)程
ps -ef|grep java kill -9 xxx
三、使用步驟詳解
(1) 進(jìn)入mysql bin目錄mysql/bin/
(2) 登錄mysql 執(zhí)行以下命令
./mysql -utest -ptest -h292.168.184.56 -P8066 -DTESTDB
(mycat的用戶賬號(hào)和授權(quán)信息是在conf/server.xml文件中配置)
(3) 表創(chuàng)建測(cè)試:
mysql> create table employee (id int not null primary key,name varchar(100),sharding_id int not null); Query OK, 0 rows affected (0.30 sec) mysql> explain create table employee (id int not null primary key,name varchar(100),sharding_id int not null); +-----------+------------------------------------------------------------------------------------------------+ | DATA_NODE | SQL | +-----------+------------------------------------------------------------------------------------------------+ | dn1 | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) | | dn2 | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) | +-----------+------------------------------------------------------------------------------------------------+ 2 rows in set (0.04 sec)
(4) 客戶端軟件使用:navicat
創(chuàng)建mycat新連接:ip:192.168.184.56,用戶名:test,密碼:test,端口:8066
可看到TESTDB數(shù)據(jù)庫(kù)下已創(chuàng)建表:employee
打開(kāi)db1,db2 數(shù)據(jù)庫(kù)也可看到已創(chuàng)建表employee
(5) 插入數(shù)據(jù)測(cè)試
mysql> insert into employee(id,name,sharding_id) values(1,'leader us',10000); ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 6 Current database: TESTDB Query OK, 1 row affected (0.03 sec) mysql> explain insert into employee(id,name,sharding_id) values(1,'leader us',10000); +-----------+-----------------------------------------------------------------------+ | DATA_NODE | SQL | +-----------+-----------------------------------------------------------------------+ | dn1 | insert into employee(id,name,sharding_id) values(1,'leader us',10000) | +-----------+-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
(6) 根據(jù)規(guī)則auto-sharding-long(主鍵范圍)進(jìn)行分片測(cè)試
mysql> explain create table company(id int not null primary key,name varchar(100)); +-----------+---------------------------------------------------------------------+ | DATA_NODE | SQL | +-----------+---------------------------------------------------------------------+ | dn1 | create table company(id int not null primary key,name varchar(100)) | | dn2 | create table company(id int not null primary key,name varchar(100)) | | dn3 | create table company(id int not null primary key,name varchar(100)) | +-----------+---------------------------------------------------------------------+ 3 rows in set (0.01 sec)
(7) 三個(gè)分片上都插入了3條數(shù)據(jù)
mysql> explain insert into company(id,name) values(1,'hp'); +-----------+---------------------------------------------+ | DATA_NODE | SQL | +-----------+---------------------------------------------+ | dn1 | insert into company(id,name) values(1,'hp') | | dn2 | insert into company(id,name) values(1,'hp') | | dn3 | insert into company(id,name) values(1,'hp') | +-----------+---------------------------------------------+ 3 rows in set (0.00 sec)
(8) 確認(rèn)是分片存儲(chǔ)
mysql> select * from employee; +----+-----------+-------------+ | id | name | sharding_id | +----+-----------+-------------+ | 2 | me | 10010 | | 4 | mydog | 10010 | | 1 | leader us | 10000 | | 3 | mycat | 10000 | +----+-----------+-------------+ 4 rows in set (0.01 sec) mysql> explain select * from employee; +-----------+----------------------------------+ | DATA_NODE | SQL | +-----------+----------------------------------+ | dn1 | SELECT * FROM employee LIMIT 100 | | dn2 | SELECT * FROM employee LIMIT 100 | +-----------+----------------------------------+ 2 rows in set (0.00 sec)
關(guān)于“Mycat如何部署與使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。