mongodb是目前开源数据库中最常用的NoSQL数据库, MongoDb在用于生产环境的三种模式,master/slaves(主从模式);replica set(副本集);auto shard 分片模式,本文主要介绍如何搭建高可用的mongodb副本+分片(Replication)集群。

一、概念

在搭建集群之前,需要首先了解几个概念:路由、副本集、分片、配置服务器等。
概念图

1.路由 mongos

数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加一个路由选择器,mongos自己就是一个请求分发中心,它负责把对应的数据请求请求转发到对应的shard服务器上。在生产环境通常有多mongos作为请求的入口,防止其中一个挂掉所有的mongodb请求都没有办法操作。

2.副本集 replica set

MongoDB的replica set是一个mongod进程实例簇,数据在这个簇中相互复制,并自动进行故障切换。
MongoDB的数据库复制增加了冗余,确保了高可用性,简化了管理任务如备份,并且增加了读能力。大多数产品部署都使用了复制。MongoDB中primary处理写操作,其它进行复制的成员则是secondaries。

3.分片 sharding

到目前为止,你都是把MongoDB当做一台服务器在用,每个mongod实例都包含应用程序数据的完整副本。就算使用了复制,每个副本也都是完整克隆了其他副本的数据。对于大多数应用程序而言,在一台服务器上保存完整数据集是完全可以接受的。但随着数据量的增长,以及应用程序对读写吞吐量的要求越来越高,普通服务器渐渐显得捉襟见肘了。尤其是这些服务器可能无法分配足够的内存,或者没有足够的CPU核数来有效处理工作负荷。除此之外,随着数据量的增长,要在一块磁盘或者一组RAID阵列上保存和管理备份如此大规模的数据集也变得不太现实。如果还想继续使用普通硬件或者虚拟硬件来托管数据库,那么这对这类问题的解决方案就是将数据库分布到多台服务器上,这种方法称之为分片。
一个副本集可以最多支持12个成员,但是只有7个成员可以参与投票。

4.配置服务器config server

顾名思义为配置服务器,存储所有数据库元信息(路由、分片)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是缓存在内存里,配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从 config server 加载配置信息,以后如果配置服务器信息变化会通知到所有的 mongos 更新自己的状态,这样 mongos 就能继续准确路由。在生产环境通常有多个 config server 配置服务器,因为它存储了分片路由的元数据,防止数据丢失!

二、环境准备

1.资源

系统系统 centos7,三台服务器:192.168.22.101/102/103,下载MongoDB安装包: mongodb-linux-x86_64-3.4.6.tgz

2,服务器规划

| 服务器101   |   服务器102 | 服务器103|
|-----------|------------|---------|
| mongos     |   mongos   | mongos  |
| config server |    config server |    config server |
| shard server1 主节点    |    shard server1 副节点 |    shard server1 仲裁 |
| shard server2 仲裁      |    shard server2 主节点 |    shard server2 副节点 |
| shard server3 副节点    |    shard server3 仲裁 |    shard server3 主节点 |
----------

端口分配:
    mongos:20000
    config:21000
    shard1:27001
    shard2:27002
    shard3:27003

3、架构图

架构图

三、集群搭建

1、安装mongodb

解压
1
tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/
改名

mv mongodb-linux-x86_64-3.4.6 mongodb
分别在每台机器建立conf、mongos、config、shard1、shard2、shard3六个目录,因为mongos不存储数据,只需要建立日志文件目录即可。

1
2
3
4
5
6
7
8
9
10
mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/mongos/log
mkdir -p /usr/local/mongodb/config/data
mkdir -p /usr/local/mongodb/config/log
mkdir -p /usr/local/mongodb/shard1/data
mkdir -p /usr/local/mongodb/shard1/log
mkdir -p /usr/local/mongodb/shard2/data
mkdir -p /usr/local/mongodb/shard2/log
mkdir -p /usr/local/mongodb/shard3/data
mkdir -p /usr/local/mongodb/shard3/log

#####配置环境变量

1
vim /etc/profile
内容
1
2
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
使立即生效
1
source /etc/profile

2、config server配置服务器

mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。
添加配置文件

1
vi /usr/local/mongodb/conf/config.conf
配置文件内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true

bind_ip = 0.0.0.0
port = 21000
fork = true

#declare this is a config db of a cluster;
configsvr = true

#副本集名称
replSet=configs

#设置最大连接数
maxConns=20000

启动三台服务器的config server

1
mongod -f /usr/local/mongodb/conf/config.conf

登录任意一台配置服务器,初始化配置副本集
#####连接

1
mongo --port 21000

#####config变量

1
2
3
4
5
6
7
8
config = {
_id : "configs",
members : [
{_id : 0, host : "192.168.22.101:21000" },
{_id : 1, host : "192.168.22.102:21000" },
{_id : 2, host : "192.168.22.103:21000" }
]
}

#####初始化副本集

1
rs.initiate(config)

其中,”_id” : “configs”应与配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 为三个节点的 ip 和 port

