Allow Remote Connections to MongoDB in AWS Ubuntu Server

Allow Remote Connections to MongoDB in AWS Ubuntu Server

June 8, 2020 / Nirav Shah

What is MongoDB?

As per Wikipedia “MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL)”.

Install MongoDB Community Edition

Step 1

  • Import the public key used by the package management system.
    $ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

    The operation should respond with an OK.

  • However, if you receive an error indicating that gnupg is not installed, you can
  • Install gnupg and its required libraries using the following command:
    $ sudo apt-get install gnupg
  • Once installed, retry importing the key
    $ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Step 2

  • Create a list file for MongoDB
  • Create the list file /etc/apt/sources.list.d/mongodb-org-4.2.list for your version of Ubuntu.
    $ sudo touch /etc/apt/sources.list.d/mongodb-org-4.2

Step 3

  • Copy the command and paste it in the terminal as this will pull the packages to install MongoDB
    	$ echo "deb [ arch=amd64,arm64 ]
    	https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Step 4

  • Reload local package database
    $ sudo apt-get update

Step 5

  • Install the MongoDB packages
    $ sudo apt-get install -y mongodb-org

Step 6

  • Start MongoDB.
    $ sudo systemctl start mongod
  • If you receive an error similar to the following when starting mongod. Failed to start mongod.service: Unit mongod.service not found.
  • Run the following command
    $ sudo systemctl daemon-reload

Step 7

  • Verify that MongoDB has started successfully
    $ sudo systemctl status mongod

Step 8

  • You can optionally ensure that MongoDB will start following a system reboot by issuing the following command
    $ sudo systemctl enable mongod

Step 9 (Optional)

  • Stop MongoDB
    $ sudo systemctl stop mongod

Step 10 (Optional)

  • Restart MongoDB
    $ sudo systemctl restart mongod

Uninstall MongoDB Community Edition

Step 11

  • Stop MongoDB
    $ sudo service mongod stop

Step 12

  • Remove Packages
    $ sudo apt-get purge mongodb-org*

Allow Remote Connections to MongoDB in Ubuntu

  • In Linux, including Ubuntu, bindIp is by default set to 127.0.0.1 in /etc/mongod.conf. This means the mongod process only listens on the local loopback interface.
  • If you set the value of the bindIp to 0.0.0.0 or remove the bindIp option, mongod process will listen on all interfaces.
    	# network interfaces
    	net:
    	port: 27017
    	bindIp: 0.0.0.0
    	
  • Or we can bind mongod process to a specific IP Address
    	# network interfaces
    	net:
    	port: 27017
    	bindIp: 192.168.1.100
    	
  • To bind to multiple IP addresses, enter a list of comma-separated IP addresses
    	# network interfaces
    	net:
    	port: 27017
    	bindIp: 127.0.0.1,192.168.1.100	
    	
  • Note: When you complete editing the configuration file just reload the demon this will apply the changes and restart the mongod service
    	$ sudo systemctl daemon-reload
    	$ sudo systemctl restart mongod
    	

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let’s Talk About Your Needed AWS Infrastructure Management Services

    Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project