数据库中间件mycat简单入门
当在项目中mysql数据库成为瓶颈的时候,我们一般会使用主从复制,分库分表的方式来提高数据库的响应速度,比如mysql主从复制,在没有数据库中间件的情况下,我们只能由开发工程师在程序中控制,这对于一个在正在线上运行的项目来说,需要改动的代码量蛮大的,也不方便扩展,比如,我想再添加一台从机,下面进行mycat入门
一、部署步骤详解
(1) 用命令行工具或图形化客户端,连接MYSQL,创建DEMO所用三个分片数据库;
CREATE database db1;
CREATE database db2;
CREATE database db3;
(2) /etc/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="192.168.0.4:3306" user="root"
password="admin">
二、运行步骤详解
(1) 进入 /usr/local/mycat/bin (默认数据端口为8066,管理端口为9066)
执行 ./mycat start
三、使用步骤详解
(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: 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)