how can I connect to a remote mongo server from Mac OS terminal

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.

222371 次浏览

您可能连接良好,但是没有足够的权限运行 show dbs

如果在命令行中传递 auth,则不需要运行 db.auth:

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

一旦你连接,你能看到收藏品吗?

> show collections

如果是这样,一切都很好,你只是没有管理权限的数据库,不能运行 show dbs

使用 Mongo 3.2或以上时,只需按如下方式使用连接字符串:

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

另一种方法是:

mongo mongodb://mongoDbIPorDomain:port