Linode Library Home
Categories
Getting Started
Beginner's Guide
Using Linux
Linode Manager
Networking
LAMP Guides
LEMP Guides
Web Servers
Web Applications
Email Guides
High Availability
SSL Guides
Databases
MySQL
PostgreSQL
Oracle 10g XE
MongoDB
CouchDB
Redis
Server Monitoring
Development
Communications
Advanced
Troubleshooting
How to Contribute
Sitemap
Linode Library RSS Feed
Linode Library Home :: Database Management Systems :: The MongoDB Database System :: Use MongoDB to Store Application Data on Ubuntu 10.04 (Lucid)
Print View Download PDF Download RST

Use MongoDB to Store Application Data on Ubuntu 10.04 (Lucid)

Author: Sam KleinmanExternal Link
Published: May 3, 2010

MongoDB is a database engine that provides access to non-relational key-value databases. It is part of the growing NoSQL movement, which 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-based output format and specialized language specific bindings that make it particularly attractive for use in custom application development. Although MongoDB is a relatively new project and has not yet been packaged by most major operating system distributions, the software has been used in a number of large scale production deploymentsExternal Link such as "GitHub", "SourceForge", and "DISQUS".

Before installing MongoDB, it is assume that you have followed our getting started guide. If you're new to Linux server administration, you may be interested in our using Linux document series including the beginner's guide and administration basics guide.

These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a Linux VPS and get started today.

Installing MongoDB Link

Download the latest binaries from the upstream sourceExternal Link.; at the time of writing this is version 1.6.0. When downloading MongoDB, ensure that you download a version that is compiled for the proper system architecture. This document uses the 32-bit version; however, these steps will work for the 64-bit edition with a few modifications. Please note that the 32-bit version of MongoDB has a database size limit of 2 gigabytes. If you expect to store more than 2 gigabytes of data, ensure that you have deployed a 64-bit system and are using the 64-bit version of MongoDB.

cd /opt/
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.0.tgz
tar zxvf mongodb-linux-i686-1.6.0.tgz
mv mongodb-linux-i686-1.6.0 /opt/mongodb

MongoDB is now deployed in the /opt/mongodb/ folder with the binaries located in the /opt/mongodb/bin/ folder. In this example, it is assumed that all database files will be stored in the /srv/db/mongodb folder and that log files will be stored in the /srv/db/mongodb.log file. Issue the following commands to create this file and directory:

mkdir -p /srv/db/mongodb
touch /srv/db/mongodb.log

In this example the name of the database will be mongodb; however, you can modify this name with another name or naming scheme as needed.

Monitor for Software Updates and Security Notices Link

When running software compiled or installed directly from sources provided by upstream developers, you are responsible for monitoring updates, bug fixes, and security issues. After becoming aware of releases and potential issues, update your software to resolve flaws and prevent possible system compromise. Monitoring releases and maintaining up to date versions of all software is crucial for the security and integrity of a system.

Please monitor the following MongoDB mailing lists for updates to ensure that you are aware of all updates to the software and can upgrade appropriately or apply patches and recompile as needed:

When upstream sources offer new releases, repeat the instructions for installing MongoDB and recompile your software when needed.

Create Basic Control Scripts Link

In typical installations, the MongoDB server process is controlled using command line arguments to binaries located at /opt/mongodb/bin/mongod. In order to simplify commands, you can create control scripts named mongodb-start and mongodb-stop in the /opt/bin/ directory. Issue the following commands to create the required directories and create the control scripts:

mkdir /opt/bin/
mkdir /opt/config/

File: /opt/bin/mongodb-stop

#!/bin/bash

pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
if [ "${pid}" != "" ]; then
    kill -2 ${pid};
fi

File: /opt/bin/mongodb-start

#!/bin/sh

/opt/mongodb/bin/mongod --config /opt/config/mongodb \
## --upgrade \ ##runs a database upgrade option if needed \

Set the following configuration directives in the /opt/config/mongodb file:

File: /opt/config/mongodb

# Configuration Options for MongoDB
#
# For More Information, Consider:
# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters
# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration

dbpath = /srv/db/mongodb
logpath = /srv/db/mongodb.log
logappend = true

bind_ip = 127.0.0.1
port = 27017
fork = true

auth = true
# noauth = true

