MongoDB is a fast and powerful NoSQL database. However, Debian repositories update slowly and often contain very old versions of packages. This tutorial explains how to install the latest version of MongoDB from the official MongoDB repository.
At the time of writing, 3.0 was the current version of MongoDB.
All commands below must be performed from the root user.
The following command adds the MongoDB public GPG Key to the system key ring:
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10Debian package management tools need this key to ensure package consistency and authenticity.
Create a mongodb.list file to add the MongoDB repository to the system. The following command will create this file:
echo "deb http://repo.mongodb.org/apt/debian "$(lsb_release -sc)"/mongodb-org/3.0 main" | tee /etc/apt/sources.list.d/mongodb-org-3.0.listUse the following command to get info about the latest packages in repositories:
apt-get updateIf you are familiar with Debian, you probably already know this command.
Now it’s time to install MongoDB. To install the latest stable version of MongoDB, use the following command:
apt-get install -y mongodb-orgmongodb-org is a metapackage That will automatically install the four component packages:
mongodb-org-server
mongodb-org-mongos
mongodb-org-shell
mongodb-org-tools
If you need a specific version of MongoDB, you can use the following command to choose the version of each component:
apt-get install -y mongodb-org=3.0.0 mongodb-org-server=3.0.0 mongodb-org-shell=3.0.0 mongodb-org-mongos=3.0.0 mongodb-org-tools=3.0.0In this example, I have selected version 3.0.0.
After the installation finishes, MongoDB is automatically started. If you need to manually run the MongoDB server, then use the following command:
service mongod startTo stop the Mongod process, use this command:
service mongod stopTo restart Mongod, use the following:
service mongod restartThe MongoDB shell can be accessed by running the following command. This allows you to connect to the MongoDB server.
mongoWhen the shell is launched, you will see output in this format:
MongoDB shell version: 3.0.0
connecting to: test
>Now you can use MongoDB on your Debian server.