1 mycat 安装部署
1 下载解压Mycat-server-1.6-RELEASE-20161010173036-linux.tar.gz 到/usr/local/mycat
2 下载解压jdk-8u101-linux-x64.tar.gz,拷贝dk-8u101 到/usr/local/mycat/
3 修改/usr/local/mycat/mycat/conf/wrapper.conf 修改wrapper.Java.command=java为上一步存放路径
wrapper.java.command=/usr/local/mycat/jdk1.8.0_101/bin/java
[mysql@hongquan conf]$ ls /usr/local/mycat/mycat/conf/wrapper.conf
/usr/local/mycat/mycat/conf/wrapper.conf
4 创建mycat 用户,改变目录权限为mycat
useradd mycat
chown -R mycat.mycat /usr/local/mycat
连接mysql的用户名:密码=system:mysql
5 修改conf/schema.xml,URL、用户名、密码修改,其余不变
[mysql@hongquan conf]$ ls /usr/local/mycat/mycat/conf/schema.xml
/usr/local/mycat/mycat/conf/schema.xml
[mysql@hongquan conf]$ vim schema.xml
password="www.com.workssys"> 6 修改/conf/server.xml 文件 [mysql@hongquan conf]$ ls /usr/local/mycat/mycat/conf/server.xml /usr/local/mycat/mycat/conf/server.xml (1) 用命令行工具或图形化客户端,连接MYSQL,创建DEMO所用三个分片数据库; CREATE database db1; CREATE database db2; CREATE database db3; (2) 修改my.inf新增以下语句,my.inf 一般会放在/etc/my.cnf 或 /etc/mysql/my.cnf,设置为Mysql大小写不敏感,否则可能会发生表找不到的问题。 lower_case_table_names = 1 (3) 解压Mycat-server-1.6-RELEASE-20161010173036-linux.tar.gz 到/usr/local/mycat [root@hqmysql1 soft]# tar xvf Mycat-server-1.6-RELEASE-20161028204710-linux_.tar.gz [root@hqmysql1 soft]# mv mycat/ /usr/local/ (4) 解压jdk-8u101-linux-x64.tar.gz,拷贝dk-8u101 到/usr/local/mycat/ [root@hqmysql1 soft]# tar xvf jdk-8u101-linux-x64.tar.gz [root@hqmysql1 soft]# mv jdk1.8.0_101/ /usr/local/ [root@hqmysql1 soft]# ll /usr/local/jdk1.8.0_101 # vim /etc/profile 添加如下内容 export JAVA_HOME=/usr/local/jdk1.8.0_101 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar [root@hqmysql1 soft]# source /etc/profile [root@hqmysql1 soft]# java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode) (5) 修改/usr/local/mycat/conf/wrapper.conf 修改wrapper.Java.command=java为上一步存放路径 wrapper.java.command=/usr/local/mycat/jdk1.8.0_101/bin/java (6) 创建mycat 用户,改变目录权限为mycat useradd mycat chown -R mycat.mycat /usr/local/mycat (8) 修改/usr/local/conf/schema.xml,URL、用户名、密码修改,其余不变,server.xml password="mysql"> [root@hqmysql1 conf]# vim server.xml [root@hongquan conf]# vim wrapper.conf [root@hongquan bin]# pwd /usr/local/mycat/mycat/bin (1) 进入 /usr/local/mycat/bin (默认数据端口为8066,管理端口为9066) 执行./mycat start [root@hongquan bin]# ./mycat start 查看mycat服务是否启动 # ps -ef |grep mycat [root@hqmysql1 bin]# netstat -nltp | grep 8066 tcp 0 0 :::8066 :::* LISTEN 24720/java [root@hqmysql1 logs]# tail -f -n 300 wrapper.log Starting Mycat-server... STATUS | wrapper | 2017/07/10 15:44:17 | Launching a JVM... ERROR | wrapper | 2017/07/10 15:44:17 | Unable to start JVM: No such file or directory (2) ERROR | wrapper | 2017/07/10 15:44:17 | JVM exited while loading the application. --修改wrapper.java.command=/usr/local/jdk1.8.0_101/bin/java Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: hqmysql1: hqmysql1: unknown error -----[root@hqmysql1 bin]# vim /etc/hosts 127.0.0.1 hqmysql1 (2) 进入logs目录,查看日志,如果wrapper.log 报错 java.NET.BindException: Address already in use 杀掉正在执行的相关java进程 ps -ef|grep java kill -9 xxx [root@hongquan bin]# ps -ef|grep java [mysql@hqmysql1 scripts]$ mysql -usystem -pmysql -h127.0.0.1 -P8066 -DTESTDB (mycat的用户账号和授权信息是在conf/server.xml文件中配置) [root@hongquan bin]# tail -f -n 300 /usr/local/mycat/mycat/logs/wrapper.log [INFO ][$_NIOREACTOR-0-RW] can't get connection for sql :select user() (io.mycat.sqlengine.SQLJob:SQLJob.java:114) [mycat@hongquan logs]$ tail -f mycat.log [root@hongquan logs]# netstat -nltp | grep 8066 tcp 0 0 :::8066 :::* LISTEN 34255/java [mysql@hongquan scripts]$ mysql -usystem -pmysql -P8066 -h10.0.1.134 -DTESTDB mysql> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.00 sec) mysql> show tables; +------------------+ | Tables in TESTDB | +------------------+ | company | | customer | | customer_addr | | employee | | goods | | hotnews | | orders | | order_items | | travelrecord | +------------------+ 9 rows in set (0.00 sec) mysql> create table employee (id int not null primary key,name varchar(100),sharding_id int not null); Query OK, 0 rows affected (0.16 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.01 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.10 sec) 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.00 sec) mysql> create table company(id int not null primary key,name varchar(100)); Query OK, 0 rows affected (0.10 sec) mysql> insert into company(id,name) values(1,'hp'); Query OK, 1 row affected (0.01 sec) mysql> select * from employee; Empty set (0.14 sec) mysql> insert into employee(id,name,sharding_id) values(1,'leader us',10000); Query OK, 1 row affected (0.00 sec) mysql> select * from employee; +----+-----------+-------------+ | id | name | sharding_id | +----+-----------+-------------+ | 1 | leader us | 10000 | +----+-----------+-------------+ 1 row in set (0.00 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) mysql>insert into employee(id,name,sharding_id) values(2,'me',10010); Query OK, 1 row affected (0.01 sec) mysql>insert into employee(id,name,sharding_id) values(3,'mycat',10000); Query OK, 1 row affected (0.00 sec) mysql>insert into employee(id,name,sharding_id) values(4,'mycat1',10010); Query OK, 1 row affected (0.01 sec) mysql> select * from employee; +----+-----------+-------------+ | id | name | sharding_id | +----+-----------+-------------+ | 2 | me | 10010 | | 4 | mycat1 | 10010 | | 1 | leader us | 10000 | | 3 | mycat | 10000 | +----+-----------+-------------+ 4 rows in set (0.01 sec) explain create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int); explain insert into travelrecord (id,user_id,traveldate,fee,days) values(1,'wang','2014-01-05',510.5,3); explain insert into travelrecord (id,user_id,traveldate,fee,days) values(7000001,'wang','2014-01-05',510.5,3); insert into company(id,name) values(1,'hp'); insert into company(id,name) values(2,'ibm'); insert into company(id,name) values(3,'oracle'); 创建客户表: create customer: create table customer(id int not null primary key,name varchar(100),company_id int not null,sharding_id int not null); 插入数据: insert into customer (id,name,company_id,sharding_id )values(1,'wang',1,10000); //stored in db1; insert into customer (id,name,company_id,sharding_id )values(2,'xue',2,10010); //stored in db2; insert into customer (id,name,company_id,sharding_id )values(3,'feng',3,10000); //stored in db1; 创建表格orders,并插入数据: create table orders (id int not null primary key ,customer_id int not null,sataus int ,note varchar(100) ); insert into orders(id,customer_id) values(1,1); //stored in db1 because customer table with id=1 stored in db1 insert into orders(id,customer_id) values(2,2); //stored in db2 because customer table with id=1 stored in db2 explain insert into orders(id,customer_id) values(2,2); select customer.name ,orders.* from customer ,orders where customer.id=orders.customer_id; 热点新闻,用取摸的方式随机分配到dn1,dn2,dn3上 create table hotnews(id int not null primary key ,title varchar(400) ,created_time datetime); insert into hotnews(id,title,created_time) values(1,'first',now()); 在分片1上 而Id为5,则到dn3上,5%3=2 ,即对应dn3的 index 其他: goods表 create table goods(id int not null primary key,name varchar(200),good_type tinyint, good_img_url varchar(200),good_created date,good_desc varchar(500), price double); 部分schema.xml <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
rule="mod-long" />
<table name="employee" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile" />
<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile">
<childTable name="orders" primaryKey="ID" joinKey="customer_id"
parentKey="id">
<childTable name="order_items" joinKey="order_id"
parentKey="id" />
childTable>
<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
parentKey="id" />
table>