3、配置分片副本集(三台机器)

设置第一个分片副本集
配置文件

1
vi /usr/local/mongodb/conf/shard1.conf

#####配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 0.0.0.0
port = 27001
fork = true

#副本集名称
replSet=shard1

#declare this is a shard db of a cluster;
shardsvr = true

#设置最大连接数
maxConns=20000

启动三台服务器的shard1 server

1
mongod -f /usr/local/mongodb/conf/shard1.conf

登陆任意一台服务器,初始化副本集

1
mongo --port 27001

#####使用admin数据库

1
use admin

#####定义副本集配置,第三个节点的 “arbiterOnly”:true 代表其为仲裁节点。

1
2
3
4
5
6
7
config = {
_id : "shard1",
members:[{_id : 0, host : "192.168.22.101:27001" },
{_id : 1, host : "192.168.22.102:27001" },
{_id : 2, host : "192.168.22.103:27001", arbiterOnly: true }
]
}

#####初始化副本集配置

1
rs.initiate(config);

设置第二个分片副本集
配置文件

1
vi /usr/local/mongodb/conf/shard2.conf

#####配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true

#副本集名称
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true

#设置最大连接数
maxConns=20000

启动三台服务器的shard2 server

1
mongod -f /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

1
mongo --port 27002

#####使用admin数据库

1
use admin

#####定义副本集配置

1
2
3
4
5
6
7
8
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.22.101:27002" , arbiterOnly: true },
{_id : 1, host : "192.168.22.102:27002" },
{_id : 2, host : "192.168.22.103:27002" }
]
}

#####初始化副本集配置

1
rs.initiate(config);

设置第三个分片副本集
配置文件

1
vi /usr/local/mongodb/conf/shard3.conf

#####配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true

#副本集名称
replSet=shard3

#declare this is a shard db of a cluster;
shardsvr = true

#设置最大连接数
maxConns=20000

启动三台服务器的shard3 server

1
mongod -f /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

1
mongo --port 27003

#####使用admin数据库

1
use admin

#####定义副本集配置

1
2
3
4
5
6
7
8
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.22.101:27003" },
{_id : 1, host : "192.168.22.102:27003" , arbiterOnly: true},
{_id : 2, host : "192.168.22.103:27003" }
]
}

#####初始化副本集配置

1
rs.initiate(config);

###4、配置路由服务器 mongos
先启动配置服务器和分片服务器,后启动路由实例启动路由实例:(三台机器)

1
vi /usr/local/mongodb/conf/mongos.conf

#####内容

1
2
3
4
5
6
7
8
9
10
11
12
13
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20000
fork = true

#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.22.101:21000,192.168.22.102:21000,192.168.22.103:21000

#设置最大连接数
maxConns=20000

启动三台服务器的mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf

###5、启用分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
mongo –port 20000
#使用admin数据库

1
2
3
4
5
6
7
use  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.22.101:27001,192.168.22.102:27001,192.168.22.103:27001")
sh.addShard("shard2/192.168.22.101:27002,192.168.22.102:27002,192.168.22.103:27002")
sh.addShard("shard3/192.168.22.101:27003,192.168.22.102:27003,192.168.22.103:27003")
#查看集群状态
sh.status()

###6、测试
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
#####指定testdb分片生效
db.runCommand( { enablesharding :”testdb”});

use tested
db.table1.ensureIndex({id:1})

#####指定数据库里需要分片的集合和片键

db.runCommand( { shardcollection : “testdb.table1”,key : {id: 1} } )

#####如果是采用hash分片
#####db.runCommand( { shardcollection : “testdb.table1”,key : {_id: “hashed”} } )

我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
测试分片配置结果
mongo 127.0.0.1:20000

#####使用testdb
use testdb;

#####插入测试数据
for (var i = 1; i <= 100; i++)db.table2.save({id:i,”test1”:”testval1”});
#####查看分片情况如下,部分无关信息省掉了

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
db.table1.stats();

{
"sharded" : true,
"ns" : "testdb.table1",
"count" : 100000,
"numExtents" : 13,
"size" : 5600000,
"storageSize" : 22372352,
"totalIndexSize" : 6213760,
"indexSizes" : {
"_id_" : 3335808,
"id_1" : 2877952
},
"avgObjSize" : 56,
"nindexes" : 2,
"nchunks" : 3,
"shards" : {
"shard1" : {
"ns" : "testdb.table1",
"count" : 42183,
"size" : 0,
...
"ok" : 1
},
"shard2" : {
"ns" : "testdb.table1",
"count" : 38937,
"size" : 2180472,
...
"ok" : 1
},
"shard3" : {
"ns" : "testdb.table1",
"count" :18880,
"size" : 3419528,
...
"ok" : 1
}
},
"ok" : 1
}

可以看到数据分到3个分片,各自分片数量为: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已经成功了!
后期运维
启动关闭
mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.

1
2
3
4
5
mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf

关闭时,直接killall杀掉所有进程

1
2
killall mongod
killall mongos