The /opt/bin/mongodb-start script loads mongod with the options specified in the /opt/config/mongodb file. The commented lines specify maintenance and upgrade options that you may need to run occasionally, but these are not required for general use. The --repair and --update flags perform a check against the data store or upgrade the data storage system when you upgrade the database engine.

The /opt/config/mongodb file specifies a number of important options that you may modify to control the functionality of mongodb. The dbpath option indicates that database files will be stored in /srv/db/mongo. The logpath directive indicates that MongoDB's logs will be located in the /srv/db/log/mongodb.log file, and that new log entries will be appended to the end of the log rather than overwriting existing log entries even after MongoDB restarts.

If the bind_ip option is not specified, MongoDB will bind to and listen for requests on all local IP addresses. In this case, MongoDB is configured to only listen for requests on the localhost interface. Modify this option to allow your MongoDB server to listen for requests on other IP addresses; however, consider the possible security implications of providing public access. The port option in this file specifies the default port, but can be modified depending on your needs.

Setting the fork option to equal true configures MongoDB to run as a daemon process in the background independently of the current user's session. Please note that MongoDB will run under the user that executes the mongodb-start script. We strongly recommend that this user not be root or another privileged user account. To provide additional security, it is recommended that you set the auth option to true in order to take advantage of MongoDB's internal authentication capabilities. If you need to test the database without authentication, you can replace the auth option to noauth.

Modify /opt/bin/mongodb-start and /opt/config/mongodb as needed to suit the needs of your deployment. Note that all sections preceded by a hash symbol (#) are comments and are not interpreted by bash.

Issue the following two commands to set these files as executable:

chmod +x /opt/bin/mongodb-start
chmod +x /opt/bin/mongodb-stop

From now on, issuing /opt/bin/mongodb-start or /opt/bin/mongodb-stop will start or stop the MongoDB process, respectively.

Using a Basic Init Script Link

We've also created a very basic "init script" as a wrapper around the mongodb-start and mongo-stop scripts described above. You will still need to modify and manage the configuration of your MongoDB server in the files above. This script only provides a means for ensuring that MongoDB will start at boot. Issue the following commands:

wget http://library.linode.com/databases/mongodb/reference/init-deb.sh
mv init-deb.sh /etc/init.d/mongodb
chmod +x /etc/init.d/mongodb
/usr/sbin/update-rc.d -f mongodb defaults

You will also need to create a user and group for mongodb; issue the following command:

adduser --system --no-create-home --disabled-login --disabled-password --group mongodb

Now issue the following command to ensure that the MongoDB user you just created will have access to all required files in the /srv/db/ hierarchy:

chown mongodb:mongodb -R /srv/db/

To start and stop MongoDB using the init script, issue the appropriate command from the following:

/etc/init.d/mongodb start
/etc/init.d/mongodb stop

Congratulations, you now have a fully functional installation of the MongoDB system.

Additional MongoDB Functionality Link

Now that MongoDB is running properly, you can begin to explore some of its features. Most interaction with MongoDB is done via the rich set of language-specific driversExternal Link. There are also a number of tools in the /opt/mongodb/bin/ directory that you might find useful for interacting with MongoDB databases. The mongo utility provides an interactive JavaScript shell for MongoDB including commands such as mongodump and mongorestore for creating and restoring backups and snapshots as well mongoexport and mongoimportjson for exporting individual collections in JSON format.

You can now fully enjoy application development with MongoDB!

More Information Link

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

License Link

This guide is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States LicenseExternal Link. Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.

Comments

Comment poster gravatar. Submitted by John Wyles on Tuesday, June 1 2010 at 06:26:58 GMT

Rather than following these instructions I recommend using the debian packages and instructions that 10gen have posted:

* http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages

Comment poster gravatar. Submitted by Mohammed Firdaus on Monday, July 12 2010 at 03:15:01 GMT

You might want to mention that MongoDB does not support single server durability which is why they recommend running in a three node configuration.

Comment poster gravatar. Submitted by Stan Schwertly on Wednesday, July 14 2010 at 12:12:20 GMT

Thanks for the tip, Mohammed!

Got a comment?

BBCode formatting is allowed. Email addresses are confidential, and are only used for gravatars and sending document/comment updates if requested. Please refer to our privacy policy. All comments are moderated and may take some time to appear on this page.