Test mongodb connection from Ubuntu

Viewed 8

ubuntu terminal, test connection to this mongodb:
mongodb+srv://doadmin:show-password@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin?tls=true&authSource=admin

2 Answers

Here's a step-by-step guide to test a MongoDB connection from an Ubuntu terminal using the command line:

Prerequisites:

  1. You have Ubuntu installed on your machine.
  2. You have a MongoDB Atlas cluster set up with the MongoDB URI provided (replace "doadmin:show-password" with your actual username and password).

Step 1: Install MongoDB Compass (optional)

While not necessary, installing MongoDB Compass can make it easier to connect to your MongoDB cluster. You can install it using the following command:

sudo apt-get update && sudo apt-get install mongodb-compass

Step 2: Open a terminal and navigate to the MongoDB binary directory

Open a terminal on your Ubuntu machine and navigate to the directory where the MongoDB binary is installed. By default, this is usually /usr/bin:

cd /usr/bin/

Step 3: Use the mongo command to connect to your MongoDB cluster

Use the following command to connect to your MongoDB cluster using the provided URI:

mongo mongodb+srv://doadmin:show-password@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin?tls=true&authSource=admin

Replace "doadmin:show-password" with your actual MongoDB username and password.

If everything is set up correctly, you should see a MongoDB shell prompt:

MongoDB shell version v4.4.3
Connecting to: mongodb+srv://doadmin:show-password@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin
MongoDB server version: 4.4.3
>

Step 4: Verify the connection

Enter the following command to verify that you are connected to the correct database:

show dbs

This should list all the databases available in your MongoDB cluster. If you see a list of databases, it means you have successfully connected to your MongoDB cluster.

Troubleshooting

If you encounter any issues while connecting, check the following:

  • Ensure that your MongoDB username and password are correct.
  • Verify that your MongoDB cluster is running and accessible.
  • Check the MongoDB Atlas documentation for troubleshooting tips.

That's it! With these steps, you should be able to test your MongoDB connection from an Ubuntu terminal using the command line.

To test a MongoDB connection from an Ubuntu terminal to the specified MongoDB instance, you will need to follow these steps. These steps assume you have MongoDB command-line tools (mongo or mongosh) installed, and your system meets any additional requirements for a TLS connection.

Prerequisites

  1. MongoDB Shell Installation: Ensure you have either mongo or the newer mongosh installed. You can install mongosh if not already present:

    # Install dependencies
    sudo apt update
    sudo apt install -y curl gnupg
    
    # Import the MongoDB public GPG Key
    curl -fsSL https://pgp.mongodb.com/server-6.0.asc | \
     sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
    
    # Create a list file for MongoDB
    echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.com/apt/ubuntu focal/mongodb-enterprise/6.0 multiverse" | \
    sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list
    
    # Reload local package database
    sudo apt update
    
    # Install the MongoDB Shell (mongosh)
    sudo apt install -y mongodb-mongosh
    
  2. Network Access & Firewall: Make sure your network allows outbound connections to the MongoDB instance on the required ports (usually 27017 for MongoDB).

Testing the Connection

  1. Open Terminal:
    Open your terminal application (Ctrl+Alt+T in Ubuntu).

  2. Run the mongosh Command:
    Use the mongosh tool to connect to your MongoDB instance. Replace <password> with the actual password given in the connection string.

    mongosh "mongodb+srv://doadmin:<password>@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin?tls=true&authSource=admin"
    

    Example command with the placeholder replaced:

    mongosh "mongodb+srv://doadmin:show-password@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin?tls=true&authSource=admin"
    
  3. Verify Connection:
    If the above command runs successfully, you should see an interactive MongoDB shell indicating a successful connection. You can run further commands inside this shell to verify connectivity and interact with your database:

    > show dbs
    

    This will list all databases you have access to.

Troubleshooting

  • Authentication Errors: Ensure the username and password are correct.
  • Network Issues: Confirm that your firewall settings and network configuration permit access to the MongoDB server’s IP and port.
  • Certificate Issues: If there are TLS/SSL certificate errors, you may need to pass additional parameters to either specify a CA file or skip validation (not recommended for production).
# Example using additional parameters for troubleshooting
mongosh --tlsCAFile /path/to/CA.pem --tlsAllowInvalidCertificates "mongodb+srv://doadmin:show-password@private-ducky-mongo-2e08a665.mongo.ondigitalocean.com/admin?tls=true&authSource=admin"

Replace /path/to/CA.pem with the path to your CA file if needed.

These steps should help you test the MongoDB connection from your Ubuntu terminal effectively.