Wed. Oct 16th, 2024

To connect to a remote MongoDB instance from your laptop, you need to follow these steps:

Ensure Remote Access is Enabled on the MongoDB Server

  • Edit the MongoDB Configuration File:
    • On the server where MongoDB is installed, find and edit the MongoDB configuration file (typically located at /etc/mongod.conf on Linux systems).
    • Look for the line starting with bindIp. By default, this is set to 127.0.0.1, which means MongoDB is only accessible from the localhost. To allow remote connections, change it to:
bindIp: 0.0.0.0 <Server IP Address> <Your Domain Name>

Connecting Remote MongoDB via Compass without Authorization

Confirm that authorization is disabled in /etc/mongod.conf, which means the server will not require user authentication. In the configuration file, this setting would look like:

security:
  authorization: "disabled"

Save the configuration file after making these changes.

Restart MongoDB:

  • Restart the MongoDB service for the changes to take effect:
sudo systemctl restart mongod
OR
sudo service mongod restart

Ensure Port 27017 is Open

  • MongoDB uses port 27017 by default. Ensure that this port is open on your server’s firewall and is accessible from your laptop.
  • For example, using ufw on Ubuntu:
sudo ufw allow 27017

Connect Using MongoDB Compass

  1. Open MongoDB Compass:
    • Launch MongoDB Compass on your laptop.
  2. Create a New Connection:
    • In the MongoDB Compass connection window, enter the following details:
      • Hostname: The IP address or hostname of your MongoDB server.
      • Port: 27017 (or whichever port MongoDB is configured to use).
      • Authentication: Since no authentication is required, you can leave the username and password fields blank.
  3. Additional Options:
    • You can specify any additional connection options if needed, such as SSL/TLS settings, though this is optional.
  4. Connect:
    • Click Connect to establish the connection to your remote MongoDB instance.

Leave a Reply

Your email address will not be published. Required fields are marked *