转载于:
MyCAT 可以视为“Mysql”集群的企业级数据库,用来替代昂贵的Oracle集群,其背后是阿里曾经开源的知名产品Cobar。MyCAT的目标是:低成本的将现有的单机数据库和应用平滑迁移到“云”端,解决数据存储和业务规模迅速增长情况下的数据瓶颈问题。
一、部署步骤详解
(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.3.0.2-20150105144205-linux.tar.gz 到/usr/local/mycat
(4) 解压jdk-7u65-linux-i586.gz,拷贝jdk1.7.0_65 到/usr/local/mycat/
(5) 修改/usr/local/conf/wrap.conf 修改wrapper.java.command=java为上一步存放路径
wrapper.java.command=/usr/local/mycat/jdk1.7.0/bin/java(6) 创建mycat 用户,改变目录权限为mycat
useradd mycat chown -R mycat.mycat /usr/local/mycat(7) 修改用户密码
passwd mycat 输入:(8) 修改/usr/local/conf/schema.xml,URL、用户名、密码修改,其余不变
<writeHost host="hostM1" url="10.1.176.104:3306" user="root" password="">二、运行步骤详解
(1) 进入 /usr/local/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目录/usr/local/mysql/bin/(2) 登录mysql 执行以下命令
./mysql -utest -ptest -h10.1.176.104 -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:10.1.176.104,用户名: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: TESTDBQuery 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)