Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to mongoDB from bash shell script

I am trying to connect to a remote MongoDB instance using a shell script, but I am not able to connect.

  #!/bin/sh

mongo --eval "db = connect('sm-repository2.db.qa.test.com:27017/testdb')"

mongo --eval "db.stats()"  # do a simple harmless command of some sort

RESULT=$?   # returns 0 if mongo eval succeeds

if [ $RESULT -ne 0 ]; then
    echo "mongodb not running"
    exit 1
else
    echo "mongodb running!"
fi

This tries to connect to my local mongo instance and gives me this error :

Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84`
like image 637
Pi Horse Avatar asked Mar 13 '14 01:03

Pi Horse


1 Answers

What you want is:

 mongo sm-repository2.db.qa.test.com:27017/testdb --eval "db.stats()"

Or for longer scripts:

 mongo sm-repository2.db.qa.test.com:27017/testdb script.js

See the full options in the documentation.

like image 61
Neil Lunn Avatar answered Sep 30 '22 09:09

Neil Lunn