mongodb 如何从 bash 执行 mongo 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31028235/
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 execute mongo commands from bash?
提问by MoienGK
I am trying to run this command from bash script:
我正在尝试从 bash 脚本运行此命令:
mongo 192.168.10.20:27000 --eval "use admin && db.shutdownServer() && quit()"
but i get this error :
但我收到此错误:
[rs.initiate() && use admin && db.shutdownServer() && quit()] doesn't exist
how can i do it without using a js file?
我怎么能不使用js文件呢?
回答by Stennie
There are differences between interactive & scripted mongo
shell sessions. In particular, commands like use admin
are not valid JavaScript and will only work in an interactive shell session.
交互式和脚本化mongo
shell 会话之间存在差异。特别是,像这样use admin
的命令不是有效的 JavaScript,只能在交互式 shell 会话中工作。
The working equivalent of your shutdown command line would be:
关闭命令行的工作等效项是:
mongo 192.168.10.20:27000/admin --eval "db.shutdownServer()"
You can include the database to use in the connection string, and there is no need to quit from a scripted mongo
shell session.
您可以在连接字符串中包含要使用的数据库,并且无需从脚本化的mongo
shell 会话中退出。
If you do need to change databases from a scripted session, there is a db.getSiblingDB()
JavaScript function. An alternative way to write the shutdown command above would be:
如果您确实需要从脚本会话中更改数据库,则可以使用db.getSiblingDB()
JavaScript 函数。编写上述关闭命令的另一种方法是:
mongo 192.168.10.20:27000 --eval "db=db.getSiblingDB('admin');db.shutdownServer()"
回答by Ugur
You can use heredoc syntax.
您可以使用 heredoc 语法。
#! /bin/sh
mongo <<EOF
use admin
db.shutdownServer()
quit()
exit
Turns out heredoc syntax throws a warning when EOF is missing at the end for bash script. This is the bash script version.
原来,当 bash 脚本的末尾缺少 EOF 时,heredoc 语法会引发警告。这是 bash 脚本版本。
#! /bin/bash
mongo <<EOF
use admin
db.shutdownServer()
quit()
EOF
Here is the output, I guess this is what you expected.
这是输出,我想这就是您所期望的。
MongoDB shell version: 2.4.14
connecting to: test
switched to db admin
Wed Jun 24 17:07:23.808 DBClientCursor::init call() failed
server should be down...
Wed Jun 24 17:07:23.810 trying reconnect to 127.0.0.1:27017
Wed Jun 24 17:07:23.810 reconnect 127.0.0.1:27017 ok
Wed Jun 24 17:07:23.812 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Jun 24 17:07:23.812 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
Wed Jun 24 17:07:23.812 DBClientCursor::init call() failed
回答by Hamed
From the mongo docs:
从mongo 文档:
--eval option
Use the --evaloption to mongo to pass the shell a JavaScript fragment, as in the following:
mongo test --eval "printjson(db.getCollectionNames())"
--eval 选项
使用mongo的--eval选项向 shell 传递 JavaScript 片段,如下所示:
mongo test --eval "printjson(db.getCollectionNames())"
You can also put your JS Fragments into a .js
file then do:
您还可以将您的 JS 片段放入一个.js
文件中,然后执行以下操作:
mongo < myScript.js
You may also find more useful stuff in this SO question
您还可以在这个SO 问题中找到更多有用的东西