一、安装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分片规则的介绍!
此外,有朋友可能在使用mongdb也想使用mycat 来进行分库分表等操作;我在这里用一个案例;来演示mycat对mongdb进行分库(因实际原因分表就不演示了)
第一步:service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
<property name="charset">utf8</property>
<property name="sequnceHandlerType">2</property>
<!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
<!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
<property name="processorBufferPoolType">0</property>
<!--默认是65535 64K 用于sql解析时最大文本长度 -->
<!--<property name="maxStringLiteralLength">65535</property>-->
<!--<property name="sequnceHandlerType">0</property>-->
<!--<property name="backSocketNoDelay">1</property>-->
<!--<property name="frontSocketNoDelay">1</property>-->
<!--<property name="processorExecutor">16</property>-->
<!--
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
<property name="handleDistributedTransactions">0</property>
<property name="serverPort">8066</property>
<!--
off heap for merge/order/group/limit 1开启 0关闭
-->
<property name="useOffHeapForMerge">1</property>
<!--
单位为m
-->
<property name="memoryPageSize">64k</property>
<!--
单位为k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
单位为m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采用zookeeper协调切换 -->
<property name="useZKSwitch">true</property>
<!-- XA Recovery Log日志路径 -->
<!--<property name="XARecoveryLogBaseDir">./</property>-->
<!-- XA Recovery Log日志名称 -->
<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
</system>
<!-- 全局SQL防火墙设置 -->
<!--白名单可以使用通配符%或着*-->
<!--例如<host host="127.0.0.*" user="root"/>-->
<!--例如<host host="127.0.*" user="root"/>-->
<!--例如<host host="127.*" user="root"/>-->
<!--例如<host host="1*7.*" user="root"/>-->
<!--这些配置情况下对于127.0.0.1都能以root账户登录-->
<!--
<firewall>
<whitehost>
<host host="1*7.0.0.*" user="root"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
-->
<user name="root">
<property name="password">123456</property>
<property name="schemas">Ch_Gps</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">Ch_Gps</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
第二步:schema.xml 按月分库存储数据