實驗目標:
同一個服務器搭建mongodb分片群集
OS:Win10
Mongodb:4.2
新手實驗記錄,如不正確,請批准指正。
第一步:創建分片集群的實例
cd
C:\Program Files\MongoDB\Server\4.2\bin
分片1:
mongod --shardsvr --replSet shard1 --dbpath "D:\MongoDBSharedCluster\shared1Node1\data" --port 4006 --logpath "D:\MongoDBSharedCluster\shared1Node1\log\shared1Node1.log"
mongod --shardsvr --replSet shard1 --dbpath "D:\MongoDBSharedCluster\shared1Node2\data" --port 4007 --logpath "D:\MongoDBSharedCluster\shared1Node2\log\shared1Node2.log"
分片2:
mongod --shardsvr --replSet shard2 --dbpath "D:\MongoDBSharedCluster\shared2Node1" --port 4008 --logpath "D:\MongoDBSharedCluster\shared2Node1\log\shared2Node1.log"
mongod --shardsvr --replSet shard2 --dbpath "D:\MongoDBSharedCluster\shared2Node2" --port 4009 --logpath "D:\MongoDBSharedCluster\shared2Node2\log\shared2Node2.log"
【分片1和分片2共計4個cmd窗口】
進入分片1中任何一個節點初始化複製集
mongo 127.0.0.1:4006
mongo 127.0.0.1:4006
config1={ _id:”shard1”, members:[
{_id:0,host:”localhost:4006”,priority:1},
{_id:1,host:”localhost:4007”,priority:2}
]}
rs.initiate(config1)
進入分片2中任何一個節點初始化複製集
mongo 127.0.0.1:4009
config2={ _id:”shard2”, members:[
{_id:0,host:”localhost:4008”,priority:2},
{_id:1,host:”localhost:4009”,priority:1}
]}
rs.initiate(config2)
第二步:配置配置服務器(共計三個實例,共三個cmd窗口)
C:\Program Files\MongoDB\Server\4.2\bin
mongod --configsvr --replSet confset --dbpath "D:\MongoDBSharedCluster\config1\data" --port 4002 --logpath "D:\MongoDBSharedCluster\config1\log\config1.log"
mongod --configsvr --replSet confset --dbpath "D:\MongoDBSharedCluster\config2\data" --port 4003 --logpath "D:\MongoDBSharedCluster\config2\log\config2.log"
mongod --configsvr --replSet confset --dbpath "D:\MongoDBSharedCluster\config3\data" --port 4004 --logpath "D:\MongoDBSharedCluster\config3\log\config3.log"
進入任何一個配置服務器的節點初始化配置服務器的群集
mongo 127.0.0.1:4002
rs.initiate(
{
_id: "confset",
configsvr: true,
members: [
{ _id : 0, host : "127.0.0.1:4002" },
{ _id : 1, host : "127.0.0.1:4003" },
{ _id : 2, host : "127.0.0.1:4004" }
]
}
)
第三步:配置路由服務器
C:\Program Files\MongoDB\Server\4.2\bin
Mongos --configdb confset/127.0.0.1:4002,127.0.0.1:4003,127.0.0.1:4004 --logpath "D:\MongoDBSharedCluster\mongos\log\mongos.log" --port 4000
mongo 127.0.0.1:4000
添加分片的信息
sh.addShard("shard1/localhost:4006,localhost:4007");
sh.addShard("shard2/localhost:4008,localhost:4009");
查看分片集
db.getSiblingDB("config").shards.find();