MongoDB报"sharding already enabled for collection xxx"的错误

本文介绍了解决在MongoDB中尝试为已启用分片的集合再次启用分片时出现的错误。通过删除config数据库中与该集合相关的记录来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中执行

user_cli.admin.command({'shardcollection':'testdb.table2','key':{'id':1}})

开启testdb.table2集合分片功能:

报:OperationFailure: sharding already enabled for collection testdb.table2 的错误。

这时切换到config库执行db.chunks.find(),发现testdb.table2的记录已经存在。

mongos> use config

mongos> db.chunks.find()

{"_id":"testdb.table-id_MinKey","ns":"testdb.table","min":{"id":{"$minKey": 1}},"max":{"id":{"$maxKey": 1}},"shard":"autotestshd5ox8","lastmod": Timestamp(1,0),"lastmodEpoch": ObjectId("5b7c22cab2f293f9ed236190")}{"_id":"testdb.table2-id_MinKey","lastmod": Timestamp(2,0),"lastmodEpoch": ObjectId("5b7c2475b2f293f9ed23630a"),"ns":"testdb.table2","min":{"id":{"$minKey": 1}},"max":{"id": 1},"shard":"autotestshdcnas"}{"_id":"testdb.table2-id_1","lastmod": Timestamp(2,1),"lastmodEpoch": ObjectId("5b7c2475b2f293f9ed23630a"),"ns":"testdb.table2","min":{"id": 1},"max":{"id": 21},"shard":"autotestshd5ox8"}

解决:在config库里面删除这个testdb.table2相关的信息相关的信息。

mongos>use config

mongos>db.collections.remove( { _id: "testdb.table2" } )

mongos>db.chunks.remove( { ns: "testdb.table2" } )

mongos>db.locks.remove( { _id: "testdb.table2" } )

mongos>use admin

mongos>db.adminCommand("flushRouterConfig)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值