如何配置MongoDB分片群集
程序员文章站
2022-05-01 16:53:55
配置MongoDB分片群集实验环境:分片服务器1(一主两从)Kgcs1 27017、27018、27019 配置文件(mongod-2.conf)分片服务器2(一主两从)Kgcs2 27020、27021、27022 配置文件(mongod2-5.conf)配置服务器(一主两从)config server 37017、37018、37019 配置文件(mongod6-8.conf)路由服务器:Mongos :47017 配置文件(mongods.conf)一、配置分片服务...
配置MongoDB分片群集
实验环境:
分片服务器1(一主两从)
Kgcs1 27017、27018、27019 配置文件(mongod-2.conf)
分片服务器2(一主两从)
Kgcs2 27020、27021、27022 配置文件(mongod2-5.conf)
配置服务器(一主两从)
config server 37017、37018、37019 配置文件(mongod6-8.conf)
路由服务器:
Mongos :47017 配置文件(mongods.conf)
一、配置分片服务器(注意配置前要把所有实例中的数据清空:)在配置文件中添加一下内容:每个复制集的每一个节点都需要配置:
1)[root@kgc6 ~]# vim /etc/mongod.conf
sharding:
clusterRole: shardsvr
replication:
oplogSizeMB: 2048
replSetName: kgcrs1(这里是复制集的名称和上面规划的要一样)
2)登录MongoDB数据库添加复制集并初始化:
[root@kgc6 bin]# mongo
> config={_id:"kgcrs1",members:[{_id:0,host:"127.0.0.1:27017"},{_id:1,host:"127.0.0.1:27018"},{_id:2,host:"127.0.0.1:27019"}]}
> rs.initiate(config)
{ "ok" : 1 }
[root@kgc6 bin]# mongo --port 27020
> config={_id:"kgcrs2",members:[{_id:0,host:"127.0.0.1:27020"},{_id:1,host:"127.0.0.1:27021"},{_id:2,host:"127.0.0.1:27022"}]}
> rs.initiate(config)
{ "ok" : 1 }
可以使用rs.status()命令查看复制集
二、配置配置服务器:(注意三台配置服务器都需要添加一下内容:)
[root@kgc6 ~]# vim /etc/mongod6.conf
harding:
clusterRole: configsvr
#replication:
replication:
oplogSizeMB: 2048
replSetName: configReplSet (配置服务器复制集名称)
3)登录到37017 MongoDB数据库添加复制集并初始化:
[root@kgc6 bin]# mongo --port 37017
> config={_id:"configReplSet",members:[{_id:0,host:"127.0.0.1:37017"},{_id:1,host:"127.0.0.1:37018"},{_id:2,host:"127.0.0.1:37019"}]}
> rs.initiate(config)
{ "ok" : 1 }
三、配置路由服务器:
1):在路由服务器的配置文件中加上如下选项:
[root@kgc6 ~]# vim /etc/mongod9.conf
sharding:
configDB: configReplSet/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019
2)启动路由服务器mongos:
[root@kgc6 bin]# mongos -f /etc/mongods.conf
[root@kgc6 bin]# whatis mongos
mongos (1) - MongoDB Sharded Cluster Query Router
[root@kgc6 bin]# whatis mongo
mongo (1) - MongoDB Shell
[root@kgc6 bin]# whatis mongod
mongod (1) - MongoDB Server
3)登录到路由服务器 47017 MongoDB数据库为分片群集添加节点:
[root@kgc6 bin]# mongo --port 47017
mongos> use admin
switched to db admin
mongos> mongos> db.runCommand({addshard:"kgcrs1/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019",name:"shard1"})
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({addshard:"kgcrs2/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022",name:"shard2"})
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "shard2",
"host" : "kgcrs2/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022",
"state" : 1
},
{
"_id" : "shard1",
"host" : "kgcrs1/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019",
"state" : 1
}
],
"ok" : 1
}
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f06ee39003f00de0f0f9440")
}
shards:
{ "_id" : "shard1", "host" : "kgcrs1/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019", "state" : 1 }
{ "_id" : "shard2", "host" : "kgcrs2/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022", "state" : 1 }
active mongoses:
"3.4.24" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
4)激活分片功能:
mongos> db
admin
mongos> db.runCommand({enablesharding:"test"}) 激活test库的分片功能
{ "ok" : 1 }
5)指定分片键对集合分片:
第一步:创建索引 :优化查询的重要手段:
mongos> use test
switched to db test
mongos> db
test
mongos> db.stu.ensureIndex({id:1})
{
"raw" : {
"kgcrs1/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019" : {
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1594291026, 2),
"t" : NumberLong(2)
},
"electionId" : ObjectId("7fffffff0000000000000002")
}
}
},
"ok" : 1
}
第二步:开启分片:
mongos> use admin
switched to db admin
mongos> db.runCommand({shardcollection:"test.stu",key:{id:1}})
{ "collectionsharded" : "test.stu", "ok" : 1 }
mongos> use config //设置块的大小
switched to db config
mongos> db.settings.save( { _id:"chunksize", value:"8" } )为8M
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
四、集合分片验证:
第一步:产生大量数据
mongos> use test;
switched to db test
mongos> for(var i=1;i<=100000;i++){db.stu.insert({'id':i,'name':'Alice'});}
//添加10万条数据
本文地址:https://blog.csdn.net/weixin_45947267/article/details/107314680
上一篇: 浏览器与Tomcat交互
下一篇: Spring事务管理(详解+实例)