Book Image

MongoDB Cookbook

By : Amol Nayak
Book Image

MongoDB Cookbook

By: Amol Nayak

Overview of this book

<p>MongoDB is a high-performance and feature-rich NoSQL database that forms the backbone of numerous complex development systems. You will certainly find the MongoDB solution you are searching for in this book.</p> <p>Starting with how to initialize the server in three different modes with various configurations, you will then learn a variety of skills including the basics of advanced query operations and features in MongoDB and monitoring and backup using MMS. From there, you can delve into recipes on cloud deployment, integration with Hadoop, and improving developer productivity. By the end of this book, you will have a clear idea about how to design, develop, and deploy MongoDB.</p>
Table of Contents (17 chapters)
MongoDB Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Starting a single node instance using command-line options


In this recipe, we will see how to start a standalone single Node server with some command-line options. We will see an example where we will perform the following tasks:

  • Starting the server that listens to port 27000

  • Writing logs to /logs/mongo.log

  • Setting the database directory to /data/mongo/db

Since the server is started for development purposes, we don't want to preallocate full size database files (we will soon see what this means).

Getting ready

If you have already seen and executed the steps mentioned in the Single node installation of MongoDB recipe, you need not do anything different. If all the prerequisites are met, we are good for this recipe too.

How to do it…

You can start a single node instance using command-line options with the following steps:

  1. The /data/mongo/db directory for the database and /logs/ for the logs should be created and present on your filesystem with appropriate write permissions.

  2. Execute the following command:

    > mongod --port 27000 --dbpath /data/mongo/db --logpath /logs/mongo.log --smallfiles
    

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works…

OK, this wasn't too difficult and is similar to the previous recipe, but we have some additional command-line options this time around. MongoDB actually supports quite a few options at startup, and we will see a list of the ones that are most common and important in my opinion:

Option

Description

--help or -h

This is used to print the information of various startup options available.

--config or -f

This specifies the location of the configuration file that contains all the configuration options. We will learn more about this option in the Single node installation of MongoDB with options from the config file recipe. It is just a convenient way of specifying the configurations in a file rather than in a command prompt, especially when the number of options specified is more. Using a separate configuration file shared across different mongod instances will also ensure that all the instances are running with identical configurations.

--verbose or -v

This makes the logs more verbose. We can put more v's to make the output even more verbose, for example, -vvvvv.

--quiet

This is the quieter output. This is the opposite of verbose or the -v option. It will keep the logs less chatty and clean.

--port

This option is used if you are looking to start the server that listens to a port other than the default 27017. We will frequently use this option whenever we are looking to start multiple Mongo servers on the same machine; for example, --port 27018 will start the server that listens to port 27018 for new connections.

--logpath

This provides a path to a logfile where the logs will be written. The value defaults to STDOUT. For example, --logpath /logs/server.out will use /logs/server.out as the logfile for the server. Remember that the value provided should be a file and not a directory where the logs will be written.

--logappend

This option will append to the existing logfile if any. The default behavior is to rename the existing logfile and then create a new file for the logs of the currently started Mongo instance. Let's assume that we used the name of the logfile as server.out and on startup the file exists. Then, by default, this file will be renamed as server.out.<timestamp>, where <timestamp> is the current time. The time is GMT as against the local time. Suppose the current date is October 28, 2013 and the time is 12:02:15, then the file generated will have the 2013-10-28T12-02-15 value as the timestamp.

--dbpath

This provides the directory where a new database will be created or an existing database is present. The value defaults to /data/db. We will start the server using /data /mongo/db as the database directory. Note that the value should be a directory rather than the name of the file.

--smallfiles

This is used frequently for development purposes when we plan to start more than one Mongo instance on our local machine. On startup, Mongo creates a database file of size 64 MB (on 64-bit machines). This preallocation happens for performance reasons, and the file is created with zeros written to it to fill out the space on the disk. Adding this option on startup creates a preallocated file of 16 MB only (again on a 64-bit machine). This option also reduces the maximum size of the database and journal files. Avoid using this option for production deployments. Also, the database file size doubles to a maximum of 2 GB by default. If the --smallfile option is chosen, it goes up to a maximum of 512 MB.

--replSet

This option is used to start the server as a member of the replica set. The value of this argument is the name of the replica set, for example, --replSet repl1. More information on this option is covered in the Starting multiple instances as part of a replica set recipe, where we will start a simple Mongo replica set.

--configsvr

This option is used to start the server as a config server. The role of the config server will be made clearer when we set up a simple sharded environment in the Starting a simple sharded environment of two shards recipe in this chapter. This, however, will be started and listen to port 27019 by default and the /data/configdb data directory. These can, of course, be overridden using the --port and --dbpath options.

--shardsvr

This informs the started mongod process that this server is being started as a shard server. By giving this option, the server also listens to port 27018 instead of the default 27017. We will learn more about this option when we start a simple sharded server.

--oplogSize

Oplog is the backbone of replication. It is a capped collection where the data being written to the primary is stored to be replicated to the secondary instances. This collection resides in a database named local. On initialization of a replica set, the disk space for the oplog is preallocated, and the database file (for the local database) is filled with zeros as placeholders. The default value is 5 percent of the disk space, which should be good enough in most cases. The size of the oplog is crucial, because capped collections are of a fixed size, and they discard the oldest documents in them upon exceeding their size-making space for new documents; if the oplog size is too small, it can result in the data being discarded before being replicated to secondary nodes. A large oplog size can result in unnecessary disk-space utilization and a longer time for the replica set initialization. For development purposes, when we start multiple server processes on the same host, we might want to keep the oplog size to a minimum value so that it quickly initiates the replica set and uses the minimum disk space possible.

There's more…

For an exhaustive list of the options available, use the --help or -h option. The preceding list of options is not exhaustive, and we will see some more coming up in the upcoming recipes as and when we need them. In the next recipe, we will see how to use a config file instead of the command-line arguments.

See also

  • The Single node installation of MongoDB with options from the config file recipe to use config files to provide startup options

  • To start a replica set, refer to the Starting multiple instances as part of a replica set recipe

  • To set up a sharded environment, refer to the Starting a simple sharded environment of two shards recipe