116

I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI:

mongodb://username:password@somewhere.mongolayer.com:10011/my_database

I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program in order to connect to this remote database.

However, I can't find the right command to get me the full shell access I would like. Using instructions found here http://docs.mongodb.org/manual/reference/program/mongo/ (search for "remote") I am able to get what looks like a connection, but without giving my username or password I am not fully connected. Running db.auth(username, password) returns 1 (as opposed to "auth fails" when I provide incorrect username and password), but I continue to get an "unauthorized" error message when issuing the show dbs command.

tadasajon
  • 14,276
  • 29
  • 92
  • 144

3 Answers3

192

You are probably connecting fine but don't have sufficient privileges to run show dbs.

You don't need to run the db.auth if you pass the auth in the command line:

mongo somewhere.mongolayer.com:10011/my_database -u username -p password

Once you connect are you able to see collections?

> show collections

If so all is well and you just don't have admin privileges to the database and can't run the show dbs

Ben
  • 9,725
  • 6
  • 23
  • 28
  • I am able to connect using the technique you describe. However, I can't run any commands, such as "show collections" or "show users". I keen getting a "not authorized for query on my_db.system.namespaces" error. – tadasajon Nov 10 '14 at 18:06
  • 1
    Ok, so heroku randomly named my mongo database a different name from the one I've been using in dev. I think that was basically my issue. – tadasajon Nov 10 '14 at 18:27
  • 1
    But if i have ?authSource=admin in the end. It doesn't work. --authenticationDatabase doesn't help. – Roman Kazanovskyi Apr 14 '16 at 08:28
  • 27017 is the default port though. just in case the example port doesn't work. https://docs.mongodb.com/manual/reference/default-mongodb-port/ – change Aug 28 '17 at 16:17
68

With Mongo 3.2 and higher just use your connection string as is:

mongo mongodb://username:password@somewhere.mongolayer.com:10011/my_database
Amio.io
  • 20,677
  • 15
  • 82
  • 117
2

Another way to do this is:

mongo mongodb://mongoDbIPorDomain:port
KayV
  • 12,987
  • 11
  • 98
  • 148