上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建

首先我们再来了解一下分片集群的架构,分片集群由三部分构成:

  • mongos:查询路由,在客户端程序和分片之间提供接口。本次实验部署2个mongos实例
  • config:配置服务器存储集群的元数据,元数据反映分片集群的内所有数据和组件的状态和组织方式,元数据包含每个分片上的块列表以及定义块的范围。从3.4版本开始,已弃用镜像服务器用作配置服务器(SCCC),config Server必须部署为副本集架构(CSRS)。本次实验配置一个3节点的副本集作为配置服务器
  • shard:每个shard包含集合的一部分数据,从3.6版本开始,每个shard必须部署为副本集(replica set)架构。本次实验部署3个分片存储数据。

(一)主机信息

(二)配置服务器副本集搭建

配置服务器三个实例的基础规划如下:

member0 192.168.10.80:27017
member1 192.168.10.80:27018
member2 192.168.10.80:27019

其参数规划如下:

接下来,我们一步一步搭建config server的副本集。

STEP1:解压mongodb安装包到/mongo目录

[root@mongosserver mongo]# pwd
/mongo
[root@mongosserver mongo]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES THIRD-PARTY-NOTICES.gotools

STEP2:根据上面参数规划,创建数据存放相关路径

# 创建文件路径
mkdir -p /replset/repset1/data
mkdir -p /replset/repset1/log
mkdir -p /replset/repset2/data
mkdir -p /replset/repset2/log
mkdir -p /replset/repset3/data
mkdir -p /replset/repset3/log [root@mongosserver repset1]# tree /replset/
/replset/
├── repset1
│   ├── data
│   ├── log
│   └── mongodb.conf
├── repset2
│   ├── data
│   └── log
└── repset3
├── data
└── log

STEP3:为3个实例创建参数文件

实例1的参数文件  /replset/repset1/mongodb.conf  :

systemLog:
destination: file
logAppend: true
path: /replset/repset1/log/mongodb.log storage:
dbPath: /replset/repset1/data
journal:
enabled: true processManagement:
fork: true # fork and run in background
pidFilePath: /replset/repset1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo # network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # shard
sharding:
clusterRole: configsvr # repliuca set
replication:
replSetName: conf

实例2的参数文件  /replset/repset2/mongodb.conf :

systemLog:
destination: file
logAppend: true
path: /replset/repset2/log/mongodb.log storage:
dbPath: /replset/repset2/data
journal:
enabled: true processManagement:
fork: true # fork and run in background
pidFilePath: /replset/repset2/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo # network interfaces
net:
port: 27018
bindIp: 0.0.0.0 # shard
sharding:
clusterRole: configsvr # repliuca set
replication:
replSetName: conf

实例3的参数文件  /replset/repset3/mongodb.conf :

systemLog:
destination: file
logAppend: true
path: /replset/repset3/log/mongodb.log storage:
dbPath: /replset/repset3/data
journal:
enabled: true processManagement:
fork: true # fork and run in background
pidFilePath: /replset/repset3/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo # network interfaces
net:
port: 27019
bindIp: 0.0.0.0 # shard
sharding:
clusterRole: configsvr # repliuca set
replication:
replSetName: conf

STEP4:启动三个mongod实例

mongod -f /replset/repset1/mongodb.conf
mongod -f /replset/repset2/mongodb.conf
mongod -f /replset/repset3/mongodb.conf # 查看是成功否启动
[root@mongosserver mongo]# netstat -nltp |grep mongod
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 28009/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 27928/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 27970/mongod

STEP5:进入任意一个实例,初始化配置服务器的副本集

rs.initiate(
{
_id: "conf",
configsvr: true,
members: [
{ _id : 0, host : "192.168.10.80:27017" },
{ _id : 1, host : "192.168.10.80:27018" },
{ _id : 2, host : "192.168.10.80:27019" }
]
}
)

STEP6:[可选] 调整节点优先级,以便于确定主节点

cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1
rs.reconfig(cfg)

对于members[n]的定义:n是members数组中的数组位置,数组以0开始,千万不能将其理解为“members[n]._id”的_id值。

查看节点优先级:

conf:PRIMARY> rs.config()

(三)分片副本集搭建

分片1副本集成员:
member0 192.168.10.81:27017
member1 192.168.10.81:27018
member2 192.168.10.81:27019

分片2副本集成员:
member0 192.168.10.82:27017
member1 192.168.10.82:27018
member2 192.168.10.82:27019

