如何在不关闭数据库的情况下正确关闭Mongo(在副本集中)?
我的服务器设置是这样的:
2 x Servers 。 mongoDB 在两台服务器之间都有副本集。每个都是一个节点。
然后我将我的 Node.js 服务器连接到 MongoDB。
发生的事情是……当我杀死辅助服务器时。 (关闭服务器)。主要的 MongoDB 仍在运行,但 Node.js 服务器当时与 MongoDB 存在连接问题。即使我重新添加了服务器,它也不起作用。我使用 mongoose 和 connect-mongo 。
那么,发生了什么?如何正确关闭Mongo节点?
My server set up is like that:
2 x Servers . The mongoDB has replica set among both servers. Each is one node.
and then I have my node.js server connect to the MongoDB.
What happen was.. when I kill the secondary server. (shutting down the server). The MongoDB at primary still up but the Node.js Server had connection issue with MongoDB then. Even I added the server back, it didn't work. I use mongoose and connect-mongo .
So, what happened? how to shut down Mongo node properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有一个包含 2 个节点的副本集,当一个节点出现故障时,另一个节点会将自身降级为辅助节点。如果您没有使用
slaveOk
true 进行连接,那么您将无法读取(并且在任何一种情况下您都将无法写入)。这是 MongoDB 实施的一项安全措施,要求副本集的大多数(即一半加一)能够互相看到,以确保可以安全地选出主节点。如果看不到多数,少数的节点就无法知道“另一半”是否选举了主节点。同时拥有两个初选将非常糟糕 (TM),因为这可能会导致更新冲突。
在您只想运行两个节点的情况下,您还可以运行仲裁器来打破平局,以防一个节点出现故障或对副本集不可见。仲裁器是一个普通的 mongod 进程,但不存储任何数据——本质上它只参与选举,否则闲置。在具有 2 个“正常”节点和 1 个仲裁器的副本集中,两个数据保存节点中的任何一个都可以在不失去多数节点的情况下关闭。
有关更多信息,请参阅有关副本集的 MongoDB 文档和有关副本集的 MongoDB 文档。 mongodb.org/display/DOCS/Adding+an+Arbiter">artibers 文档。
If you have a replica set with 2 nodes, when one node goes down the other will demote itself to secondary. If you aren't connecting with
slaveOk
true, then you won't be able to read (and in either case you won't be able to write).This is a safety measure imposed by MongoDB, which requires that a majority (meaning half plus one) of a replica set be able to see one another in order to ensure that a primary can be safely elected. If a majority cannot be seen, the nodes in the minority cannot know whether the "other half" have elected a primary. Having two primaries at the same time would be Very Bad (TM), as that could lead to conflicting updates.
In situations where you only want to run two nodes, you can also run an arbiter to break ties in the case that one node goes down or becomes otherwise invisible to the replica set. An arbiter is a normal
mongod
process, but does not store any data -- essentially it only participates in elections, and is idle otherwise. In a replica set with 2 "normal" nodes and one arbiter, either one of the two data-holding nodes can go down without losing a majority.For more, see the MongoDB documentation on replica sets and the documentation on artibers.
如果在您删除辅助节点后,您的主节点仍然是主节点,则这是节点的驱动程序问题。无论如何,你总是应该有一个具有偶数个副本节点的仲裁器,“为什么”在 mongodb 的文档中有详细记录。
如果这是一个node.js问题,您使用的是哪个版本的node-mongodb-native?两个月前我遇到了一些不同的复制集问题,但最新版本已修复。驱动程序的最后一个副本集问题已于 9 月 9 日关闭,您应该尝试使用最后一个标记版本 (V0.9.6.18 当我写这篇文章时)
If your primary is still primary after you take down the secondary, it's a node's driver issue. Anyway you always should have an arbiter with an even number of replica nodes, the "why" is well documented on mongodb's doc.
In case this is a node.js issue, wich version of node-mongodb-native are you using ? I had some different replicaset issues 2 month ago but there have ben fixed with the latest versions. The last replicaset issue of the driver has ben closed the 9th Sept, you shoud giv it a try with the last tagged version (V0.9.6.18 as i'm writing this)