mongodb3.4+sharding分片集群环境搭建

2023-05-16

背景:mongodb集群搭建方式有三种,1、主从(官方已经不推荐),2、副本集,3、分片。这里介绍如何通过分片sharding方式搭建mongodb集群。sharding集群方式也基于副本集,在搭建过程中,需要对分片和配置节点做副本集。最后将做好的副本集的分片加入到路由节点,构成集群。

sharding方式的集群中,有三类角色,分别是shard,config,router。如下图所示。


shard:分片节点,存储数据。

config:配置节点,不会存储数据,会存储元数据信息,比如片键的范围。

router:路由节点,是mongo集群与外部客户端连接的入口,他提供mongos客户端,对客户端透明,让客户端感觉使用单节点数据库。

环境介绍:

   这里设计的集群如下节点分别如下:

   router : 127.0.0.1:27017

   config : config/127.0.0.1:27117,127.0.0.1:27217,127.0.0.1:27317

   shard : shard1/127.0.0.1:27018,127.0.0.1:27019  shard2/127.0.0.1:27118,127.0.0.1:27119

搭建步骤:

mongodb下载地址:

http://downloads.mongodb.org/linux/mongodb-linux-x86_64-3.4.0.tgz?_ga=2.55492392.2036626358.1530260071-2147164267.1528775467

如果想要下载其他版本,可以在这里选择:http://dl.mongodb.org/dl/linux。

一、设置三类配置文件,分片节点,配置节点,路由节点。

shard01.conf

port=27018
fork=true
dbpath=/home/hadoop/software/mongodb-3.4/data/shard01
logpath=/home/hadoop/software/mongodb-3.4/logs/shard01.log
logappend=true
bind_ip=127.0.0.1
replSet=shard1
shardsvr=true

shard02.conf配置文件和shard02.conf配置文件的区别就是端口改为了27019,另外数据存储路径和日志路径均和文件名一致。

shard21.conf

port=27118
fork=true
dbpath=/home/hadoop/software/mongodb-3.4/data/shard21
logpath=/home/hadoop/software/mongodb-3.4/logs/shard21.log
logappend=true
bind_ip=127.0.0.1
replSet=shard2

shardsvr=true

shard22.conf配置文件和shard21.conf配置文件的区别就是端口改为了27119,另外数据存储路径和日志路径均和文件名一致。

config.conf

port=27117
fork=true
dbpath=/home/hadoop/software/mongodb-3.4/data/config
logpath=/home/hadoop/software/mongodb-3.4/logs/config.log
logappend=true
bind_ip=127.0.0.1
replSet=config
configsvr=true

config2.conf,config3.conf配置文件和config.conf配置文件的区别就是端口改为了27217和27317,另外数据存储路径和日志路径均和文件名一致。

router.conf

port=27017
configdb=config/127.0.0.1:27117,127.0.0.1:27217,127.0.0.1:27317
logpath=/home/hadoop/software/mongodb-3.4/logs/router.log
fork=true
logappend=true

关于配置文件说明:

shard01.conf,shard02.conf是shard1分片的配置。

shard21.conf,shard22.conf是shard2分片的配置。

config.conf,config2.conf,config3.conf是config节点配置。

二、启动分片节点和配置节点。

启动脚本内容:


#!/bin/sh
rm -rf data/*/*
rm -f logs/*

bin/mongod -f conf/shard01.conf
bin/mongod -f conf/shard02.conf
bin/mongod -f conf/shard21.conf
bin/mongod -f conf/shard22.conf
bin/mongod -f conf/config.conf
bin/mongod -f conf/config2.conf
bin/mongod -f conf/config3.conf  

启动之后,显示如下信息,表明启动成功。


[root@server mongodb-3.4]# ./start_mongod.sh 
about to fork child process, waiting until server is ready for connections.
forked process: 4870
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 4895
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 4920
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 4944
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 4970
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 5002
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 5035
child process started successfully, parent exiting  

三、配置节点构成副本集。


> var config = {_id:'config',members:[{_id:0,host:'127.0.0.1:27117'},{_id:1,host:'127.0.0.1:27217'},{_id:2,host:'127.0.0.1:27317'}]}
> rs.initiate(config)
{ "ok" : 1 }
config:SECONDARY> 
config:SECONDARY> 
config:PRIMARY> exit  

配置分片之后,等待10S左右,当前节点会变为primary节点。

四、分片节点构成副本集。

登录127.0.0.1:27018

bin/mongo --port 27018


> var config = {_id:'shard1',members:[{_id:0,host:'127.0.0.1:27018'},{_id:1,host:'127.0.0.1:27019'}]}
> rs.initiate(config)
{ "ok" : 1 }
shard1:SECONDARY> 
shard1:SECONDARY> 
shard1:PRIMARY>   

接着登录127.0.0.1:27118

bin/mongo --port 27118


> var config = {_id:'shard2',members:[{_id:0,host:'127.0.0.1:27118'},{_id:1,host:'127.0.0.1:27119'}]}
> rs.initiate(config)
{ "ok" : 1 }
shard2:SECONDARY> 
shard2:PRIMARY>   

两个分片shard1,shard2设置成功之后,等待10s左右就会发现当前节点默认变为了当前分片的primary节点。

五、启动路由节点,并增加分片。

启动脚本内容:


#!/bin/sh
bin/mongos -f conf/router.conf  

增加分片格式如下:"shard1/127.0.0.1:27018,127.0.0.1:27019"。


[root@server mongodb-3.4]# ./start_mongos.sh 
about to fork child process, waiting until server is ready for connections.
forked process: 5928
child process started successfully, parent exiting
[root@server mongodb-3.4]# bin/mongo --port 27017
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.4.0
Server has startup warnings: 
2018-06-29T14:51:29.383+0800 I CONTROL  [main] 
2018-06-29T14:51:29.383+0800 I CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2018-06-29T14:51:29.383+0800 I CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2018-06-29T14:51:29.383+0800 I CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2018-06-29T14:51:29.384+0800 I CONTROL  [main] 
mongos> sh.addShard("shard1/127.0.0.1:27018,127.0.0.1:27019")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/127.0.0.1:27118,127.0.0.1:27119")
{ "shardAdded" : "shard2", "ok" : 1 }
  

数据库集群状态截图:


六、设置数据库启用分片,启用索引。


mongos> use admin
switched to db admin
mongos> sh.enableSharding("push")
{ "ok" : 1 }
mongos> sh.shardCollection("push.user",{name:1})
{ "collectionsharded" : "push.user", "ok" : 1 }  

七、写入数据测试。


mongos> for(var i=1;i<=2000000;i++){db.user.save({_id:i,name:"user-"+i,age:18})}
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2000000 })
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5b35d5a5d35df9180e59a17e")
}
  shards:
	{  "_id" : "shard1",  "host" : "shard1/127.0.0.1:27018,127.0.0.1:27019",  "state" : 1 }
	{  "_id" : "shard2",  "host" : "shard2/127.0.0.1:27118,127.0.0.1:27119",  "state" : 1 }
  active mongoses:
	"3.4.0" : 1
 autosplit:
	Currently enabled: yes
  balancer:
	Currently enabled:  yes
	Currently running:  no
		Balancer lock taken at Fri Jun 29 2018 14:46:03 GMT+0800 (CST) by ConfigServer:Balancer
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		5 : Success
  databases:
	{  "_id" : "push",  "primary" : "shard1",  "partitioned" : true }
		push.user
			shard key: { "name" : 1 }
			unique: false
			balancing: true
			chunks:
				shard1	6
				shard2	7
			{ "name" : { "$minKey" : 1 } } -->> { "name" : "user-10" } on : shard1 Timestamp(5, 0) 
			{ "name" : "user-10" } -->> { "name" : "user-1142445" } on : shard1 Timestamp(6, 0) 
			{ "name" : "user-1142445" } -->> { "name" : "user-1284894" } on : shard2 Timestamp(6, 1) 
			{ "name" : "user-1284894" } -->> { "name" : "user-1509894" } on : shard2 Timestamp(5, 2) 
			{ "name" : "user-1509894" } -->> { "name" : "user-1734895" } on : shard2 Timestamp(5, 3) 
			{ "name" : "user-1734895" } -->> { "name" : "user-191792" } on : shard2 Timestamp(5, 4) 
			{ "name" : "user-191792" } -->> { "name" : "user-217119" } on : shard2 Timestamp(4, 7) 
			{ "name" : "user-217119" } -->> { "name" : "user-245186" } on : shard2 Timestamp(4, 4) 
			{ "name" : "user-245186" } -->> { "name" : "user-390373" } on : shard1 Timestamp(4, 1) 
			{ "name" : "user-390373" } -->> { "name" : "user-5636" } on : shard1 Timestamp(3, 3) 
			{ "name" : "user-5636" } -->> { "name" : "user-81307" } on : shard1 Timestamp(3, 4) 
			{ "name" : "user-81307" } -->> { "name" : "user-9" } on : shard1 Timestamp(2, 4) 
			{ "name" : "user-9" } -->> { "name" : { "$maxKey" : 1 } } on : shard2 Timestamp(2, 0)   

这里在集群上插入200W条数据。然后查看数据分布情况,发现两个分片shard1,shard2上均有数据。

在shard1分片上验证数据分布。


shard1:PRIMARY> show dbs;
admin  0.000GB
local  0.046GB
push   0.032GB
shard1:PRIMARY> use push
switched to db push
shard1:PRIMARY> db.user.count()
885844  

在shard2分片上验证数据分布。


shard2:PRIMARY> show dbs;
admin  0.000GB
local  0.055GB
push   0.042GB
shard2:PRIMARY> use push
switched to db push
shard2:PRIMARY> db.user.count()
1114156  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mongodb3.4+sharding分片集群环境搭建 的相关文章

  • Git-TortoiseGit使用报错:cannot spawn xxx\bin\ssh.exe: No such file or directory fatal

    一般开发都是用eclipse自带的git插件提交代码 xff0c 今天试了一把TortoiseGit的提交 结果报错 查看TortoiseGit gt Settings xff1a 发现这里环境变量和设置的环境变量不一致 xff0c 猜测是
  • windows+spark本地运行环境搭建

    spark作为一个内存mapreduce框架 xff0c 速度是hadoop的10倍甚至100倍 windows下可以通过简单设置 xff0c 搭建本地运行环境 1 下载spark预编译版本 xff0c spark运行环境依赖jdk sca
  • git clone 报错:Peer reports incompatible or unsupported protocol version解决办法

    git通过git clone下载github上的资源到机器上 xff0c 结果出现如题所示的错误 root 64 server data git clone https github com pingcap tidb docker comp
  • explian使用介绍

    1 xff09 id列数字越大越先执行 xff0c 如果说数字一样大 xff0c 那么就从上往下依次执行 xff0c id列为null的就表是这是一个结果集 xff0c 不需要使用它来进行查询 2 xff09 select type列常见的
  • centos7安装rustup

    rust安装 xff0c 因为被墙的原因 xff0c 在国内几乎很难安装 xff0c 需要通过代理安装 但是rustup却很容易 xff0c 一般在linux下 xff0c 通过官方指定的下列语句 xff0c 基本可以安装rustup cu
  • TiDB在Centos7上通过源码编译安装

    这里难以编译安装的是tikv tidb的三大部分tidb pd tikv中tidb pd均是采用go语言编写 xff0c 安装go语言包即可编译 xff0c 唯独tikv是采用rust语言写的 xff0c 他的编译是最复杂的 而且编译环境非
  • Cloudera Manager 5.12.0图文详解安装过程

    这里介绍的是cdh5的离线安装方式 xff0c 需要的文件提前准备好 xff0c 安装过程会快一些 安装前提 xff1a 机器配置内存一定要高 xff0c 我这里安装的虚拟机均是redhat7 xff1a 内存分别是6G 4G 4G 准备的
  • Failed to get D-Bus connection: Operation not permitted

    docker容器centos7中 xff0c 通过systemctl start service出现下错误 Failed to get D Bus connection Operation not permitted docker中的容器启
  • C++中如何求数组长度

    C 43 43 中没有直接提供求数组长度的方法 xff0c 提供了sizeof begin end 等方法 xff0c 可以供求数组长度使用 可以通过两种方式来求数组长度 xff0c 这里使用模版类 一个是使用sizeof 做除法 xff0
  • IDEA+scala插件开发spark程序

    spark由scala语言编写 xff0c 开发spark程序 xff0c 自然也少不了scala环境 xff0c 这里介绍如何利用Intellij IDEA开发spark 1 环境准备 jdk scala idea这些对于本文来说都已经默
  • hadoop-3.0.1源码编译需要注意的事项

    这次尝试了一下源码编译最新的hadoop3 0 1 xff0c 发现了几个和原来不太一样的地方 记录下来 xff1a 1 需要的jdk不再是原来的1 7 xff0c 直接jdk1 8就编译通过了 xff1b 2 以前安装需要安装编译依赖cm
  • hadoop-3.0.1单节点安装部署

    hadoop 3 0 1编译上和原来有不同的地方 xff0c 部署时 xff0c 也有一些需要注意的地方 安装部署过程一笔带过 xff1a a 设置免秘钥登录 b 设置jdk环境变量 c 配置编译好的hadoop环境变量 xff0c HAD
  • Dll动态链接库创建与隐式链接方式使用

    C 43 43 动态链接库的背景 xff1a windows操作系统诞生以来 xff0c dll就作为win操作系统的基础 xff0c 通常情况下dll不能直接运行 xff0c 也无法接收消息 xff0c 只能供其他可执行程序或者dll来调
  • def模块定义方式创建动态链接库与动态加载使用dll

    前面介绍了通过宏定义 declspec dllexport 的方式创建动态链接库 xff0c 需要定义头文件和宏 xff0c 定义函数时需要使用宏定义 xff0c 这种方式过于繁琐 xff0c 这里介绍使用模块定义文件的方式创建动态链接库
  • Echarts-图表根据值的不同展示成不同的颜色

    series name 39 直接访问 39 type 39 bar 39 barWidth 39 60 39 data 10 52 200 334 390 330 220 itemStyle normal color function p
  • error: no default toolchain configured

    当我们在windows上通过msi方式安装过rust之后 xff0c 运行rustc或者cargo命令行命令时 xff0c 出现如题所示的错误 stackoverflow上有一个issue 说过这个问题 最后的解决办法是通过rustup命令
  • hive启动报错汇总以及解决办法

    1 Relative path in absolute URI system java io tmpdir 7D 7Bsystem user name 7D p span style font size 12px 需要将hive site
  • Hive使用JsonSerDe格式化json数据

    hive默认使用分隔符如空格 xff0c 分号 xff0c 34 34 xff0c 制表符 t来格式化数据记录 xff0c 对于复杂数据类型如json nginx日志等 xff0c 就没有办法拆分了 xff0c 这时候需要更加强大的SerD
  • ClassNotFoundException: org.apache.spark.sql.hive.thriftserver.SparkSQLCLIDriver解决办法

    我们通过源码编译的spark 2 3 0来启动spark sql进行sql操作 xff0c 结果出现如下错误 xff1a Spark assembly has been built with Hive including Datanucle
  • centos7源码编译安装lua:lua5.1升级lua5.3

    我们通过yum安装的lua默认是5 1版本 xff0c 如果不符合我们有的程序需要 xff0c 可以通过源码编译安装最新版lua 过程记录如下 下载 gt 解压 gt 编译三步曲 wget http www lua org ftp lua

随机推荐