分片3副本集成员:
member0 192.168.10.83:27017
member1 192.168.10.83:27018
member2 192.168.10.83:27019

其参数规划如下:

这里一共有3个分片,每个分片都是3个节点的副本集,副本集的搭建过程与上面config server副本集搭建过程相似,这里不再重复赘述,唯一不同的是副本集的初始化。shard副本集的初始化与配置副本集初始化过程相比,少了 configsvr: true 的参数配置。

三个shard副本集的初始化:

# shard001
rs.initiate(
{
_id: "shard001",
members: [
{ _id : 0, host : "192.168.10.81:27017" },
{ _id : 1, host : "192.168.10.81:27018" },
{ _id : 2, host : "192.168.10.81:27019" }
]
}
)
# shard002
rs.initiate(
{
_id: "shard002",
members: [
{ _id : 0, host : "192.168.10.82:27017" },
{ _id : 1, host : "192.168.10.82:27018" },
{ _id : 2, host : "192.168.10.82:27019" }
]
}
) # shard003
rs.initiate(
{
_id: "shard003",
members: [
{ _id : 0, host : "192.168.10.83:27017" },
{ _id : 1, host : "192.168.10.83:27018" },
{ _id : 2, host : "192.168.10.83:27019" }
]
}
)

(四)配置并启动mongos

本次试验在192.168.10.100服务器上启动2个mongos进程,分别使用端口27000和28000。

STEP1:配置mongos实例的参数

端口27000参数配置,特别注意,需要先创建涉及到的路径:

systemLog:
destination: file
logAppend: true
path: /mongo/log/mongos-27000.log processManagement:
fork: true # fork and run in background
pidFilePath: /mongo/mongod-27000.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo # network interfaces
net:
port: 27000
bindIp: 0.0.0.0 sharding:
configDB: conf/192.168.10.80:27017,192.168.10.80:27018,192.168.10.80:27019

端口28000参数配置,特别注意,需要先创建涉及到的路径:

systemLog:
destination: file
logAppend: true
path: /mongo/log/mongos-28000.log processManagement:
fork: true # fork and run in background
pidFilePath: /mongo/mongod-28000.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo # network interfaces
net:
port: 28000
bindIp: 0.0.0.0 sharding:
configDB: conf/192.168.10.80:27017,192.168.10.80:27018,192.168.10.80:27019

STEP2:启动mongos实例

# 启动mongos实例
[root@mongosserver mongo]# mongos -f /mongo/mongos-27000.conf
[root@mongosserver mongo]# mongos -f /mongo/mongos-28000.conf # 查看实例信息
[root@mongosserver mongo]# netstat -nltp|grep mongos
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 2209/mongos
tcp 0 0 0.0.0.0:28000 0.0.0.0:* LISTEN 2241/mongos

(五)添加分片到集群配置服务器

STEP1:使用mongo连接到mongos

mongo --host 192.168.10.100 --port 27000
# 或者
mongo --host 192.168.10.100 --port 28000

STEP2:添加分片到集群

sh.addShard( "shard001/192.168.10.81:27017,192.168.10.81:27018,192.168.10.81:27019")
sh.addShard( "shard002/192.168.10.82:27017,192.168.10.82:27018,192.168.10.82:27019")
sh.addShard( "shard003/192.168.10.83:27017,192.168.10.83:27018,192.168.10.83:27019")

STEP3:查看分片信息

mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ffc0709b040c53d59c15c66")
}
shards:
{ "_id" : "shard001", "host" : "shard001/192.168.10.81:27017,192.168.10.81:27018,192.168.10.81:27019", "state" : 1 }
{ "_id" : "shard002", "host" : "shard002/192.168.10.82:27017,192.168.10.82:27018,192.168.10.82:27019", "state" : 1 }
{ "_id" : "shard003", "host" : "shard003/192.168.10.83:27017,192.168.10.83:27018,192.168.10.83:27019", "state" : 1 }
active mongoses:
"4.2.10" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true } mongos>

(六)启用分片

(6.1)对数据库启用分片
分片是以集合为单位进行的,在对一个集合进行分片之前,需要先对其数据库启用分片,对数据库启用分片并不会重新分发数据,只是说明该数据库上的集合可以进行分片操作。

sh.enableSharding("lijiamandb");

(6.2)对集合启用分片

如果集合已经存在数据,必须手动创建在分片键上创建索引,然后再对集合进行分片,如果集合为空,MongoDB会在分片的时候自动在分片键上创建索引。
mongodb提供了2种策略来对集合进行分片:

  • 哈希(hash)分片,对单列使用hash索引作为分片键
