samettonyali / MongoDBStudies

MongoDB Installation and a Sample Project

Home Page:https://www.digitalocean.com/community/tutorials/how-to-integrate-mongodb-with-your-node-application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MongoDBStudies

MongoDB Installation and Sample Projects

This project explains how MongoDB is installed on Ubuntu 18.04 LTS and includes some MongoDB projects to explain how it is accessed over applications (Web or Desktop).

Step 1 - MongoDB Installation on Ubuntu 18.04 LTS

Prerequisites

You may be required to import the software distributor's GPG keys to ensure the consistency and authenticity of the package. Execute this command to import MongoDB keys to your server.

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 68818C72E52529D4

Create a MongoDB list file in /etc/apt/sources.list.d/ by executing this command

$ sudo echo "deb http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Installation

Update the repository...

$ sudo apt update

and install MongoDB

$ sudo apt install -y mongodb

You can verify its status by running the command shown below

$ sudo systemctl status mongodb

Or you can see that it has been started listening on port 27017 with the netstat command below

$ sudo netstat -plntu

Step 2 - MongoDB Service Management

As soon as MongoDB is installed it starts running. In order to stop, start, restart, disable, or enable it you should execute these service commands, respectively.

$ sudo systemctl stop mongodb

$ sudo systemctl start mongodb

$ sudo systemctl restart mongodb

$ sudo systemctl disable mongodb

$ sudo systemctl enable mongodb

Step 3 - Remote Access on Firewall

MongoDB runs on port 27017 by default. However, enabling access to MongoDB from everywhere gives unrestricted access to the database data, which is a security threat for applications. Therefore, we need to give access to specific IP address location to default MongoDB's port by executing following command.

$ sudo ufw allow from yourServerIP/32 to any port 27017

$ sudo ufw status

If the firewall is inactive it can be enabled by executing the following command.

$ sudo ufw enable

Also, you need to add your server IP address to /etc/mongodb.conf configuration file as shown below

bind_ip = 127.0.0.1,yourServerIP

#port = 27017

After the modification and exiting the editor you should restart MongoDB.

$ sudo systemctl restart mongodb

Step 4 - Creating MongoDB Credentials

User authentication in MongoDB is disabled by default. It means that the database has been started without any access control. In order to create MongoDB credentials, execute following commands.

$ mongo

> show dbs

Switch to the admin database, and then create the root user executing the following commands.

> use admin

> db.createUser({user:"root", pwd:"yourPasswordHere", roles:[{role:"root", db:"admin"}]})

In addition, you should modify 9th line in /lib/systemd/system/mongod.service as shown below.

ExecStart=/usr/bin/mongod --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS

After modifying the configuration file the daemon should be reloaded, and the service should be restarted.

$ systemctl daemon-reload

$ sudo systemctl restart mongodb

Now you should authenticate yourself as a MongoDB user while connecting to the database.

$ mongo -u "root" -p --authenticationDatabase "admin"

After this point, you are required to enter your password in order to access to the database.

About

MongoDB Installation and a Sample Project

https://www.digitalocean.com/community/tutorials/how-to-integrate-mongodb-with-your-node-application


Languages

Language:HTML 62.7%Language:JavaScript 25.3%Language:CSS 8.6%Language:Dockerfile 3.4%