Mongodb – How to connect to Mongodb Atlas from command line

mongodb-4.0

I am currently taking the m220 lecture for javascript developer, and i am having issues connecting with my Atlas sandbox, I get the following output when I try to connect. my connection string is in bold.

C:\Users\OLAMI\Desktop\Coding Files\Database\MONGO LECTURES\mflix-js (2)>**mongo "mongodb+srv://m220student: m220password@mflix-4prbz.mongodb.net/test?retryWrites=true"**

MongoDB shell version v4.0.6
connecting to: mongodb://mflix-shard-00-00-4prbz.mongodb.net.:27017,mflix-shard-00-01-4prbz.mongodb.net.:27017,mflix-shard-00-02-4prbz.mongodb.net.:27017/test?authSource=admin&gssapiServiceName=mongodb&replicaSet=mflix-shard-0&retryWrites=true&ssl=true
2019-04-13T07:10:53.729+0100 I NETWORK [js] Starting new replica set monitor for mflix-shard-0/mflix-shard-00-00-4prbz.mongodb.net.:27017,mflix-shard-00-01-4prbz.mongodb.net.:27017,mflix-shard-00-02-4prbz.mongodb.net.:27017
2019-04-13T07:10:55.190+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-00-4prbz.mongodb.net.:27017 (1 connections now open to mflix-shard-00-00-4prbz.mongodb.net.:27017 with a 5 second timeout)
2019-04-13T07:10:55.300+0100 I NETWORK [js] Successfully connected to mflix-shard-00-02-4prbz.mongodb.net.:27017 (1 connections now open to mflix-shard-00-02-4prbz.mongodb.net.:27017 with a 5 second timeout)
2019-04-13T07:10:55.397+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] changing hosts to mflix-shard-0/mflix-shard-00-00-4prbz.mongodb.net:27017,mflix-shard-00-01-4prbz.mongodb.net:27017,mflix-shard-00-02-4prbz.mongodb.net:27017 from mflix-shard-0/mflix-shard-00-00-4prbz.mongodb.net.:27017,mflix-shard-00-01-4prbz.mongodb.net.:27017,mflix-shard-00-02-4prbz.mongodb.net.:27017
2019-04-13T07:10:56.333+0100 I NETWORK [js] Successfully connected to mflix-shard-00-00-4prbz.mongodb.net:27017 (1 connections now open to mflix-shard-00-00-4prbz.mongodb.net:27017 with a 5 second timeout)
2019-04-13T07:10:56.371+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-01-4prbz.mongodb.net:27017 (1 connections now open to mflix-shard-00-01-4prbz.mongodb.net:27017 with a 5 second timeout)
2019-04-13T07:10:57.430+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-02-4prbz.mongodb.net:27017 (1 connections now open to mflix-shard-00-02-4prbz.mongodb.net:27017 with a 5 second timeout)
2019-04-13T07:10:57.818+0100 I NETWORK [js] Marking host mflix-shard-00-00-4prbz.mongodb.net:27017 as failed :: caused by :: Location40659: can’t connect to new replica set master [mflix-shard-00-00-4prbz.mongodb.net:27017], err: Location8000: bad auth Authentication failed.
2019-04-13T07:10:58.241+0100 I NETWORK [js] Marking host mflix-shard-00-02-4prbz.mongodb.net:27017 as failed :: caused by :: SocketException: can’t authenticate against replica set node mflix-shard-00-02-4prbz.mongodb.net:27017 :: caused by :: socket exception
[CONNECT_ERROR] server [mflix-shard-00-02-4prbz.mongodb.net:27017] connection pool error: network error while attempting to run command ‘isMaster’ on host 'mflix-shard-00-02-4prbz.mongodb.net:27017’
2019-04-13T07:10:58.640+0100 I NETWORK [js] Marking host mflix-shard-00-01-4prbz.mongodb.net:27017 as failed :: caused by :: SocketException: can’t authenticate against replica set node mflix-shard-00-01-4prbz.mongodb.net:27017 :: caused by :: socket exception
[CONNECT_ERROR] server [mflix-shard-00-01-4prbz.mongodb.net:27017] connection pool error: network error while attempting to run command ‘isMaster’ on host 'mflix-shard-00-01-4prbz.mongodb.net:27017’
2019-04-13T07:11:00.091+0100 I NETWORK [js] Marking host mflix-shard-00-00-4prbz.mongodb.net:27017 as failed :: caused by :: Location40659: can’t connect to new replica set master [mflix-shard-00-00-4prbz.mongodb.net:27017], err: Location8000: bad auth Authentication failed.
2019-04-13T07:11:00.092+0100 E QUERY [js] Error: can’t authenticate against replica set node mflix-shard-00-00-4prbz.mongodb.net:27017 :: caused by :: can’t connect to new replica set master [mflix-shard-00-00-4prbz.mongodb.net:27017], err: Location8000: bad
auth Authentication failed. :
connect@src/mongo/shell/mongo.js:343:13
@(connect):1:6
exception: connect failed