sh.shardCollection("<database>.<collection>",{shard key field : "hashed"}) 
  • 范围(range)分片,可以使用多个字段作为分片键,并将数据划分为由分片键确定的连续范围
sh.shardCollection("<database>.<collection>",{<shard key field>:1,...} )

例子:对集合user进行hash分片

// 连接到mongos创建集合
use lijiamandb for (i=1;i<100000;i++){
db.user.insert({
"id" : i,
"name" : "name"+i,
"age" : Math.floor(Math.random()*120),
"created" : new Date()
});
} // 使用mongostat可以看到,所有数据都写入到了主节点(shard2),每个数据库的主节点可能不同,可以使用sh.status()查看。
[root@mongosserver ~]# mongostat --port 27000 5 --discover
host insert query update delete getmore command dirty used flushes mapped vsize res faults qrw arw net_in net_out conn set repl time
localhost:27000 352 *0 *0 *0 0 704|0 0 0B 356M 32.0M 0 0|0 0|0 224k 140k 10 RTR Jan 15 10:52:32.046 host insert query update delete getmore command dirty used flushes mapped vsize res faults qrw arw net_in net_out conn set repl time
192.168.10.81:27017 *0 *0 *0 *0 0 2|0 0.3% 0.8% 0 1.90G 133M n/a 0|0 1|0 417b 9.67k 23 shard001 SEC Jan 15 10:52:32.061
192.168.10.81:27018 *0 *0 *0 *0 0 3|0 0.3% 0.8% 1 1.93G 132M n/a 0|0 1|0 1.39k 11.0k 28 shard001 PRI Jan 15 10:52:32.067
192.168.10.81:27019 *0 *0 *0 *0 0 2|0 0.3% 0.8% 0 1.95G 148M n/a 0|0 1|0 942b 10.2k 26 shard001 SEC Jan 15 10:52:32.070
192.168.10.82:27017 352 *0 *0 *0 407 1192|0 2.5% 11.7% 1 1.99G 180M n/a 0|0 1|0 1.52m 1.15m 29 shard002 PRI Jan 15 10:52:32.075
192.168.10.82:27018 *352 *0 *0 *0 409 441|0 4.5% 8.9% 0 1.96G 163M n/a 0|0 1|0 566k 650k 25 shard002 SEC Jan 15 10:52:32.085
192.168.10.82:27019 *352 *0 *0 *0 0 2|0 4.4% 9.7% 0 1.92G 168M n/a 0|0 1|0 406b 9.51k 24 shard002 SEC Jan 15 10:52:32.093
192.168.10.83:27017 *0 *0 *0 *0 0 1|0 0.2% 0.6% 1 1.89G 130M n/a 0|0 1|0 342b 9.17k 22 shard003 SEC Jan 15 10:52:32.099
192.168.10.83:27018 *0 *0 *0 *0 0 2|0 0.2% 0.6% 0 1.95G 139M n/a 0|0 1|0 877b 9.92k 28 shard003 PRI Jan 15 10:52:32.107
192.168.10.83:27019 *0 *0 *0 *0 0 1|0 0.2% 0.6% 0 1.90G 133M n/a 0|0 1|0 342b 9.17k 21 shard003 SEC Jan 15 10:52:32.113
localhost:27000 365 *0 *0 *0 0 731|0 0 0B 356M 32.0M 0 0|0 0|0 233k 145k 10 RTR Jan 15 10:52:37.047 // 使用分片键id创建hash分片,因为id上没有hash索引,会报错
sh.shardCollection("lijiamandb.user",{"id":"hashed"})
/* 1 */
{
"ok" : 0.0,
"errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection",
"code" : 72,
"codeName" : "InvalidOptions",
"operationTime" : Timestamp(1610679762, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1610679762, 4),
"signature" : {
"hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
"keyId" : NumberLong(0)
}
}
} // 需要手动创建hash索引
db.user.ensureIndex() // 查看索引
/* 1 */
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "lijiamandb.user"
},
{
"v" : 2,
"key" : {
"id" : "hashed"
},
"name" : "id_hashed",
"ns" : "lijiamandb.user"
}
] // 最后在重新分片即可

到这里,我们分片集群环境已经搭建完成,接下来我们将会学习分片键的选择机制。

【完】

相关文档合集:

