Mycat部署与使用
MyCAT 可以视为“MySQL”集群的企业级数据库,用来替代昂贵的Oracle集群,其背后是阿里曾经开源的知名产品Cobar。MyCAT的目标是:低成本的将现有的单机数据库和应用平滑迁移到“云”端,解决数据存储和业务规模迅速增长情况下的数据瓶颈问题。
下面就适合订单的业务场景做介绍
1 应用场景
Mycat 有很多数据分库规则,接下来几篇就相关觉得常用的规则进行试用与总结。
一般来说,按自然月份或按日期来进行数据分片的规则比较适用于商城订单查询,类似最近1周、2周、3个月内的数据。或是报表类应用。
这样的数据放在一个片区内省去了数据合并的时间。
当然按月数据量不要过大就OK。
一、部署步骤详解
(1) 用命令行工具或图形化客户端,连接MYSQL,创建DEMO所用三个分片数据库;
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新增以下语句, 一般会放在/etc/my.cnf 或 /etc/mysql/my.cnf,设置为Mysql表名大小写不敏感,否则可能会发生表找不到的问题。
lower_case_table_names = 1
(3) 解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 到安装目录下,会生成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) 创建mycat 用户,改变目录权限为mycat
useradd mycat
chown -R mycat.mycat mycat
(7) 修改用户密码
passwd mycat
输入:
(8) 修改mycat/conf/schema.xml,URL、用户名、密码修改,其余不变
<writeHost host="hostM1" url="192.168.184.56:3306" user="root"
password="root">
二、运行步骤详解
(1) 进入 mycat/bin (默认数据端口为8066,管理端口为9066)
执行./mycat start
(2) 进入logs目录,查看日志,如果wrapper.log 报错 java.NET.BindException: Address already in use 杀掉正在执行的相关java进程
ps -ef|grep java
kill -9 xxx
三、使用步骤详解
(1) 进入mysql bin目录mysql/bin/
(2) 登录mysql 执行以下命令
./mysql -utest -ptest -h192.168.184.56 -P8066 -DTESTDB
(mycat的用户账号和授权信息是在conf/server.xml文件中配置)
(3) 表创建测试:
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
创建mycat新连接:ip:192.168.184.56,用户名:test,密码:test,端口:8066
可看到TESTDB数据库下已创建表:employee
打开db1,db2 数据库也可看到已创建表employee
(5) 插入数据测试
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) 根据规则auto-sharding-long(主键范围)进行分片测试
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) 三个分片上都插入了3条数据
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) 确认是分片存储
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)