Best Answer

How to connect to Mongodb Atlas from command line

To connect with MongoDB atlas from the command line as first you have to Sign In the MongoDB Atlas cluster with your login credentials.

After login in Atlas cluster connect with your cluster name and click on connect button. enter image description here

Then click on Connect with the Mongo Shell

enter image description here

Then according to your environment setup choose the appropriate mongo shell (as in my place I have been already installed the MongoDB v4.0.8. So, I have choose the (I have the Mongo Shell installed) then copy the command

enter image description here

mongo "mongodb+srv://mflix-xxxxx.mongodb.net/test" --username m220student

Note: Here I have written xxxxx due to the security reason. In your sandbox there will be different address (Everyone will have different address of his/her cluster)

Important: You will be prompted for the password for the m220student user's (MongoDB User) username. When entering your password, make sure that any special characters are URL encoded.

If you have installed the MongoDB then open the command prompt with MongoDB BIN directory path. Which is as shown below

C:\Program Files\MongoDB\Server\4.0\bin>mongo "mongodb+srv://mflix-xxxxx.mongodb.net/test" --username m220student
MongoDB shell version v4.0.8
Enter password:        --- type here the password
connecting to: mongodb://mflix-shard-00-00-xxxxx.mongodb.net.:27017,mflix-shard-00-01-xxxxx.mongodb.net.:27017,mflix-shard-00-02-xxxxx.mongodb.net.:27017/test?authSource=admin&gssapiServiceName=mongodb&replicaSet=mflix-shard-0&ssl=true
2019-04-14T10:10:19.943+0300 I NETWORK  [js] Starting new replica set monitor for mflix-shard-0/mflix-shard-00-00-xxxxx.mongodb.net.:27017,mflix-shard-00-01-xxxxx.mongodb.net.:27017,mflix-shard-00-02-xxxxx.mongodb.net.:27017
2019-04-14T10:10:21.193+0300 I NETWORK  [js] Successfully connected to mflix-shard-00-00-xxxxx.mongodb.net.:27017 (1 connections now open to mflix-shard-00-00-xxxxx.mongodb.net.:27017 with a 5 second timeout)
2019-04-14T10:10:21.197+0300 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-01-xxxxx.mongodb.net.:27017 (1 connections now open to mflix-shard-00-01-xxxxx.mongodb.net.:27017 with a 5 second timeout)
2019-04-14T10:10:21.406+0300 I NETWORK  [ReplicaSetMonitor-TaskExecutor] changing hosts to mflix-shard-0/mflix-shard-00-00-xxxxx.mongodb.net:27017,mflix-shard-00-01-xxxxx.mongodb.net:27017,mflix-shard-00-02-xxxxx.mongodb.net:27017 from mflix-shard-0/mflix-shard-00-00-xxxxx.mongodb.net.:27017,mflix-shard-00-01-xxxxx.mongodb.net.:27017,mflix-shard-00-02-xxxxx.mongodb.net.:27017
2019-04-14T10:10:22.240+0300 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-00-xxxxx.mongodb.net:27017 (1 connections now open to mflix-shard-00-00-xxxxx.mongodb.net:27017 with a 5 second timeout)
2019-04-14T10:10:22.242+0300 I NETWORK  [js] Successfully connected to mflix-shard-00-01-xxxxx.mongodb.net:27017 (1 connections now open to mflix-shard-00-01-xxxxx.mongodb.net:27017 with a 5 second timeout)
2019-04-14T10:10:23.288+0300 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to mflix-shard-00-02-xxxxx.mongodb.net:27017 (1 connections now open to mflix-shard-00-02-xxxxx.mongodb.net:27017 with a 5 second timeout)
Implicit session: session { "id" : UUID("ccee2086-56dc-496e-b667-8d2a5276c52b") }
MongoDB server version: 4.0.8
Error while trying to show server startup warnings: user is not allowed to do action [getLog] on [admin.]
MongoDB Enterprise mflix-shard-0:PRIMARY> show dbs
admin  0.000GB
local  2.769GB
mflix  0.095GB
MongoDB Enterprise mflix-shard-0:PRIMARY>
MongoDB Enterprise mflix-shard-0:PRIMARY> use mflix
switched to db mflix
MongoDB Enterprise mflix-shard-0:PRIMARY> show collections
comments
movies
theaters
users
MongoDB Enterprise mflix-shard-0:PRIMARY>

Here, you can see that I am able to connect with MongoDB Atlas cluster with mongo shell.

For further your ref here