Mycat的简易安装及测试
1.环境 OS版本 CentOS release 6.5 (Final) 64bit DB版本 Mysql 5.6.37 Mycat 1.6 jdk1.7及以上版本 2.实战部署 1.创建用户及用户组 [root@mysql01 ~]# groupadd mycat [root@mysql01 ~]# useradd -g mycat mycat 2.上传解压JDK Java Oracle官方下载地址为: http://www.oracle.com/technetwork/java/javase/archive-139210.html [mycat@mysql01 ~]$ tar zxvf jdk-7u80-linux-x64.tar.gz 3.上传解压mycat [mycat@mysql01 ~]$ tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 4.指定目录安装mycat及jdk [root@mysql01 mycat]# mv mycat /usr/local/ [root@mysql01 mycat]# mv jdk1.7.0_80 /usr/local/mycat/ 5.配置jdk环境变量 export JAVA_HOME=/usr/local/mycat/jdk1.7.0_80 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 6.创建测试库(mycat 对db1 db2 db3做了默认配置) mysql> create database db1; mysql> create database db2; mysql> create database db3; 7.配置mycat到数据库的连接(schema.xml) #手动测试 通过url user password 能链接到测试库 <writeHost host="hostM1" url="localhost:3306" user="root" password="123456"> 8.启动mycat [mycat@mysql01 bin]$ ./mycat start Starting Mycat-server... [mycat@mysql01 bin]$ 日志抛出异常 [mycat@mysql01 logs]$ more wrapper.log STATUS | wrapper | 2017/09/27 22:34:57 | --> Wrapper Started as Daemon STATUS | wrapper | 2017/09/27 22:34:57 | Launching a JVM... ERROR | wrapper | 2017/09/27 22:35:03 | JVM exited while loading the application. INFO | jvm 1 | 2017/09/27 22:35:03 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException : mysql01: mysql01: Name or service not known STATUS | wrapper | 2017/09/27 22:35:07 | Launching a JVM... [mycat@mysql01 logs]$ 9.修改hosts文件,绑定主机名 [root@mysql01 3306]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 mysql01 10.再次启动mycat [mycat@mysql01 bin]$ ./mycat start Starting Mycat-server... [mycat@mysql01 bin]$ 日志未见异常,启动成功 [mycat@mysql01 bin]$ ps -ef|grep mycat mycat 3374 1 0 22:39 ? 00:00:00 /usr/local/mycat/bin/./wrapper- 3.测试 1.利用mycat连接mysql数据库 #在mysql系统用户下执行登陆,此时的mysql相当于客户端 [mysql@mysql01 ~]$ mysql -uroot -p123456 -h127.0.0.1 -P8066 -DTESTDB Warning: Using a password on the command line interface can be insecure. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 其中8066是mycat的监听端口,其中-u,-p,-h分别是用户名,密码和主机,-D是连接的逻辑库。
这里的端口,用户名,密码,逻辑库都是在 server.xml 文件中配置的 2.创建Travelrecord表 mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int); Query OK, 0 rows affected (0.20 sec) mysql> 至于表名为什么是travelrecord,这个和配置文件schema.xml的配置有关 3.插入数据 mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10); mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(5000001,'Job',20160102,100,10); mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(10000001,'Slow',20160103,100,10); mysql> 至于ID为什么取三个值,这个与conf目录下autopartition-long.txt的定义有关,这个文件主要定义auto-sharding-long的规则。 这里主要是测试在id取不同区间的值时,分片的效果。 4.查看分片效果 #重新登陆到mysql上查询(不要通过mycat查询) [mysql@mysql01 ~]$ mysql -uroot -p123456 -h 127.0.0.1 -P 3306 mysql> select * from db1.travelrecord; +----+---------+------------+------+------+ | id | user_id | traveldate | fee | days | +----+---------+------------+------+------+ | 1 | Victor | 2016-01-01 | 100 | 10 | +----+---------+------------+------+------+ 1 row in set (0.00 sec) mysql> select * from db2.travelrecord; +---------+---------+------------+------+------+ | id | user_id | traveldate | fee | days | +---------+---------+------------+------+------+ | 5000001 | Job | 2016-01-02 | 100 | 10 | +---------+---------+------------+------+------+ 1 row in set (0.00 sec) mysql> select * from db3.travelrecord; +----------+---------+------------+------+------+ | id | user_id | traveldate | fee | days | +----------+---------+------------+------+------+ | 10000001 | Slow | 2016-01-03 | 100 | 10 | +----------+---------+------------+------+------+ 1 row in set (0.00 sec) mysql> 数据分片插入成功 4.查看MyCAT具体会将数据分配到哪个节点上 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) | +-----------+----------------------------------------------------------------------------------------------+ 1 row in set (0.00 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) | +-----------+-------------------------------------------------------------------------------------------------+ 1 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) | +-----------+---------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> 语法其实蛮简单,就是SQL语句前加上explain语句 5.关于配置文件,conf目录下主要以下三个需要熟悉 server.xml是Mycat服务器参数调整和用户授权的配置文件 schema.xml是逻辑库定义和表以及分片定义的配置文件 rule.xml是分片规则的配置文件