1. MongoDB Sharding(一) -- 分片的概念
2. MongoDB Sharding(二) -- 搭建分片集群
3. MongoDB Sharding(三) -- zone
4. MongoDB Sharding(四) -- 分片集群的维护管理

MongoDB Sharding(二) -- 搭建分片集群的更多相关文章

  1. MongoDB 搭建分片集群

    在MongoDB(版本 3.2.9)中,分片是指将collection分散存储到不同的Server中,每个Server只存储collection的一部分,服务分片的所有服务器组成分片集群.分片集群(S ...

  2. mongo 3.4分片集群系列之二:搭建分片集群--哈希分片

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  3. mongo 3.4分片集群系列之三:搭建分片集群--哈希分片 + 安全

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  4. mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  5. MongoDB在windows平台分片集群部署

    本文转载自:https://www.cnblogs.com/hx764208769/p/4260177.html 前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多 ...

  6. MongoDB部署实战(一)MongoDB在windows平台分片集群部署

    前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多类型,错误的,操作的,...用MongoDB存储日志,大量的日志产生,大量读写吞吐量很大的时候,单个Server很 ...

  7. mongodb3.6集群搭建:分片集群认证

    上篇集群已经创建,现在加入认证. 1. 生成密钥文件每个服务器上创建路径: mkdir -p /var/lib/mongo/auth 生成64字节的密钥文件openssl rand -base64 6 ...

  8. 网易云MongoDB分片集群(Sharding)服务已上线

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MongoDB sharding cluster(分片集群)是MongoDB提供的数据在线水平扩展方案,包括 ...

  9. Mongodb主从复制/ 副本集/分片集群介绍

    前面的文章介绍了Mongodb的安装使用,在 MongoDB 中,有两种数据冗余方式,一种 是 Master-Slave 模式(主从复制),一种是 Replica Sets 模式(副本集). Mong ...

随机推荐

  1. 【APIO2019】桥梁(询问分块)

    Description 给定一张 \(n\) 个点,\(m\) 条边的无向图,边 \(i\) 的权值为 \(d_i\).现有 \(q\) 次操作,第 \(j\) 个操作有两种模式: \(1\ b_j\ ...

  2. OpenWrt下基于OLSR的Ad-Hoc组网实现网络摄像头多节点访问

    文章目录 Ad-Hoc组网配置 摄像头端口映射 PC连接设置 结果 Ad-Hoc组网配置 参照博客 链接: link. 摄像头端口映射 这里使用到了海康网络摄像头,先将网络摄像头的网口连接到任意一个节 ...

  3. vue-cli脚手架搭建vue3.0+typescripe项目

    新开个项目,小项目,小.顺手就用vue吧,vue3出来也几个月了,直接上了吧.一年多没用vue了,用的时候也得再熟悉,不如直接干3了! vue官方推荐使用的脚手架是 Vite 和 vue-cli ,延 ...

  4. sqli-labs less8-10(布尔盲注时间盲注)

    less-8 布尔盲注 首先利用?id=1' and 1=1 --+和?id=1' and 1=2 --+确定id的类型为单引号''包裹.然后进行盲注. 盲注思路: 破解当前数据库名: and len ...

  5. CSS-backgroound和radial-giadient的常见用法

    前言 这里主要介绍下css中background和radial-giadient径向渐变的使用,工作中用到的地方可能也不太多,但是每次用到了都需要查阅官网,查资料就比较麻烦,这里记录一下我自己整理的常 ...

  6. oracle DG查看延时时间

    oracle DG查看延时时间 SQL> select value from v$dataguard_stats where name='apply lag'; 例如: SQL> sele ...

  7. double类型和int类型的区别

    引例: double a=19*3.3; System.out.print(a); 结果为62.9999996,不是62.7:这里不单纯是因为给的是double类型 (1) 62.7 和 62.699 ...

  8. db2常用操作

    1. db2建立远程节点编目及删除 db2 catalog tcpip node nodeName remote remoteIp server remotePort db2 list node di ...

  9. Hexo结合Stun静态博客搭建从入门到入土

    摘要 安装npm,安装hexo相关依赖,安装主题stun 修改hexo配置,修改stun配置,部署到github,gitee实现静态访问 给博客加上全局搜索,访问量统计 hexo博客编写模板 tips ...

  10. 如何最简单、通俗地理解Python的文件?

    原文链接:https://www.zhihu.com/question/431437471/answer/1588566615 一.笔记 1) Python文件 ① Python文件后缀一般以 .py ...