MongoDB Shell: How to Connect to a Remote Server

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Connecting to a remote MongoDB server is a common task for developers and database administrators. This tutorial covers the various methods you can use to connect to a MongoDB instance that is not hosted on your local machine.

Before we begin, ensure that you have the MongoDB shell (mongo) installed on your local system. It’s also essential to have network connectivity to the remote server and the necessary permissions to access the database.

Basic Connection

To connect to a MongoDB server, you can use the mongo command followed by the hostname or IP address of your server:

mongo <host>:<port>

Replace <host> with the address of your MongoDB server, and <port> with the port number (the default is 27017).

If the MongoDB instance is running with the default settings, the following command will connect you to the remote server:

mongo remote-server.example.com

If the server is using a non-default port, specify it:

mongo remote-server.example.com:28015

Authentication

Most production databases require authentication. To connect with a username and password, use the following syntax:

mongo -u <username> -p <password> --authenticationDatabase <auth-db> remote-server.example.com

Here, replace <username> and <password> with your credentials. The <auth-db> is the authentication database, usually admin for MongoDB.

For better security, you can also enter the command without the password and the MongoDB shell will prompt you to enter it:

mongo -u <username> --authenticationDatabase <auth-db> remote-server.example.com

After you execute the foregoing command, you will be prompted to enter the password.

Using Connection String (URI)

A MongoDB connection can also be established using a connection string, also known as a Uniform Resource Identifier (URI). This method is particularly useful when you need to specify multiple options or for applications, such as MongoDB drivers and clients that use a connection string:

mongo "mongodb://<username>:<password>@remote-server.example.com:27017/<dbname>?authSource=<auth-db>"

Replace <dbname> with the name of the database you want to connect to and <auth-db> with the name of your authentication database.

The MongoDB URI can contain more complex configurations including replica sets and options like SSL, read preferences, and many others:

mongo "mongodb://user1:password1@host1:27017,user2:password2@host2:27018/?replicaSet=replicaSetName&ssl=true"

Using MongoDB Compass

MongoDB Compass is the official GUI for MongoDB. Although this tutorial focuses on shell access, MongoDB Compass can be used to explore data and perform administrative operations without needing to remember shell commands.

To connect through MongoDB Compass, you would need the host, port, and credentials, and proceed as follows:

Hostname: remote-server.example.com
Port: 27017
Authentication: On
Username: <username>
Password: <password>
Authentication DB: <auth-db>

Connecting via SSH Tunnel

In some cases, direct access to the MongoDB server is not available for security reasons. In such cases, you might need to connect using an SSH tunnel. This creates a secure connection between your local machine and the remote server. Execute the following command in your terminal to set up an SSH tunnel:

ssh -L 59000:localhost:27017 [email protected]

This command maps the remote MongoDB port (27017) to a local port (59000) on your machine. Ensure that the local port you select is free and does not conflict with other services.

Once the SSH tunnel is established, you can connect to MongoDB like this:

mongo --port 59000

Advanced Configuration

In some environments, you might need to specify additional parameters or configurations to connect to your MongoDB server. These may include setting read preferences, write concerns, or using SSL & TLS certifications.

For example, you can specify read and write preferences as follows:

mongo --readPreference nearest --writeConcern { w: "majority" } remote-server.example.com:27017

If your database requires an SSL connection, you need to include SSL options:

mongo --ssl --sslCAFile /path/to/ca.pem --sslPEMKeyFile /path/to/client.pem remote-server.example.com:27017

Conclusion

Connecting to a remote MongoDB server can be accomplished in various ways depending on your environment, security requirements, and personal preferences. Whether you prefer a simple terminal-based approach or require an SSH tunnel for a secure connection, the MongoDB shell and tools such as MongoDB Compass provide flexible options for remote database connection and management.