针对 MongoDB 中的副本集运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12820027/
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
Run script against replica set in MongoDB
提问by Jan Zyka
I have replica set of 3 nodes and I want to run a cleanup script against it every end of day. What I would do if there was only single node would be a simple bash script:
我有 3 个节点的副本集,我想每天结束时针对它运行一个清理脚本。如果只有一个节点,我会做的是一个简单的 bash 脚本:
~/mongo/bin/mongo host:port cleanupScript.js
But since I want to run the same script against replica-set I can't use this approach. I would need to somehow find which node is primary and run the script against that node.
但是因为我想针对副本集运行相同的脚本,所以我不能使用这种方法。我需要以某种方式找到哪个节点是主节点并针对该节点运行脚本。
So the question: Is there a way how to run the script against whole replica set and let the mongoprocess pick the primary node and execute on it?
所以问题是:有没有办法如何针对整个副本集运行脚本并让mongo进程选择主节点并在其上执行?
Thanks!
谢谢!
回答by Asya Kamsky
The mongo shell can connect directly to a replica set - this works with 2.4 (current), 2.2 (previous) and 2.0 (the version before that).
mongo shell 可以直接连接到副本集 - 这适用于 2.4(当前)、2.2(以前的)和 2.0(之前的版本)。
Assuming you have a replica set called myrsand your hosts are host1:27017and host2:27017, use the following syntax:
假设您有一个副本集被调用myrs并且您的主机是host1:27017and host2:27017,请使用以下语法:
mongo --host myrs/host1:27017,host2:27017
mongo --host myrs/host1:27017,host2:27017
The shell will figure out the rest, including connecting to the primary and if the primary steps down or goes away it will reconnect to the new primary once it's elected.
The shell will figure out the rest, including connecting to the primary and if the primary steps down or goes away it will reconnect to the new primary once it's elected.
回答by Abhay PS
I normally set priority for a node in replicaset. This gives me freedom to chose which node should get read and write load.
我通常为副本集中的节点设置优先级。这让我可以自由选择哪个节点应该获得读写负载。
In your case, I think, if you set priority for the nodes then you can always run your script against the highest priority node as that node will be primary almost all the time.
在您的情况下,我认为,如果您为节点设置优先级,那么您始终可以针对最高优先级节点运行脚本,因为该节点几乎一直是主要节点。
Setting priority is quite simple and straight forward. You can check this link http://docs.mongodb.org/manual/tutorial/force-member-to-be-primary/
设置优先级非常简单直接。您可以查看此链接http://docs.mongodb.org/manual/tutorial/force-member-to-be-primary/
I hope this will solve your problem.
我希望这能解决你的问题。
OKAY.... Probably this is what you need..
OKAY.... 可能这就是你需要的...
#!/bin/bash
PRIMARY=`~/mongo/bin/mongo localhost:27017 --eval "printjson(rs.isMaster())" | grep "primary" | cut -d"\"" -f4`
echo "$PRIMARY"
~/mongo/bin/mongo "$PRIMARY" cleanupScript.js
Run this on any node and it will give you the "server:port" of the primary. Give full path to the mongo executable.. just to avoid errors.
在任何节点上运行它,它将为您提供主节点的“服务器:端口”。提供 mongo 可执行文件的完整路径。只是为了避免错误。
PS: the whole command is in the backticks. Somehow those are not visible so thought of telling you.
PS:整个命令都在反引号中。不知何故,那些是不可见的,所以想告诉你。
回答by William Z
Unfortunately, the 'mongo' shell does not support making connections to a replica set: only to individual nodes. You have two choices if you want to do this:
不幸的是,'mongo' shell 不支持连接到副本集:仅连接到单个节点。如果您想这样做,您有两个选择:
- Use a different language, which supports making a connection to a replica set. (PHP, Python, Perl, Java and Ruby all support this.)
- Have a driver script that runs an 'rs.status()' command, parses the output of that command, and determines the current primary. If it's not the node you've connected to, then re-connect to the correct primary node
- 使用不同的语言,它支持连接到副本集。(PHP、Python、Perl、Java 和 Ruby 都支持这一点。)
- 有一个驱动程序脚本,它运行一个“rs.status()”命令,解析该命令的输出,并确定当前的主。如果它不是您连接的节点,则重新连接到正确的主节点
I wish I had a better answer for you.
我希望我有一个更好的答案给你。

