Setting mongodb for remote access

Created at 2018-01-30 Updated at 2024-11-07 - 2 min. read

Setting up mongodb on remote system with remote access.

  1. Install the mongo database from here.

  2. I am using ubuntu 16.04. Check if its running or not.
    sudo service mongod status.

  3. Try to connect it over locally by running command mongo
  4. create a user and assign him the role userAdminAnyDatabase

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    use admin
    var user = {
    "user" : "admin",
    "pwd" : "manager",
    roles : [
    {
    "role" : "userAdminAnyDatabase",
    "db" : "admin"
    }
    ]
    }

    db.createUser(user);
    exit
  5. Connect with the Admnistrator user

    1
    mongo admin -u admin -p manager
  6. Create Application User for test database

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    mongo admin -u admin -p manager
    use test
    var user = {
    "user" : "website",
    "pwd" : "abc123",
    roles : [
    {
    "role" : "readWrite",
    "db" : "test"
    }
    ]
    }

    db.createUser(user);

    exit
  7. Make your mongo accessible from remote
    Open the mongo config file

    sudo nano /etc/mongod.conf

    Change the following in the above file

    1
    2
    3
    4
    # network interfaces
    net:
    port: 27017
    bindIp: 0.0.0.0 <- change this
  8. Set authentication on your mongo.

    1
    sudo nano /etc/mongod.conf

    Add the following line at the end of file

    1
    2
    security:
    authorization: enabled
  9. Restart the service

    1
    sudo service mongod restart

Now you could easily connect to mongo from remote using the username and password you created.

Table of Content

Site by Ashutosh Kumar Singh using Hexo & Random

Traveller - Developer - Foodie - Biker - Cyclist - Rider - Technocrat