This post shows users and new students that How to Install MongoDB on Ubuntu 18.04 LTS Server.
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.
MongoDB is already in Ubuntu default repositories however, the version in Ubuntu repositories isn’t the latest and greatest. In order to install the latest, you must install MongoDB package repository on Ubuntu, and this tutorial will show you how.
For more details about MongoDB, please check its official website.
Installation Process of MongoDB on Ubuntu
Step 1: Add MongoDB Package repository to Ubuntu
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
Step 2: Install MongoDB on Ubuntu 18.04
sudo apt update
sudo apt install -y mongodb-org
#Step 3: Manage MongoDB
sudo systemctl stop mongod.service
sudo systemctl start mongod.service
sudo systemctl enable mongod.service
By default, MongoDB listens on port 27017. After installing, the local server should be able to communicate with MongoDB. To verify whether MongoDB is running and active, run the commands below:
sudo systemctl status mongod
After run above Commands You should see something like below:
mukesh@ubuntu1604:~$ sudo systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-11-04 10:23 CST; 23min ago
Docs: https://docs.mongodb.org/manual
Main PID: 2383 (mongod)
Tasks: 23
Memory: 60.7M
CPU: 2.613s
CGroup: /system.slice/mongod.service
└─2383 /usr/bin/mongod --config /etc/mongod.conf
Nov 04 10:23:13 ubuntu1604 systemd[1]: Started High-performance, schema-free document-oriented database.
Nov 04 11:23:13 ubuntu1604 systemd[1]: Started High-performance, schema-free document-oriented database.
Run the commands below, to connect to MongoDB shell:
mongo --host 127.0.0.1:27017
After run above Command You should see something like below:
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
Step 4: Adding Admin User
For enable authentication, run the commands to create a new admin user after you’ve logged into MongoDB server.
> use admin
For create a new admin user, run the commands below:
> db.createUser({user:"admin", pwd:"new_password_here", roles:[{role:"root", db:"admin"}]})
After run above Commands you should see a successful admin user created :
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
Exit and continue below to enable MongoDB logon authentication, Run the commands below to open MongoDB config file :
sudo nano /lib/systemd/system/mongod.service
Then change the highlighted line to look like the one below and save :
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
Save and exit
Restart MongDB to make the changes apply.
sudo systemctl daemon-reload
sudo service mongod restart
Now only authetication users will be allowed to access the database server :
mongo -u admin -p new_password_here --authenticationDatabase admin
Step 5: Completely Remove MongoDB
Stop the database server
sudo systemctl stop mongod.service
Remove all packages
sudo apt purge mongodb-org*
Finally, remove MongoDB databases and log files.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
That’s all
If you find any error and issue in above steps , please use comment box below to report.