MongoDB - Connect with CLI

Connect to your Public Cloud Databases for MongoDB using the Command Line Interface (CLI)

Last updated 09th September 2021


Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

This guide explains how to connect to a MongoDB database instance with a Command Line Interface (CLI).



A MongoDB instance can be managed through multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), also known as a Console or Terminal. To interact correctly with the MongoDB instance, we need to first install something called a MongoDB client, in order to connect and control the MongoDB service remotely. It's a Client-Server interaction.


Install MongoDB Shell on your client

Please note that MongoDB Shell is also natively integrated in the MongoDB community software package.

You first need to install a software to interact with your MongoDB instance remotely. This official software can be installed on various supports like your own computer, a VPS, a virtual machine... the only rule is to be able to reach the public network (Internet) and have sufficient rights to install it.

In order to do so and depending on your configuration, you may need to follow official MongoDB documentation to install MongoDB shell (referred to as mongosh).

Follow the steps here after selecting Windows, MacOS or Linux operation system :

Once installed, you need to catch your IP address in order to authorize connection form this specific client.

If you don't know how to get your IP, please visit a website like Copy the IP address numbers shown on this website and keep them for later. In our example, we will use the (fake) IP

We are now ready to learn how to connect to our MongoDB instance.

Configure your MongoDB instance to accept incoming connections

Before making a connection, we need to verify that our MongoDB instance is correctly configured.

Log in to your OVHcloud Control Panel and switch to Public Cloud in the top navigation bar. After selecting your Public Cloud project, click on Databases in the left-hand navigation bar, and select your MongoDB instance.

Step 1: Verify your user roles and password

Select the Users tab. Verify that you have a user with sufficient rights and a configured password. If you don't remember the user's password, you can either create a new user or regenerate the password of an existing user. Be careful! By doing so you will need to update all the places where you already use this user + password pair.

We provide official MongoDB built-in roles. Please read the official MongoDB documentation to select the right roles for your use-case.

In our example, we will create a user called bastien with the (fake) password Mysup3rs3cur3p4ssw0rd and the roles readWriteAnyDatabase and userAdminAnyDatabase.

User Creation

Once created or updated, the user has to be ready and with the "Enabled" status in the control panel.

User ready

Step 2: Authorize incoming connections from the MongoDB client

In this step, select the Authorised IP's tab (Access Control List). By default, a Public Cloud Database does not accept any form of connection from the outside world. Like this we can help prevent intrusive connection attempts.

Click to authorize a new IP, and enter the previously found IP of your remote client. In our case we will enter

Add an IP

If you want to allow a connection from the outside, you can enter the IP Please use it carefully.

Get your connection information (URI)

Now all the setup should be done, from the remote client and the MongoDB instance.

Select the General Information tab. In the Login Informations section, copy the Service URI.

It should be similar to this when you have a single node (Essential service plan) :


And like this when you have a MongoDB cluster with multiple nodes, called a replica set (Business or Enterprise service plans) :

mongodb://<username>:<password>@<hostname node1>,<hostname node 2>,<hostname node 3>/admin?replicaSet=replicaset&tls=true

A bit of information to know :

  • It will pass the username and password arguments
  • Will connect to the hostname or the replica set (his name is replicaset)
  • to the "admin" database directly
  • on the default MongoDB port (27017)
  • All of that securely, with TLS activated

We will now follow official MongoDB documentation to perform our first connection.

In your CLI, type mongosh --version. The result should look like this :

laptop$ mongosh --version

It means that mongosh is correctly installed and working properly. If you do not see something like this result, please go back to the previous step and reinstall MongoDB Shell.

To perform a connection, simply type mongosh followed by the Service URI copied before :

laptop$mongosh "mongodb://<username>:<password>@<hostname>/admin?tls=true"

Don't forget you need to modify the username, password and hostname. In our example, it will look like this :

laptop$mongosh "mongodb://"

Once connected correctly, you should see something similar to :

Current Mongosh Log ID: 6137881570829aa067222c24
Connecting to:      mongodb://<credentials>
Using MongoDB:      4.4.6
Using Mongosh Beta: 0.12.1

For mongosh info see:

replicaset [direct: primary]>

Congratulations! You are now fully able to interact with your MongoDB instance!

MongoDB Shell usage

Once connected, you can manage your MongoDB instance with built-in MongoDB Shell methods.

Please follow the official MongoDB documentation.

To verify:

  • db : will indicate on which database you are
  • use myNewDatabase will switch to another database (will create one if not existing)
  • db.myCollection.insertOne( ... ) will insert data in your collection

In our example, it will look like this :

replicaset [direct: primary]> db
replicaset [direct: primary]> use myNewDatabase
switched to db myNewDatabase
replicaset [direct: primary]> db
replicaset [direct: primary]> db.myCollection.insertOne( { x: 1 } );
  acknowledged: true,
  insertedId: ObjectId("61378bc0fc37b3df64e48b18")
replicaset [direct: primary]> db.myCollection.find()
[ { _id: ObjectId("61378bc0fc37b3df64e48b18"), x: 1 } ]
replicaset [direct: primary]>

Go further

MongoDB capabilities

Managing a MongoDB service from the OVHcloud Control Panel

Configuring vRack for Public Cloud

Visit our dedicated Discord channel: Ask questions, provide feedback and interact directly with the team that builds our databases services.

Join our community of users on

¿Le ha resultado útil esta guía?

Si lo desea, también puede enviarnos sus sugerencias para ayudarnos a mejorar nuestra documentación.

Imágenes, contenido, estructura...: ayúdenos a mejorar nuestra documentación con sus sugerencias.

No podemos tratar sus solicitudes de asistencia a través de este formulario. Para ello, haga clic en "Crear un tíquet" .

¡Gracias! Tendremos en cuenta su opinión.

Otras guías que podrían interesarle...

OVHcloud Community

¡Acceda al espacio de la OVHcloud Community! Resuelva sus dudas, busque información, publique contenido e interactúe con otros miembros de la comunidad.

Discuss with the OVHcloud community

A partir del 1 de enero de 2015, con arreglo a la Directiva 2006/112/CE modificada, los precios IVA incluido pueden variar según el país de residencia del cliente (por defecto, los precios con IVA incluyen el IVA español vigente).