Get Started with MongoDB on Ubuntu in Just a Few Steps
MongoDB is an open source document-oriented NoSQL database that stores data in JSON-like documents. It is one of the most popular NoSQL databases in the world and is used by many companies and organizations. This guide will show you how to get started with MongoDB on Ubuntu in just a few steps.
Prerequisites
Before you get started, you will need to have a server running Ubuntu 18.04 or higher. You will also need to have a user with sudo privileges.
Step 1: Install MongoDB
The first step is to install MongoDB. We will be using the official MongoDB repository to install the latest version.
First, add the MongoDB repository to your system’s list of sources:
By default, MongoDB is configured to listen on all interfaces on port 27017. To configure the server to only listen on the localhost interface, edit the configuration file located at /etc/mongod.conf:
“`
sudo nano /etc/mongod.conf
“`
Find the line that reads:
“`
net:
port: 27017
bindIp: 127.0.0.1
“`
Save and close the file, then restart the MongoDB service for the changes to take effect:
“`
sudo systemctl restart mongod
“`
Step 3: Secure MongoDB
MongoDB is configured with no authentication by default, which means anyone can access your database. To secure the server, we need to create an administrative user and enable authentication.
First, connect to the MongoDB shell:
“`
mongo
“`
Once connected, create a new user with the userAdminAnyDatabase role:
Once the user is created, exit the shell and edit the configuration file again:
“`
sudo nano /etc/mongod.conf
“`
Uncomment the security section and add the following lines:
“`
security:
authorization: enabled
“`
Save and close the file, then restart the MongoDB service for the changes to take effect:
“`
sudo systemctl restart mongod
“`
In this guide, we have shown you how to get started with MongoDB on Ubuntu in just a few steps. We have installed MongoDB, configured it to listen on the localhost interface, and enabled authentication. You should now be ready to start using MongoDB for your projects.
Get Started with MongoDB on Ubuntu in Just a Few Steps
Get Started with MongoDB on Ubuntu in Just a Few Steps
MongoDB is an open source document-oriented NoSQL database that stores data in JSON-like documents. It is one of the most popular NoSQL databases in the world and is used by many companies and organizations. This guide will show you how to get started with MongoDB on Ubuntu in just a few steps.
Prerequisites
Before you get started, you will need to have a server running Ubuntu 18.04 or higher. You will also need to have a user with sudo privileges.
Step 1: Install MongoDB
The first step is to install MongoDB. We will be using the official MongoDB repository to install the latest version.
First, add the MongoDB repository to your system’s list of sources:
“`
sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo “deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
“`
Once the repository is added, update the package list and install the MongoDB package:
“`
sudo apt-get update
sudo apt-get install -y mongodb-org
“`
Once the installation is complete, start the MongoDB service and enable it to start on boot:
“`
sudo systemctl start mongod
sudo systemctl enable mongod
“`
Step 2: Configure MongoDB
By default, MongoDB is configured to listen on all interfaces on port 27017. To configure the server to only listen on the localhost interface, edit the configuration file located at /etc/mongod.conf:
“`
sudo nano /etc/mongod.conf
“`
Find the line that reads:
“`
net:
port: 27017
bindIp: 127.0.0.1
“`
Save and close the file, then restart the MongoDB service for the changes to take effect:
“`
sudo systemctl restart mongod
“`
Step 3: Secure MongoDB
MongoDB is configured with no authentication by default, which means anyone can access your database. To secure the server, we need to create an administrative user and enable authentication.
First, connect to the MongoDB shell:
“`
mongo
“`
Once connected, create a new user with the userAdminAnyDatabase role:
“`
use admin
db.createUser({ user: “admin”,
pwd: “strong_password”,
roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
});
“`
Once the user is created, exit the shell and edit the configuration file again:
“`
sudo nano /etc/mongod.conf
“`
Uncomment the security section and add the following lines:
“`
security:
authorization: enabled
“`
Save and close the file, then restart the MongoDB service for the changes to take effect:
“`
sudo systemctl restart mongod
“`
In this guide, we have shown you how to get started with MongoDB on Ubuntu in just a few steps. We have installed MongoDB, configured it to listen on the localhost interface, and enabled authentication. You should now be ready to start using MongoDB for your projects.
Archives
Recent
Categories
Meta
Calendar