Search code examples
mongodbazuredockermongodb-compass

How to connect mongoDB Compass to mongoDB container which runs in azure VM?


I have mongoDB container which runs on azure VM, and I'm trying to connect it to my mongoDB compass. I have Public IP address to my VM, the port 27017 is open in my vm and also in my mongo container.

I have authentication, so to connect my mongo I'm Enter the mongo container and write the command "mongo -u username -p password --authenticationDatabase admin" (Relevant).

When I'm trying to connect I get "connection timed out" error message.

docker container ls

enter image description here

Open ports on the VM

enter image description here

My compass login page

enter image description here


Solution

  • I solved it by changing the configuration file of the mongoDB container.

    Step 1:, make sure port 27017 is open on the VM:

    enter image description here

    Step 2:, create a mongoDB configuration file as below, name it mongod.conf, and change the bindIp field to your host IP (change < Host IP> to your host IP).

    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # Where and how to store data.
    storage:
      dbPath: /data/db
      journal:
        enabled: true
    #  engine:
    #  mmapv1:
    #  wiredTiger:
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log
    
    # network interfaces
    net:
      port: 27017
      bindIp: 127.0.0.1,<Host IP>
    
    
    # how the process runs
    processManagement:
      timeZoneInfo: /usr/share/zoneinfo
    
    security:
      authorization: enabled
    
    #operationProfiling:
    
    #replication:
    
    #sharding:
    
    ## Enterprise-Only Options:
    
    #auditLog:
    
    #snmp:
    

    Step 3: Copy the mogod.conf file into your VM, and save it wherever you want.

    Step 4: Run the command:

     docker run -d -v <FolderPathOnTheVM>/mongod.conf:/etc/mongod.conf -p 27017:27017 mongo -f /etc/mongod.conf
    

    Make sure you changed the < FolderPathOnTheVM> to the path of the mongod.conf file on the VM (The path of step 3).