通过shell脚本执行mongo命令

时间:2015-08-09 13:50:40

标签: mongodb shell ubuntu

我正在尝试连接到我的mongo数据库并在其上执行一些命令,例如:

#!/bin/bash

mongo myMongoDbip:27017/admin -u username -p pwd

mongo --shell --eval  "use neura_staging"

我可以连接到数据库,但我的所有其他命令都不起作用,例如使用特定的数据库。

你能详细说明一些关于它的信息吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

你应该有这样的东西

#!/bin/bash

USER=username
PW=secret
AUTHDB=admin
LOGINDATA="-u $USER -p $PW --authenticationDatabase $AUTHDB"
HOST=hostname.example.com

mongo $HOST:27017 $LOGINDATA --shell --eval ’use neura_staging’

您对mongo的第二次调用未按预期工作的原因是此调用会打开 new (并且因为您未提供任何未经身份验证的登录数据)连接。