本文档目的在于介绍mycat的安装部署, 以及给出一两个mycat水平切分的例子。
一个用于MySQL读写分离和与数据切分的高可用中间件
一个模拟为MySQLServer的超级数据库代理
一个能平滑扩展支持大表的分布式数据库系统
一个可管控多种关系数据库的数据库路由器
192.168.8.87 mysql + mycat
192.168.8.86 mysql
1) 上传mycat安装文件
[iotmp@host87 mycat]$ ll
total 15296
-rw-r--r-- 1 iotmp iotmp 15662280 Mar 18 11:02 Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[iotmp@host87 mycat]$
2) 解压安装文件
[iotmp@host87 mycat]$ tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[iotmp@host87 mycat]$ cd mycat/
[iotmp@host87 mycat]$ cd conf/
[iotmp@host87 conf]$
3) 修改配置文件server.xml
[iotmp@host87 conf]$ cat server.xml
0
0
utf8
2
0
0
1
1m
1k
0
384m
true
druidparser
1
8066
9066
0.0.0.0
123456
mydb1,mydb2
[iotmp@host87 conf]$
4) 修改配置文件schema.xml
[root@host87 conf]# cat schema.xml
select user()
select user()
[root@host87 conf]#
[iotmp@host87 conf]$
5) 配置文件rule.xml
[root@host87 conf]# cat rule.xml
id
rang-long
id
mod-long
autopartition-long.txt
2
[root@host87 conf]# cat autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000.
0-100000=0
100001-200000=1
[root@host87 conf]#
1)创建测试库和表, 在两节点均执行
mysql> create database testdb1 default charset "utf8";
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> create database testdb2 default charset "utf8";
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> use testdb1
Database changed
mysql>
mysql> create table t1(id bigint);
Query OK, 0 rows affected (0.13 sec)
mysql>
mysql> use testdb2
Database changed
mysql>
mysql> create table t2(id bigint);
Query OK, 0 rows affected (0.08 sec)
mysql>
2) 启动mycat
[root@host87 conf]# /home/iotmp/mycat/mycat/bin/mycat start
Starting Mycat-server...
[root@host87 conf]#
[root@host87 conf]# sudo netstat -nltp|grep -E "8066|9066"
tcp6 0 0 :::8066 :::* LISTEN 18759/java
tcp6 0 0 :::9066 :::* LISTEN 18759/java
[root@host87 conf]#
3) 使用客户端连接mycat, 默认端口8066,可以看到连接的是mycat server.
[root@host87 conf]# mysql -uroot -p123456 -P8066 -h192.168.8.87
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2015, 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.
4) 查看实例
mysql> show databases;
+----------+
| DATABASE |
+----------+
| mydb1 |
| mydb2 |
+----------+
2 rows in set (0.00 sec)
5)插入数据
mysql>
mysql> use mydb1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+-----------------+
| Tables in mydb1 |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql>
mysql> insert into t1(id) values(1);
Query OK, 1 row affected (0.05 sec)
mysql>
mysql> insert into t1(id) values(2);
Query OK, 1 row affected (0.02 sec)
mysql>
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.04 sec)
mysql>
mysql> use mydb2
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> insert into t2(id) values(100);
Query OK, 1 row affected (0.02 sec)
mysql>
mysql> insert into t2(id) values(100001);
Query OK, 1 row affected (0.04 sec)
mysql>
mysql> select * from t2;
+--------+
| id |
+--------+
| 100001 |
| 100 |
+--------+
2 rows in set (0.00 sec)
mysql>
以上操作创建了两张表,每张表插入了两条数据,待会验证是不是分到不同的数据库表上去了.
6) 登陆到实际的后端db,验证表数据
[root@host87 conf]# mysql -uroot -p123456 -P3306 -h192.168.8.87
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2445
Server version: 5.6.41-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
mysql> use testdb1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> use testdb2
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select * from t2;
+--------+
| id |
+--------+
| 100001 |
+--------+
1 row in set (0.00 sec)
mysql>
87上每个表只有一条数据,再看86的
[root@host87 conf]# mysql -uroot -p123456 -P3306 -h192.168.8.86
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 410955
Server version: 5.6.41-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> use testdb1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select * from t1;
+------+
| id |
+------+
| 2 |
+------+
1 row in set (0.01 sec)
mysql> use testdb2
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select * from t2;
+------+
| id |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
mysql>
86上每个表上也只有一条数据
在本次实验中,可以看到,使用mycat插入的数据,按照分片规则(rang-long和mod-long)分别插入到了相应的后端物理数据库中, 分库分表验证成功.