一、安装MySQL或MariaDB(本文以MariaDB为例)
MySQL手动安装方法:点击查看
MariaDB安装:
1、下载MariaDB的repo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$ vi /etc/yum.repos.d/MariaDB.repo
# MariaDB 的Yum源
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
#保存退出
#更新Yum源
$ yum clean all
$ yum makecache
|
2、安装MariaDB
1
2
3
4
5
6
7
8
9
10
|
$ yum install -y mariadb.x86_64 mariadb-server.x86_64 mariadb-libs.x86_64
# 启动MariaDB
$ service mysql start
#MariaDB
# 端口:3360
# 账户密码:root/123456
#Datadir:/var/lib/mysql
#配置文件:/etc/my.cnf
|
其他修改MariaDB的密码或授权操作与MySql无异,可按http://www.cnblogs.com/raphael5200/p/5265736.html 中进行操作。
二、安装部署MyCat
1、下载 安装MyCat
下载地址:http://www.mycat.org.cn/
安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
$ tar -zxvf Mycat-server-1.5.1-RELEASE-2016081617.tar.gz
$ mv mycat/ /usr/local/
$ cd /usr/local/mycat/conf
$ vim wrapper.conf
# Java Application 以下参数可以省略
wrapper.java.command=/usr/java/jdk1.7.0_71/bin/java
wrapper.java.additional.12=-XX:+UseParNewGC
wrapper.java.additional.13=-XX:+UseConcMarkSweepGC
wrapper.java.additional.14=-XX:+UseCMSCompactAtFullCollection
wrapper.java.additional.15=-XX:CMSFullGCsBeforeCompaction=0
wrapper.java.additional.16=-XX:CMSInitiatingOccupancyFraction=70
#设置Hosts名
$ vim /etc/hosts
192.168.101.161 server-161
#mycat 就已经启动了 端口8066
./bin/mycat start
#查看方法
$ mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB
|
三、配置MyCat分片
1、在MariaDB中新建3个数据库db1,db2,db3
1
2
3
4
5
6
7
|
CREATE
database
db1;
CREATE
database
db2;
CREATE
database
db3;
-- 注意:若是LINUX版本的MYSQL,则需要设置为Mysql大小写不敏感,否则可能会发生表找不到的问题。
-- 在MySQL的配置文件/etc/my.cnf 的[mysqld] 中增加一行
lower_case_table_names = 1
|
2、配置MyCat的schema.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$ vim /usr/local/mycat/conf/schema.xml
<schema name=
"TESTDB"
checkSQLschema=
"false"
sqlMaxLimit=
"100"
>
#TESTDB 是MyCat默认的一个测试逻辑数据库,需要在此节点下定义逻辑表,但在这里只是指定表的名称,并不对表进行详细的定义。
#下面这条语句就是指逻辑表tb_user_info将在dn1,dn2,dn3上创建 使用的分片规则是 auto-sharding-long
<table name=
"tb_user_info"
dataNode=
"dn1,dn2,dn3"
rule=
"auto-sharding-long"
/>
</schema>
#配置dataNode 如果业务需要的话,可以配置多个dataHost 指定多个节点
<dataNode name=
"dn1"
dataHost=
"localhost1"
database=
"db1"
/>
<dataNode name=
"dn2"
dataHost=
"localhost1"
database=
"db2"
/>
<dataNode name=
"dn3"
dataHost=
"localhost1"
database=
"db3"
/>
<dataHost name=
"localhost1"
maxCon=
"1000"
minCon=
"10"
balance=
"0"
writeType=
"0"
dbType=
"mysql"
dbDriver=
"native"
switchType=
"1"
slaveThreshold=
"100"
>
<heartbeat>select
user
()</heartbeat>
<writeHost host=
"hostM1"
url=
"192.168.244.11:3306"
user=
"root"
password=
"111111"
>
</writeHost>
</dataHost>
|
在schema.xml 中配置好的表名,实际上只是一个逻辑的表,这个表在物理数据库中并不存在,需要在MyCat通过Create Table 来创建这个表,执行Create语句以后,MyCat会在真实MySql配置的数据库中创建表。
配置完以后,保存退出,重启MyCat
3、auto-sharding-long分片规则
在上面的例子中使用到auto-sharding-long分片规则,在这里要说明一下这个分片规则的实现原理。
在mycat/conf.rule.xml中定义了分片规则的实现原理 auto-sharding-long 分片规则是这样的:
<tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> </function>
可见这个TableRule是通过id 来进行分片的,分片的算法是rang-long 下面在算法中使用到了autopartition-long.txt(mycat/conf下),打开看看:
1
2
3
4
5
|
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
|
K=1000条记录,M=10000条记录,那么下面三个配置就是0~500万的记录会存在数据库db1的表中,500万~1000万会存在db2的表中,1000万~1500万会存在db3的表中。
4、牛刀小试
$ ../bin/mycat start
$ ../bin/mycat status 查看MyCat状态
$ ps -ef | grep mycat 查看MyCat进程
$ ss -tanl 查看端口监听情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
[root@node1 bin]# mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB
...
mysql> show tables;
+
------------------+
| Tables
in
TESTDB |
+
------------------+
| company |
| customer |
| customer_addr |
| employee |
| goods |
| hotnews |
| orders |
| order_items |
| tb_user_info |
+
------------------+
9
rows
in
set
(0.00 sec)
-- 在MyCat中创建物理表
mysql>
create
table
tb_user_info (id
bigint
not
null
auto_increment
primary
key
,
name
varchar
(100));
Query OK, 0
rows
affected (0.16 sec)
-- 查看物理表
mysql> use db1;
mysql> show tables;
+
---------------+
| Tables_in_db1 |
+
---------------+
| tb_user_info |
+
---------------+
mysql> use db2;
Database
changed
mysql> show tables;
+
---------------+
| Tables_in_db2 |
+
---------------+
| tb_user_info |
+
---------------+
1 row
in
set
(0.00 sec)
mysql> use db3
Database
changed
mysql> show tables;
+
---------------+
| Tables_in_db3 |
+
---------------+
| tb_user_info |
+
---------------+
1 row
in
set
(0.00 sec)
|
可以在MySql中的三个数据库中看到,表确实已经创建了。
下面我们分别向表中插入三条数据分别ID是 100,6000000,11000000,看是否正常分配到三个表中:
能过在MyCat中执行explain SQL语句,可以查看插入的记录将会被分配到哪个表中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
insert
into
tb_user_info(id,
name
)
values
(100,
'lucy'
);
insert
into
tb_user_info(id,
name
)
values
(6000000,
'lily'
);
insert
into
tb_user_info(id,
name
)
values
(11000000,
'tom'
);
mysql> explain
insert
into
tb_user_info(id,
name
)
values
(100,
'lucy'
);
+
-----------+------------------------------------------------------+
| DATA_NODE | SQL |
+
-----------+------------------------------------------------------+
| dn1 |
insert
into
tb_user_info(id,
name
)
values
(100,
'lucy'
) |
+
-----------+------------------------------------------------------+
1 row
in
set
(0.09 sec)
mysql> explain
insert
into
tb_user_info(id,
name
)
values
(6000000,
'lily'
);
+
-----------+----------------------------------------------------------+
| DATA_NODE | SQL |
+
-----------+----------------------------------------------------------+
| dn2 |
insert
into
tb_user_info(id,
name
)
values
(6000000,
'lily'
) |
+
-----------+----------------------------------------------------------+
1 row
in
set
(0.00 sec)
mysql> explain
insert
into
tb_user_info(id,
name
)
values
(11000000,
'tom'
);
+
-----------+----------------------------------------------------------+
| DATA_NODE | SQL |
+
-----------+----------------------------------------------------------+
| dn3 |
insert
into
tb_user_info(id,
name
)
values
(11000000,
'tom'
) |
+
-----------+----------------------------------------------------------+
1 row
in
set
(0.00 sec)
|
实际物理表中查询结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
mysql>
select
*
from
tb_user_info;
+
-----+------+
| id |
name
|
+
-----+------+
| 100 | lucy |
+
-----+------+
1 row
in
set
(0.00 sec)
mysql>
select
*
from
db2.tb_user_info;
+
---------+------+
| id |
name
|
+
---------+------+
| 6000000 | lily |
+
---------+------+
1 row
in
set
(0.00 sec)
mysql>
select
*
from
db3.tb_user_info;
+
----------+------+
| id |
name
|
+
----------+------+
| 11000000 | tom |
+
----------+------+
1 row
in
set
(0.00 sec)
|
至此一个简单的MyCat分片技术就实现了,后续会有更多MyCat分片规则的介绍!