如何将 MongoDB 副本集转换为独立服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16914281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a MongoDB replica set to a stand alone server
提问by Amol M Kulkarni
Consider, I have 4 replicate sets and the config is as follows:
考虑一下,我有 4 个复制集,配置如下:
{
"_id": "rs_0",
"version": 5,
"members" : [
{"_id": 1, "host": "127.0.0.1:27001"},
{"_id": 2, "host": "127.0.0.1:27002"},
{"_id": 3, "host": "127.0.0.1:27003"},
{"_id": 4, "host": "127.0.0.1:27004"}
]
}
I am able to connect to all sets using
mongo --port <port>
我能够使用
mongo --port <port>
There are documents for getting information on Convert a Standalone to a Replica Set, but can anyone tell me how to convert back to standalone from replica set?
有获取有关Convert a Standalone to a Replica Set信息的文档,但谁能告诉我如何从副本集转换回独立?
采纳答案by Sergio Tulentsev
Just remove a host from replica set (rs.remove('host:port')
), relaunch it without replSet
parameter and it's standalone again.
只需从副本集 ( rs.remove('host:port')
) 中删除一个主机,在没有replSet
参数的情况下重新启动它,它就会再次独立。
回答by amvalente
Remove all secondary hosts from replica set (rs.remove('host:port')), restart the mongo deamon without replSet parameter (editing /etc/mongo.conf) and the secondary hosts starts in standalone mode again.
从副本集中删除所有辅助主机 (rs.remove('host:port')),重新启动没有 replSet 参数的 mongo 守护进程(编辑 /etc/mongo.conf),辅助主机再次以独立模式启动。
The Primary host is tricky one, because you can't remove it from the replica set with rs.remove. Once you have only the primary node in the replica set, you should exit mongo shell and stop mongo. Then you edit the /etc/mongo.conf and remove the replSet parameter and start mongo again. Once you start mongo you are already in standalone mode, but the mongo shell will prompt a message like:
主要主机是棘手的,因为您无法使用 rs.remove 从副本集中删除它。一旦副本集中只有主节点,您应该退出 mongo shell 并停止 mongo。然后编辑 /etc/mongo.conf 并删除 replSet 参数并再次启动 mongo。启动 mongo 后,您已经处于独立模式,但是 mongo shell 会提示如下消息:
2015-07-31T12:02:51.112+0100 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
2015-07-31T12:02:51.112+0100 [initandlisten] ** 警告:mongod 启动时没有 --replSet 但 1 个文档存在于 local.system.replset 中
to remove the warning you can do 2 procedures: 1) Droping the local db and restarting mongo:
要删除警告,您可以执行 2 个程序:1) 删除本地数据库并重新启动 mongo:
use local
db.dropDatabase();
/etc/init.d/mongod restart
2)Or if you don't want to be so radical, you can do:
2)或者如果你不想那么激进,你可以这样做:
use local
db.system.replset.find()
and it will prompt a message like:
它会提示如下消息:
{ "_id" : "replicaSetName", "version" : 1, "members" : [ { "_id" : 0, "host" : "hostprimary:mongoport" } ] }
then you will erase it using:
然后您将使用以下方法擦除它:
db.system.replset.remove({ "_id" : "replicaSetName", "version" : 1, "members" : [ { "_id" : 0, "host" : "hostprimary:mongoport" } ] })
and it will probably prompt:
它可能会提示:
WriteResult({ "nRemoved" : 1 })
Now, you can restart the mongo and the warning should be gone, and you will have your mongo in standalone mode without warnings
现在,您可以重新启动 mongo 并且警告应该消失了,并且您的 mongo 将处于独立模式而不会发出警告
回答by Raj Sharma
On an Ubuntu Machine
在 Ubuntu 机器上
- Stop your mongo server
- open /etc/mongod.conf
- Comment the replication and replSetName line
- 停止你的 mongo 服务器
- 打开/etc/mongod.conf
- 注释复制和 replSetName 行
#replication: #replSetName: rs0
#replication: #replSetName: rs0
- Start your mongo server and go to mongo shell
- drop local database
- 启动您的 mongo 服务器并转到 mongo shell
- 删除本地数据库
use local
db.dropDatabase()
- Restart mongo
- 重启mongo
回答by Charles Lin
The MongoDB Documentationsuggests the following to perform maintenance on a replica set member, which brings the the replica set member into standalone mode for further operations. With little modification it can be made standalone:
的MongoDB的文档建议将一个副本集构件,其使副本集构件成独立模式为进一步的操作进行维护以下。只需稍加修改,它就可以独立运行:
- If node in concern is the only node in a shard, drain the chunks to other shards as per MongoDB documentation here, or else the sharded database will break, i.e.
- Make sure balancer is enabled by connecting to mongos and run
sh.startBalancer(timeout, interval)
- For the shard in concern, go to admin database and
db.adminCommand( { removeShard: "mongodb0" } )
- Check draining status by repeating above removeShard command, wait for draining to complete
- Make sure balancer is enabled by connecting to mongos and run
- If node in concern is primary, do
rs.stepDown(300)
- Stop the node by running
db.shutdownServer()
- Change the yaml config by:
- commenting out replication.replSetName(--replSetName in command line)
- commenting out sharding.clusterRolefor shard or config server (--shardsvc and --configsvr in command line)
- commenting out net.port, then change it to a different port (--port in command line)
- Start the mongod instance
- If change is permanent, go to other mongod instance and run
rs.remove("host:port")
- 如果所关注的节点是分片中的唯一节点,请按照此处的 MongoDB 文档将块排空到其他分片,否则分片数据库将中断,即
- 通过连接到 mongos 并运行来确保启用了平衡器
sh.startBalancer(timeout, interval)
- 对于相关分片,请转到管理数据库并
db.adminCommand( { removeShard: "mongodb0" } )
- 重复上面的removeShard命令检查排空状态,等待排空完成
- 通过连接到 mongos 并运行来确保启用了平衡器
- 如果关注的节点是主要节点,请执行
rs.stepDown(300)
- 通过运行停止节点
db.shutdownServer()
- 通过以下方式更改 yaml 配置:
- 注释掉replication.replSetName(命令行中的 --replSetName)
- 注释掉分片或配置服务器的sharding.clusterRole(命令行中的 --shardsvc 和 --configsvr)
- 注释掉net.port,然后将其更改为不同的端口(命令行中的 --port )
- 启动 mongod 实例
- 如果更改是永久性的,请转到其他 mongod 实例并运行
rs.remove("host:port")
After this, the node in concern should be up and running in standalone mode.
在此之后,相关节点应该以独立模式启动并运行。
回答by Shrinivas Kalangutkar
1) Go to mongo shell on Secondary servers
1) 转到辅助服务器上的 mongo shell
2) Stop the secondary servers by using below command:
2)使用以下命令停止辅助服务器:
use admin
db.shutdownServer()
After executing below command. Your primary will automatically become stand alone server.
执行以下命令后。您的主要服务器将自动成为独立服务器。
For more information, please check the below link: http://www.tutespace.com/2016/03/stopping-and-starting-mongodb.html
更多信息,请查看以下链接:http: //www.tutespace.com/2016/03/stopping-and-starting-mongodb.html