In this MongoDB tutorial, we explain how to install the database on Debian 10 Buster and give the basics of MongoDB`
MongoDB is a database engine that provides access to non-relational, document-oriented databases. It is part of the growing NoSQL movement, along with databases like Redis and Cassandra (although there are vast differences among the many non-relational databases).
MongoDB seeks to provide an alternative to traditional relational database management systems (RDBMS). In addition to its schema-free design and scalable architecture, MongoDB provides a JSON output and specialized, language-specific bindings that make it particularly attractive for use in custom application development and rapid prototyping. MongoDB has been used in a number of large scale production deployments and is currently one of the most popular database engines across all systems.
MongoDB require significant amount of Memory. If you are running MongoDB on production environment, ensure the system has adequate memory. For testing purposes on localhost, you can get away with at least 8GB of RAM.
Getting Started
- Ensure to secure your system if you are running the database on a remote server and restrict access to critical network services.
Update your system:
sudo apt-get update && sudo apt-get upgrade -y
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo
.
Add the MongoDB Repository
The mongodb
package provided by Debian is not maintained by MongoDB Inc. and conflicts with the official mongodb-org
package.
Update your repositories. This allows apt
to read from the newly added MongoDB repo:
sudo apt-get update
Add the MongoDB repository to your sources.list.d
directory:
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Import the MongoDB public GPG key for package signing.
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Install MongoDB
Now that the MongoDB repository has been added, we’re ready to install the latest stable version of MongoDB:
sudo apt-get install -y mongodb-org
This command installs mongodb-org
, a meta-package that includes the following:
mongodb-org-server
- The standard MongoDB daemon, and relevant init scripts and configurationsmongodb-org-mongos
- The MongoDB Shard daemonmongodb-org-shell
- The MongoDB shell, used to interact with MongoDB via the command linemongodb-org-tools
- Contains a few basic tools to restore, import, and export data, as well as a variety of other functions.
These packages provide a good base that will serve most use cases, and we recommend installing them all. However, if you want a more minimal installation, you can selectively install packages from the above list rather than using the mongodb-org
metapackage.
Configure MongoDB
The configuration file for MongoDB is located at /etc/mongod.conf
, and is written in YAML format. Most of the settings are well commented within the file. We’ve outlined the default options below:
dbPath
indicates where the database files will be stored (/var/lib/mongodb
by default)systemLog
specifies the various logging options, explained below:destination
tells MongoDB whether to store the log output as a file or sysloglogAppend
specifies whether to append new entries to the end of an existing log when the daemon restarts (as opposed to creating a backup and starting a new log upon restarting)path
tells the daemon where to send its logging information (/var/log/mongodb/mongod.log
by default)
net
specifies the various network options, explained below:port
is the port on which the MongoDB daemon will runbindIP
specifies the IP addresses MongoDB to which binds, so it can listen for connections from other applications
These are only a few basic configuration options that are set by default.
We strongly recommend uncommenting the security
section and adding the following:
security:
authorization: enabled
The authorization
option enables role-based access control for your databases. If no value is specified, any user will have the ability to modify any database. We’ll explain how to create database users and set their permissions later in this guide.
For more information on how to customize these and other values in your configuration file, refer to the official MongoDB configuration tutorial.
After making changes to the MongoDB configuration file, restart the service as shown in the following section.
Start and Stop MongoDB
To start, restart, or stop the MongoDB service, issue the appropriate command from the following:
sudo systemctl start mongod
sudo systemctl restart mongod
sudo systemctl stop mongod
You can also enable MongoDB to start on boot:
sudo systemctl enable mongod
Create Database Users
If you enabled role-based access control in the Configure MongoDB section, create a user administrator with credentials for use on the database:
Exit the mongo shell:
quit()
Permissions for different databases are handled in separate roles
objects. This example creates the user, example-user
, with read-only permissions for the user-data
database and has read and write permissions for the exampleDB
database that we’ll create in the Manage Data and Collections section below.Create a new, non-administrative user to enter test data. Change both example-user
and password
to something relevant and secure:
db.createUser({user: "example-user", pwd: "password", roles:[{role: "read", db: "user-data"}, {role:"readWrite", db: "exampleDB"}]})
To create additional users, repeat Steps 6 and 7 as the administrative user, creating new usernames, passwords and roles by substituting the appropriate values.
As the mongo-admin
user, create a new database to store regular user data for authentication. The following example calls this database user-data
:
use user-data
Test your connection to MongoDB with the credentials created in Step 3, using the admin
database for authentication:
mongo -u mongo-admin -p --authenticationDatabase admin
The -u
, -p
, and --authenticationDatabase
options in the above command are required in order to authenticate connections to the shell. Without authentication, the MongoDB shell can be accessed but will not allow connections to databases.The mongo-admin
user created in Step 3 is purely administrative based on the roles specified. It is defined as an administrator of users for all databases, but does not have any database permissions itself. You may use it to create additional users and define their roles. If you are using multiple applications with MongoDB, set up different users with custom permissions for their corresponding databases.
Exit the mongo shell:
quit()
Use the following command to create an administrative user with the ability to create other users on any database. For better security, change the values mongo-admin
and password
:
db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})
Keep these credentials in a safe place for future reference. The output will display all the information written to the database except the password:
Successfully added user: {
"user" : "mongo-admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
By default, MongoDB connects to a database called test
. Before adding any users, create a database to store user data for authentication:
use admin
Open the mongo
shell:
mongosh
For more information on access control and user management, as well as other tips on securing your databases, refer to our other MongoDB tutorials.
Uninstalling MongoDB
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.
Stop MongoDB
Stop the MongoDB service.
sudo service mongod stop
Remove Packages
Uninstall the available packages:
sudo apt-get purge mongodb-org*
Remove Data
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
And that should do it.