快速體驗MyCAT
橫空出世的MyCat截至到2015年4月,保守估計已經有超過60個項目在使用,主要應用在電信領域、互聯網項目,大部分是交易和管理系統,少量是信息系統。比較大的系統中,數據規模單表單月30億。本人也初步嘗鮮,體驗了一把,見以下描述,供大家參考。
一、安裝mycat
MyCAT 是使用 JAVA 語言進行編寫開發,使用前需要先安裝 JAVA 運行環境(JRE),由于 MyCAT 中使用
了 JDK7 中的一些特性,所以要求必須在 JDK7 以上的版本上運行。
環境準備
1) JDK 下載
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
注:必須 JDK7 或更高版本.
2) MySQL 下載
http://dev.mysql.com/downloads/mysql/5.5.html#downloads
注:MyCAT 支持多種數據庫接入,如:MySQL、 SQLServer、 Oracle、 MongoDB 等,推薦使用 MySQL
做集群。
3) MyCAT 項目主頁
https://github.com/MyCATApache/
注:MyCAT 相關源碼、文檔都可以在此地址下進行下載。
windows下可以下載Mycat-server-1.4-RELEASE-yyyymmddxxxx-win.tar
將其解壓在某個目錄下,建議解壓到本地某個盤符根目錄下(不建議使用中文目錄),如下為解壓后的:
D:/>cd mycat
D:/mycat>dir
驅動器 D 中的卷沒有標簽。
卷的序列號是 0006-4285
D:/mycat 的目錄
2015/09/24 14:55 <DIR> .
2015/09/24 14:55 <DIR> ..
2015/09/24 14:55 <DIR> bin //bin 程序目錄
2015/09/24 14:51 <DIR> catlet
2015/09/24 16:35 <DIR> conf //conf 目錄下存放配置文件
2015/09/24 14:55 <DIR> lib //lib 目錄下主要存放 mycat 依賴的一些 jar 文件
2015/09/25 10:49 <DIR> logs //log 目錄下主要是日志存放
2015/09/22 23:30 217 version.txt
D:/mycat>more version.txt //版本信息
BuildTime 2015-09-22 15:30:06
GitVersion b9af103db1f04afca17edaa05bfe3118ed0b23c2
MavenVersion 1.4-RELEASE
GitUrl https://github.com/MyCATApache/Mycat-Server.git
MyCatSite http://www.mycat.org.cn
QQGroup 106088787
二、配置mycat
1、mycat的配置
文件全部基于xml文件來完成,位于mycat路徑下的conf目錄
通常包括以下配置文件:全部基于xml文件來完成,位于mycat路徑下的conf目錄,通常包括以下配置文件:
autopartition-long.txt
cacheservice.properties
dnindex.properties
ehcache.xml
index_to_charset.properties
log4j.xml //日志文件的相關配置
partition-hash-int.txt
partition-range-mod.txt
router.xml
rule.xml
schema.xml
sequence_conf.properties
sequence_db_conf.properties
sequence_time_conf.properties
server.xml
wrapper.conf
2、基本配置
修改server.xml如下
<user name="test"> //配置連接到mycat的用戶名及密碼等
<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>
修改schema.xml如下
D:/mycat/conf>type schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="testdb" checkSQLschema="false" sqlMaxLimit="100"> //邏輯數據庫名及相關配置
<!– 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="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" /> //定義了一張全局表
</schema>
<dataNode name="dn1" dataHost="192.168.1.204" database="db1" /> //定義分片節點,位于為2臺不同的Linux主機
<dataNode name="dn2" dataHost="192.168.1.143" database="db2" /> //2個不同的主機使用了不同的物理庫名
//注:也可以在同一個實例上來定義分片,庫名不同即可
<dataHost name="192.168.1.204" maxCon="1000" minCon="10" balance="0" //定義分片主機相關屬性
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.204:3306" user="mycat" //連接到物理數據庫地址用戶名密碼等
password="pwd">
</writeHost>
</dataHost>
<dataHost name="192.168.1.143" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.143:3307" user="mycat"
password="pwd">
</writeHost>
</dataHost>
</mycat:schema>
三、啟動mycat
1、直接運行
mycat.bat console 或者startup_nowrap.bat
D:/mycat/bin>mycat.bat console
wrapper | –> Wrapper Started as Console
wrapper | Launching a JVM…
jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
jvm 1 |
jvm 1 | log4j 2015-09-25 11:11:07 [./conf/log4j.xml] load completed.
jvm 1 | MyCAT Server startup successfully. see logs in logs/mycat.log //可以看到啟動成功
2、裝載成服務啟動
D:/mycat/bin>mycat.bat install
wrapper | Mycat-server installed.
D:/mycat/bin>mycat start //也可以通過界面方式啟動mycat
wrapper | Starting the Mycat-server service…
wrapper | Mycat-server started.
四、演示mycat
1、登錄
登錄方式類似于 mysql 的服務端登陸,目前 mycat 有兩個端口,8066 數據端口,9066 管理端口
登陸方式如:
mysql -h127.0.0.1 -utest -ptest -P9066 [-dmycat]
-h 后面是主機,即當前 mycat 按照的主機地址,本地可用 127.0.0.1 遠程需要遠程 ip
-u Mycat server.xml 中配置的邏輯庫用戶
-p Mycat server.xml 中配置的邏輯庫密碼
-P 后面是端口 默認 9066,注意 P 是大寫
-d Mycat server.xml 中配置的邏輯庫
數據端口與管理端口的配置端口修改:
數據端口默認 8066,管理端口默認 9066 ,如果需要修改需要配置 serve.xml
2、演示
–基于管理端口演示
D:/mycat/bin>mysql -utest -ptest -P9066 -Dtestdb mysql> show @@help; -- 獲取有關管理的相關命令 mysql> show @@database; -- 查看邏輯數據庫 +----------+ | DATABASE | +----------+ | testdb | +----------+ mysql> show @@datanode; --查看分片節點 +------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ | NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME | +------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ | dn1 | 192.168.1.204/db1 | 0 | mysql | 0 | 10 | 1000 | 152 | 0 | 0 | 0 | -1 | | dn2 | 192.168.1.143/db2 | 0 | mysql | 0 | 0 | 1000 | 0 | 0 | 0 | 0 | -1 | +------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ mysql> show @@server; --查看服務器狀態 +---------------+-------------+--------------+------------+---------------+---------------+---------+--------+-----------------------+ | UPTIME | USED_MEMORY | TOTAL_MEMORY | MAX_MEMORY | RELOAD_TIME | ROLLBACK_TIME | CHARSET | STATUS | AVG_BUFPOOL_ITEM_SIZE | +---------------+-------------+--------------+------------+---------------+---------------+---------+--------+-----------------------+ | 23m 50s 344ms | 344386624 | 2058878976 | 2058878976 | 1443150953033 | -1 | gbk | ON | 1878 | +---------------+-------------+--------------+------------+---------------+---------------+---------+--------+-----------------------+ mysql> show @@version; --查看版本 +----------------------------------------+ | VERSION | +----------------------------------------+ | 5.5.8-mycat-1.4-RELEASE-20150922233010 | +----------------------------------------+ --基于數據端口演示,分別創建連接用戶及數據庫 (root@192.168.1.204) [(none)]> grant all privileges on *.* to 'mycat'@'192.168.%.%' identified by 'pwd'; Query OK, 0 rows affected (0.00 sec) (root@192.168.1.204) [(none)]> create database db1; Query OK, 1 row affected (0.00 sec) (root@192.168.1.143) [(none)]> grant all privileges on *.* to 'mycat'@'192.168.%.%' identified by 'pwd'; Query OK, 0 rows affected (0.00 sec) (root@192.168.1.143) [(none)]> create database db2; D:/mycat/bin>mysql -utest -ptest -P8066 -Dtestdb D:/mycat/bin>mysql -utest -ptest -P8066 -Dtestdb mysql> select database(); +------------+ | DATABASE() | +------------+ | testdb | +------------+ mysql> show tables; +------------------+ | Tables in testdb | +------------------+ | goods | +------------------+ mysql> desc goods; ERROR 1146 (42S02): Table 'db2.goods' doesn't exist mysql> explain create table goods(id int auto_increment not null primary key,val varchar(20)); +-----------+--------------------------------------------------------------------------------+ | DATA_NODE | SQL | +-----------+--------------------------------------------------------------------------------+ | dn1 | create table goods(id int auto_increment not null primary key,val varchar(20)) | | dn2 | create table goods(id int auto_increment not null primary key,val varchar(20)) | +-----------+--------------------------------------------------------------------------------+ mysql> create table goods(id int auto_increment not null primary key,val varchar(20)); Query OK, 0 rows affected (0.05 sec) mysql> insert into goods(val) values('apple'); mysql> explain select * from goods; +-----------+-------------------------------+ | DATA_NODE | SQL | +-----------+-------------------------------+ | dn2 | SELECT * FROM goods LIMIT 100 | --此處可知,select查詢會直接查詢第二個分片 +-----------+-------------------------------+ mysql> select 'Leshami' as author,'http://blog.csdn.net/leshami' as Blog; +---------+------------------------------+ | author | Blog | +---------+------------------------------+ | Leshami | http://blog.csdn.net/leshami | +---------+------------------------------+ mysql> select * from goods; +----+-------+ | id | val | +----+-------+ | 1 | apple | +----+-------+ D:/mycat/bin>mysql -umycat -ppwd -h192.168.1.143 -P3307 -e "select * from db2.goods" Warning: Using a password on the command line interface can be insecure. +----+-------+ | id | val | +----+-------+ | 1 | apple | +----+-------+ D:/mycat/bin>mysql -umycat -ppwd -h192.168.1.204 -P3306 -e "select * from db1.goods" Warning: Using a password on the command line interface can be insecure. +----+-------+ | id | val | +----+-------+ | 1 | apple | +----+-------+ D:/mycat/bin>mysql -uuser -puser -P8066 -Dtestdb --只讀用戶連接 mysql> use testdb; Database changed mysql> show tables; +------------------+ | Tables in testdb | +------------------+ | goods | +------------------+ mysql> desc goods; ERROR 1495 (HY000): User readonly --提示用戶只讀
作者:robinson_0612 發表于2015/10/8 9:29:39 原文鏈接