MongoDB is currently the most popular NoSQL database system and fourth in the overall ranking of database systems! The growth of MongoDB has been impressive over the last few years as a document-oriented database system that uses JSON-like objects for describing the data model.
Non-Relational Databases for Scalability
In a sense, the non-relational database has been rediscovered in the last few years. Many leading tech companies like Google, Facebook, and Amazon were already using non-relational databases in their large production clusters, when in 2009 the term “NoSQL” was first coined to give the various non-SQL database system implementations a common name.
From that point, the use of NoSQL databases among many other companies (especially startups) and open-source projects has been growing ever since.
The NoSQL database systems are particularly well-suited for web services that need to scale (by using simpler data replication across a cluster of machines) and also to adapt to quickly-changing data models (by using dynamic schemas). The NoSQL data structures used by the various database implementations can be: key-value stores, graphs, or documents.
Install MongoDB via Standard Ubuntu Packages
Ubuntu includes its own MongoDB packages. However, the official MongoDB packages are generally more up-to-date.
We recommend using the official MongoDB packages, especially for production services, but if you want, you can use the following command to install the MongoDB packages that are included in Ubuntu.
sudo apt-get install mongodb mongodb-clients
This will install the MongoDB server and the related client tools (see the next section for the list).
Install MongoDB via Official MongoDB Packages
Installing MongoDB with the official MongoDB packages is recommended for production.
Step 1: Import the MongoDB Public GPG Key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Step 2: Add the MongoDB Repository to Your Package Sources
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
Step 3: Update the Package List
sudo apt-get update
Step 4: Install the MongoDB Packages
sudo apt-get install -y mongodb-org
The output of this command will show the list of MongoDB packages that will be installed:
... The following extra packages will be installed: mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools The following NEW packages will be installed: mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. Need to get 114 MB of archives. ...
During the install, a new user named mongodb will be created, and the mongodb server will be started automatically.
After the install, the following MongoDB executables should be available:
- mongod – The primary daemon process for the MongoDB system
- mongodump – Creates a binary export of the contents of a database
- mongoexport – Produces a JSON or CSV export of MongoDB data
- mongofiles – Manipulates files stored in MongoDB as GridFS objects
- mongoimport – Imports content from JSON, CSV, or TSV
- mongooplog – Polls operations from the replication oplog of a remote server
- mongoperf – Checks disk I/O performance independently of MongoDB
- mongorestore – Writes data from a binary database dump to a MongoDB instance
- mongos – A routing service for determining the location of data in a sharded cluster
- mongostat – Provides a quick overview of a currently running mongod or mongos instance
- mongotop – Tracks the amount of time a MongoDB instance spends reading and writing data
Now we can check what version of MongoDB we just installed:
$ mongod --version db version v3.0.0 git version: a841fd6394365954886924a35076691b4d149168 OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
That is the latest stable MongoDB version. Now we’ll see how we can use MongoDB.
Now we’ll discuss a couple details regarding utilizing MondoDB in general — from server management to security maintenance, and more.
Controlling the MongoDB server
Note that the upstart-scripts (which were just installed with our MongoDB package) will automatically start and stop the MongoDB server for you during bootups and shutdowns. However, sometimes you need to stop and start the server manually.
Stopping the server (as mongod should already be running now):
$ sudo service mongod stop mongod stop/waiting
Starting the server:
$ sudo service mongod start mongod start/running, process 1652
Restarting the server (useful when the configuration files have been changed and need reloading):
$ sudo service mongod restart mongod stop/waiting mongod start/running, process 1682
MongoDB Configuration and Logs
The MongoDB server configuration file is located in /etc/mongod.conf. The options of this file are described here.
Normally, the default configuration setup is fine to begin with, but as the next section will make clear, you will need to edit this file for various security measures. If you have made changes to this file, you will need to restart your MongoDB server.
By default, MongoDB stores its data in /data/db/ and the log file is located at /var/log/mongodb/mongod.log.
MongoDB Security and Access Control
Before you begin your exploration of this great NoSQL database, it’s important to also think about the security of your MongoDB installation. As NoSQL databases are most often used in a web service and cluster-computing context, good security measures are critical.
Recently there was a major security issue with tens of thousands of vulnerable MongoDB installs on the Internet. The issue was not due to MongoDB itself, but due to the fact that the users of MongoDB were not applying proper access controls to their server configuration.
MongoDB has excellent documentation on how to apply the right security measures. At the very least, you should implement the relevant items on this security checklist before connecting your production MongoDB server to the Internet.
Check out these links for more on MongoDB and security:
Getting Started with MongoDB
Now we are ready to start doing MongoDB database operations. The official MongoDB beginners tutorial will quickly guide you into understanding how these operations are done.
The tutorial starts by executing the mongo command line tool we just installed:
$ mongo MongoDB shell version: 3.0.0 connecting to: test 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
As you can see, we are being connected to the test database by default.
Final Thoughts on MongoDB
Consider also keeping an eye on the official blog to learn about new releases, best practices, and how others are using MongoDB.
We hope you had fun getting to know more about this popular NoSQL database system. It looks like MongoDB is only going to grow in relevance, so it’s definitely a resource worth understanding.