Category Archives: MongoDB

How to Connect to MongoDB Atlas using Atlas CLI (mac)

by ,

If you are new to the MongoDB, it could be bit overwhelming or confusing to setup with all the tutorials that exist. The MongoDB docs website is very useful and rich of information, however, outside of that, there is not a whole lot of information on how to do this step by step, especially if you have trouble authenticating via the command line. The following steps will show you how to connect via the Terminal on a mac.

First, using Homebrew, install the MongoDB Atlas ClI by running the following command in your terminal.

brew install mongodb-atlas-cli

To check for successful installation, type the following.

atlas

You should see information on how to use the Atlas CLI including examples, available commands and flags.

To create an Atlas account, use the following command.

atlas auth register

You should see the following in your terminal.

Atlas CLI authentication in terminal

 

Press the Enter key. The following window should open in your browser.

MongoDB Atlas create account

 

Create an account. You should then see the identity verification screen appear.

MongoDB Atlas verify identity

 

Verify your email address in your email account. If successful, you will then be redirected back to the login screen.

MongoDB Atlas login screen

 

Log into your new account and you should be presented with the verification code screen. NOTE: Your code may have expired. If so, you will need to generate a new one by pressing Enter or return and then type the following to generate a new validation code.

atlas auth login

In your console, you should see something similar to the following.

Atlas CLI setup

 

Select UserAccount and press Enter. You should see the following output, similar to what you saw previously in first step. Press Enter so and the the following screen will appear. Enter your activation code from terminal.

Atlas CLI authentication in terminal MongoDB Atlas paste or enter verification code

 

Press the Confirm Authorization button

MongoDB Atlas confirm authorization

 

The following screen will appear. NOTE: IT IS RECOMMENDED TO KEEP THIS TAB OPEN UNTIL SETUP IS COMPLETE, OTHERWISE YOU SEE SOME AUTHORIZATION ERROR MESSAGES. MORE ON THAT LATER IN THIS TUTORIAL.

MongoDB Atlas CLI account ready

 

Back in your terminal, choose either json or plain text for your default output format.

Atlas CLI default output format

 

At this point you should be all set. You can also use the Atlas CLI by opening up a new tab and go to https://cloud.mongodb.com/. You should be able to see your account information and organizations.

MongoDB Atlas GUI Dashboard

Authorization Errors

If you accidentally close the Your account is ready window, you may see one of two errors below.

Error unauthorized. To log in using your Atlas username and password or to get credentials using a service account or API keys, run: atlas auth login or Error unauthorized. Error 400 “INVALID_CLIENT_ID”

atlas auth logout error message

Locate the atlascli directory. The configuration files are stored in a hidden directory specific to your operating system.

  • macOS:
    ~/.config/atlascli/
    or
    ~/Library/Application Support/atlascli/
  • Linux:
    ~/.config/atlascli/
  • Windows:
    %AppData%\atlascli\

Delete the files inside this folder. This will remove all saved profiles and credentials. Log in again. Run “atlas auth login” to create a new activation code as shown above and repeat the steps until you confirm the activation code and see and leave open the tab that shows that “Your account is ready”.