mongodb shard cluster

本文介绍了一个MongoDB分片集群的部署过程,包括配置文件设置、域名配置、Config Server及Shard副本集初始化、Mongos配置及关联Shard等关键步骤。

mongodb shard cluster

环境准备

域名配置

[root@node2 mongodb]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.234 node2

192.168.3.234 cfg1
192.168.3.235 cfg2 
192.168.3.236 cfg3 

192.168.3.234 shd1 
192.168.3.235 shd2 
192.168.3.236 shd3 

配置文件目录


cfgdir/
|-- cfg
|   `-- cfg.cnf
|-- mgs
|   `-- mgs.cnf
`-- shd
    `-- shd.cnf

数据文件目录

datadir/
|-- cfg
`-- shd 

日志文件目录

logdir
|-- cfg
|   `-- cfg.log
`-- shd
    `-- shd.log

config server部署

配置文件

port=27017

dbpath=/usr/local/mongodb/datadir/cfg/

logpath=/usr/local/mongodb/logdir/cfg/cfg.log

fork=true

logappend=true

bind_ip=localhost,cfg1

replSet=repcfg

directoryperdb=true

configsvr=1               


config server副本集初始化

rs.initiate(
  {
    _id: "repcfg",
    configsvr: true,
    members: [
      { _id : 0, host : "cfg1:27017" },
      { _id : 1, host : "cfg2:27017" },
      { _id : 2, host : "cfg3:27017" }
    ]
  }
)

shard集群

shard 配置文件

port=27018

dbpath=/usr/local/mongodb/datadir/shd/

logpath=/usr/local/mongodb/logdir/shd/shd.log

fork=true

logappend=true

bind_ip=localhost,shd1

replSet=repshd

directoryperdb=true

shardsvr=1

shard副本集初始化

rs.initiate(
  {
    _id : "repshd",
    members: [
      { _id : 0, host : "shd1:27018" },
      { _id : 1, host : "shd2:27018" },
      { _id : 2, host : "shd3:27018" }
    ]
  }
)

mongos

mongos配置文件

port=27019
configdb=repcfg/cfg1:27017,cfg2:27017,cfg3:27017	

连接mongos

[root@node2 ~]# mongo --port 27019
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27019/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3a0e3c69-e62e-480e-83ae-ec66fc2a9594") }
MongoDB server version: 4.2.7
Server has startup warnings: 
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] 
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] 
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] ** WARNING: This server is bound to localhost.
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          Remote systems will be unable to connect to this server. 
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          Start the server with --bind_ip <address> to specify which IP 
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          addresses it should serve responses from, or with --bind_ip_all to
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          bind to all interfaces. If this behavior is desired, start the
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-11-05T15:06:07.056+0800 I  CONTROL  [main] 
mongos> 
mongos> 

关联shard副本集

sh.addShard( "repshd/shd1:27018,shd2:27018,shd3:27018")

启用shard数据库

sh.enableSharding("inventory")

启用shard collection

sh.shardCollection("inventory.products", { prod_id : "hashed" } )

验证mogos路由

mongos> db.products.find({prod_id:1}).explain()
{
	"queryPlanner" : {
		"mongosPlannerVersion" : 1,
		"winningPlan" : {
			"stage" : "SINGLE_SHARD",
			"shards" : [
				{
					"shardName" : "repshd",
					"connectionString" : "repshd/shd1:27018,shd2:27018,shd3:27018",
					"serverInfo" : {
						"host" : "node2",
						"port" : 27018,
						"version" : "4.2.7",
						"gitVersion" : "51d9fe12b5d19720e72dcd7db0f2f17dd9a19212"
					},
					"plannerVersion" : 1,
					"namespace" : "inventory.products",
					"indexFilterSet" : false,
					"parsedQuery" : {
						"prod_id" : {
							"$eq" : 1
						}
					},
					"queryHash" : "10A0442B",
					"planCacheKey" : "C0C6317B",
					"winningPlan" : {
						"stage" : "SHARDING_FILTER",
						"inputStage" : {
							"stage" : "FETCH",
							"filter" : {
								"prod_id" : {
									"$eq" : 1
								}
							},
							"inputStage" : {
								"stage" : "IXSCAN",
								"keyPattern" : {
									"prod_id" : "hashed"
								},
								"indexName" : "prod_id_hashed",
								"isMultiKey" : false,
								"isUnique" : false,
								"isSparse" : false,
								"isPartial" : false,
								"indexVersion" : 2,
								"direction" : "forward",
								"indexBounds" : {
									"prod_id" : [
										"[5902408780260971510, 5902408780260971510]"
									]
								}
							}
						}
					},
					"rejectedPlans" : [ ]
				}
			]
		}
	},
	"serverInfo" : {
		"host" : "node2",
		"port" : 27019,
		"version" : "4.2.7",
		"gitVersion" : "51d9fe12b5d19720e72dcd7db0f2f17dd9a19212"
	},
	"ok" : 1,
	"operationTime" : Timestamp(1604563026, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1604563033, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
### 部署MongoDB分片集群于单机上的指南 #### 准备工作 为了在一个机器上设置分片集群,需要配置多个进程来模拟不同的服务器角色。这包括mongod实例作为配置服务器、shard节点以及mongos路由服务。 #### 启动配置服务器 创建用于存储配置数据的小型副本集。即使是在单一主机环境中也推荐这样做以保持架构的一致性和未来扩展的可能性[^1]: ```bash mkdir -p /data/configdb mongod --configsvr --replSet configRepl --port 27019 --bind_ip localhost --dbpath /data/configdb & ``` #### 初始化配置服务器复制集 连接至刚刚启动的服务并初始化复制集: ```javascript rs.initiate() ``` #### 设置Shards 对于每一个计划中的碎片,在本地端口运行新的`mongod`实例,并指定合适的数据目录路径。这里展示两个例子: ```bash # Shard 1 mkdir -p /data/db/shard1 mongod --dbpath /data/db/shard1 --replSet rs1 --fork --logpath /var/log/mongodb-shard1.log & # Shard 2 mkdir -p /data/db/shard2 mongod --port 27021 --dbpath /data/db/shard2 --replSet rs2 --fork --logpath /var/log/mongodb-shard2.log & ``` 接着通过shell客户端登录任一上述新建立的shard节点完成其内部复制集初始化过程: ```javascript // 进入 shard1 的命令行界面 mongo --port 27020 > rs.initiate() // 对于第二个分片重复相同的操作... ``` #### 启动Mongos查询路由器 最后一步是开启至少一个mongos实例,该组件负责协调跨不同片段之间的读写请求流量分配: ```bash mongos --configdb configRepl/localhost:27019 --port 27018 --fork --logpath /var/log/mongodb-mongos.log & ``` 此时可以通过访问此端口来进行后续管理和维护活动;比如添加已有的shard成员到当前分布式的命名空间里去。 #### 添加Shards到Cluster 利用MongoDB Shell连接到任意可用的mongos实例执行如下指令向群集中加入先前定义好的各个部分: ```javascript use admin sh.addShard("rs1/localhost:27020") sh.addShard("rs2/localhost:27021") ``` 以上步骤完成后即成功构建了一个简易版但功能完整的分布式数据库系统模型。值得注意的是实际生产环境下还需要考虑更多因素如网络隔离、资源限制等措施确保稳定可靠运作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值