Introduction
MongoDB is a free and open source NoSQL document database commonly used in modern web applications. This tutorial will help you set up MongoDB for a production application environment on the server.
To follow this tutorial, you need:
An Ubuntu** server** with a non-root account that can use the sudo
command has been set up, and the firewall has been turned on. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.
MongoDB has been included in the Ubuntu package repository, but the official MongoDB repository provides the latest version, which is the recommended way to install the software. In this step, we add this official repository to our server.
Ubuntu ensures the authenticity of the packages by verifying that they are signed with GPG keys, so we must first import their keys into the official MongoDB repository.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80--recv EA312927
After successfully importing the key, you will see:
gpg: Total number processed:1
gpg: imported:1(RSA:1)
Next, we must add the MongoDB repository details so that apt
knows where to download the package.
Issue the following command to create a listing file for MongoDB.
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse"| sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
After adding the repository details, we need to update the package list.
sudo apt-get update
Now we can install the MongoDB package ourselves.
sudo apt-get install -y mongodb-org
This command will install multiple packages containing the latest stable version of MongoDB and useful management tools for the MongoDB server.
Next, start MongoDB with systemctl
.
sudo systemctl start mongod
You can also use systemctl
to check whether the service has started correctly.
sudo systemctl status mongod
● mongodb.service - High-performance, schema-free document-oriented database
Loaded:loaded(/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active:active(running) since Mon 2016-04-2514:57:20 EDT; 1min 30s ago
Main PID:4093(mongod)
Tasks:16(limit:512)
Memory:47.1M
CPU:1.224s
CGroup:/system.slice/mongodb.service
└─4093/usr/bin/mongod --quiet --config /etc/mongod.conf
The last step is to enable MongoDB at system startup.
sudo systemctl enable mongod
The MongoDB server is now configured and running. You can use the systemctl
command to manage MongoDB services (for example, sudo systemctl stop mongod
, sudo systemctl start mongod
).
Assuming that you have enabled a firewall on the server, you cannot access the MongoDB server from the Internet.
If you plan to only use the MongoDB server locally and the application running on the same server, it is recommended to use security settings. However, if you want to be able to connect to the MongoDB server from the Internet, we must allow incoming connections in ufw
.
To allow 27017
to access MongoDB on the default port from anywhere, you can use sudo ufw allow 27017
. However, enabling Internet access to the MongoDB server on the default installation allows unrestricted access to the entire database server.
In most cases, MongoDB can only be accessed from certain trusted locations, such as another server hosting the application. To accomplish this task, you can allow access to MongoDB's default port, while specifying the IP address of another server that will explicitly allow connections.
sudo ufw allow from your_other_server_ip/32 to any port 27017
You can use the following ufw
command to verify the firewall settings changes.
sudo ufw status
You should see the port traffic allowed by 27017
in the output. If you decide to only allow a certain IP address to connect to the MongoDB server, the IP address of the allowed location will be listed in the output instead of Anywhere.
Status: active
To Action From
- - - - - - - - - - - - 27017 ALLOW Anywhere
OpenSSH ALLOW Anywhere
27017( v6) ALLOW Anywhere(v6)OpenSSH(v6) ALLOW Anywhere(v6)
You have successfully installed MongoDB on Ubuntu 16.04.
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How to Install MongoDB on Ubuntu 16.04"
Recommended Posts