快速体验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 | +----------------------------------------+ --基于数据端口演示,分别创建连接用户及数据库 ([emailprotected]) [(none)]> grant all privileges on *.* to 'mycat'@'192.168.%.%' identified by 'pwd'; Query OK, 0 rows affected (0.00 sec) ([emailprotected]) [(none)]> create database db1; Query OK, 1 row affected (0.00 sec) ([emailprotected]) [(none)]> grant all privileges on *.* to 'mycat'@'192.168.%.%' identified by 'pwd'; Query OK, 0 rows affected (0.00 sec) ([emailprotected]) [(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.youkuaiyun.com/leshami' as Blog; +---------+------------------------------+ | author | Blog | +---------+------------------------------+ | Leshami | http://blog.youkuaiyun.com/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 原文链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值