Overview
MongoDB is one of the most popular open source NoSQL database solutions available. Unlike relational databases that store rows of data, document-oriented databases like this one store data as, you guessed it, documents. These types of databases are not constricted by rigid schemas. They also scale much easier and more efficiently than relational databases, meaning it is a lot easier to store once unthinkable amounts of application data.
MongoDB is able to achieve and maintain high performance – much greater than any relational database, such as Microsoft SQL – even while storing petabytes of data by offloading a lot of logic to your application. A relational database it is not, but this tradeoff gives our developers the flexibility they need to work with data in today’s high volume, big data world.
The tutorial will guide you through the installation through to managing your databases.
Installing MongoDB
- Import the public key used by the MongoDB package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
- Create an aptitude source file for MongoDB, which is used to query MongoDB’s package management system.
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
- Refresh the local package database.
sudo apt-get update
- Install the latest stable version of MongoDB.
sudo apt-get install -y mongodb-org
Starting and Stopping MongoDB
By default on Ubuntu, MongoDB will automatically be started after installation. However, there will come a time when you’re going to have to either start or stop the database server.
- Starting MongoDB
service mongod start
- Stopping MongoDB
service mongod stop
- Restarting MongoDB
service mongod restart
Creating A Database
Databases can be created on-the-fly by our developers through their application. However, for security reasons, it may be best to create them beforehand, so that we may add some restrictions.
- Log into the MongoDB console
mongo
- As you probably just realized, we can access the server without any credentials. And even lacking this, we are able to create and manage databases. We’ll address security a little later in this tutorial.
- Using the use command, create your database. Use, you ask. But I haven’t created the database yet! If the database does not exist, the use command will create it.
use mydatabase
Creating User Accounts
In order for us to begin securing our MongoDB server, we’ll need accounts. Some accounts will be for our applications to store and retrieve data, and others will be for operational work on the server.
Creating a Root Admin Account
You likely want at least one account that has full privileges to Mongo and all created databases.
- Access the Mongo console.
- Set the database to Admin
use admin
- Run the following command, replacing user and password with your own. Ideally, this account should NOT be named root.
db.createUser( { user:"superuser", pwd:"super-secret-password", roles:["root"] } )
Creating your Application Service Account
Every application should have its own account, and each account should limited privileges – allowing on that which must be done for your application to run. Your application shouldn’t be able to create user accounts unless your application’s purpose is to be management front-end for MongoDB.
- If not already, log into the Mongo console.
- Select the application database you will be using. If it doesn’t already exist, it will automatically be created.
use myapp
- Create the service account for your application, changing the user and pwd values for your environment.
db.createUser( { user:"app1user", pwd:"secret-password', roles: [ { role: "readWrite", db:"myapp"} ] } )