How to Install MongoDB on Debian 9

Try it in our public cloud & Get $5 Credit

How to Install MongoDB on Debian 9

MongoDB is a free, open-source document-oriented database, which is classified as a NoSQL database; NoSQL databases are not relational databases but are based on other data storage and retrieval means. Unlike other database programs, such as MySQL and MariaDB, MongoDB is based on JSON-like documents that are flexible, which means fields in different documents can vary and the data structure can change over time. MongoDB also provides users with high availability, smooth scaling, and simple geographic distribution, making it an efficiently utilized solution.

install mongodb

MongoDB was released in February of 2009 and is made available for free use while offering commercial support and other services to consumers.

Getting Started

To install MongoDB on Debian 9, sometimes called Debian Stretch, you will need one node that is running the Linux Debian 9 operating system; the node you choose may be a cloud server node or dedicated server node. The operating system needs to be up to date, and you will need root access to your server.

Root access normally requires a root username and root password, and this allows you to execute commands with higher levels of authority than a standard user would have access to execute.

How to Install MongoDB on Debian 9

The first thing to remember is that each command executed below is executed with root user access.

First, it’s essential to update the server before attempting to install MongoDB:
apt-get update && apt-get upgrade -y

When the server is finishing updating, it’s time to install MongoDB:
apt-get install mongodb

Once MongoDB is installed, you will need to start and stop MongoDB, verifying that it functions correctly:
systemctl start mongodb
systemctl stop mongodb

Now you can check your MongoDB version, making sure it’s the proper version or the version you were expecting:
mongod --version
db version v3.2.11
git version: 009580ad490190ba33d1c6253ebd8d91808923e4
OpenSSL version: OpenSSL 1.0.2l 25 May 2017
allocator: tcmalloc
modules: none
build environment:
distarch: x86_64
target_arch: x86_64

After completing the installation and verifying the MongoDB version, it’s time to test MongoDB, making sure it functions as anticipated on your server:
MongoDB shell version: 3.2.11
connecting to: test
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> exit


Congratulations, you’ve completed installing MongoDB on Linux Debian 9, and it’s time to move your server into production. If you found this guide helpful, please share it with other users that are interested in setting up the same document-oriented database system.