Book Image

PHP and MongoDB Web Development Beginner's Guide

Book Image

PHP and MongoDB Web Development Beginner's Guide

Overview of this book

With the rise of Web 2.0, the need for a highly scalable database, capable of storing diverse user-generated content is increasing. MongoDB, an open-source, non-relational database has stepped up to meet this demand and is being used in some of the most popular websites in the world. MongoDB is one of the NoSQL databases which is gaining popularity for developing PHP Web 2.0 applications.PHP and MongoDB Web Development Beginner’s Guide is a fast-paced, hands-on guide to get started with web application development using PHP and MongoDB. The book follows a “Code first, explain later” approach, using practical examples in PHP to demonstrate unique features of MongoDB. It does not overwhelm you with information (or starve you of it), but gives you enough to get a solid practical grasp on the concepts.The book starts by introducing the underlying concepts of MongoDB. Each chapter contains practical examples in PHP that teache specific features of the database.The book teaches you to build a blogging application, handle user sessions and authentication, and perform aggregation with MapReduce. You will learn unique MongoDB features and solve interesting problems like real-time analytics, location-aware web apps etc. You will be guided to use MongoDB alongside MySQL to build a diverse data back-end. With its concise coverage of concepts and numerous practical examples, PHP and MongoDB Web Development Beginner’s Guide is the right choice for the PHP developer to get started with learning MongoDB.
Table of Contents (17 chapters)
PHP and MongoDB Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action - downloading and running MongoDB on Linux


Now, we are going to learn how to download and run the MongoDB server on a Linux box:

  1. 1. Fire up the terminal program. Type in the following command and hit Enter

    wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.3.tgz > mongo.tgz
    
  2. 2. Extract the downloaded archive by using the following command:

    tar xzf mongo.tgz
    
  3. 3. Rename the extracted directory by using the following command:

    mv mongodb-linux-i686-1.8.3 mongodb
    
  4. 4. Create the data directory /data/db by using the following command:

    sudo mkdir p /data/db
    sudo chown `id -u` /data/db
    
  5. 5. Startup the server by running the following command:

    ./mongodb/bin/mongod
    
  6. 6. Open another tab in the terminal and run the next command:

    ./mongodb/bin/mongo
    
  7. 7. Type show dbs into the shell and hit Enter.

What just happened?

In step 1, we downloaded the latest stable release of MongoDB 32-bit version for Linux using the wget program, and stored it as a GZIP tarball named mongo.tgz on your machine.

Note

At the time of this writing, the latest production release for MongoDB is 1.8.3. So when you try this, if a newer production release is available, you should download that version instead.

In steps 2 and 3, we extracted the tarball and renamed the extracted directory to mongodb for convenience. In step 4, we created the data directory /data/db for MongoDB, and gave it permission to read from and write to that directory. In step 5, we startup the MongoDB server by executing the mongodb/bin/mongod script.

In step 6, after we have successfully launched the server, we start the mongo interactive shell:

$./mongodb/bin/mongo
MongoDB shell version: 1.8.1
url: test
connection to test
type "help" for help
>

Once the shell has started, we issue the command show dbs to list all the pre-loaded databases in the server:

>show dbs
local (empty)
admin (empty)
>

The databases listed here are special databases pre-built within the server. They are used for administration and authentication purposes. We do not need to concern ourselves with them right now.

Note

Installing MongoDB using package managers

You can use the package manager of your Linux distribution (apt for Debian/Ubuntu, yum for Fedora/CentOS) to install MongoDB. To get distro-specific instructions, Ubuntu/Debian users should visit http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages. Users of CentOS and Fedora should visit http://www.mongodb.org/display/DOCS/CentOS+and+Fedora+Packages. The advantage of using a package manager, other than being able to install with fewer commands, is that you can launch the Mongo server and the client just by typing mongod and mongo respectively in the shell.

Installing MongoDB on OS X

The instructions for installing MongoDB on an OS X powered Mac machine are the same as those for Linux. You have to download the OS X specific binaries for Mongo (available at http://www.mongodb.org/downloads), and follow the same steps to execute them.

Alternatively, if you have package managers installed on your OS X (Homebrew or MacPorts), you can use them to install MongoDB.

To install MongoDB with HomeBrew use the following command:

$ brew update
$ brew install mongodb

To use MacPorts to install MongoDB use the following command:

$ sudo port install mongodb

Configuring MongoDB

When we launched the mongod program, it booted up with some default configuration settings, such as the path to the data directory (C:\data\db on Windows or /data/db on Unix). In real world deployments, we want to be able to specify these settings ourselves. There are two ways to achieve that. We can either modify them by supplying command-line parameters to the mongod program at invocation, or by using file-based configurations.

Command-line parameters

We can override the default MongoDB settings by passing command-line parameters to the mongod program. For example, the next command tells MongoDB to use C:\mongodb_data as data directory by sending it as a --dbpath argument:

C:\>mongodb\bin\mongod --dbpath C:\mongodb_data

The following table lists some useful command-line parameters and their functions:

Parameter

What it does

--dbpath

Path to the directory for storing data files.

--bind_ip

IP address that the mongod server will listen on, default is 127.0.0.1.

--port

Port address that mongod will listen on, default is 27017.

--logpath

Full file path to the log file where the MongoDB messages will be written. By default all messages are written to standard output.

--logappend

Setting this option to true appends the messages at the end of the log file. Setting it to false overwrites the log.

We can see the full list of command-line options by running mongod with the--help option:

C:\>mongodb\bin\mongod -help

File-based configuration

An alternative to sending all those command-line parameters to mongod manually is to put the required configuration settings in a file and then pass the path of the file as a--config option. For example, consider the following sample configuration file:

dbpath = D:\mongodb_data
logpath = D:\mongodb.log
logappend = true

We store this file to a location, say C:\mongodb.conf. Now, to start MongoDB with the these settings, we have to enter the next command in the CMD prompt:

C:\>mongodb\bin\mongod --config C:\mongodb.conf

mongod will be loaded with these configuration settings. Note that file-based parameters are the same as those for command-line options.

Note

If you are on a Linux machine, and you have installed Mongo using a package manager, such a configuration file may already exist in your system, typically at the location /etc/mongo.conf. You can modify that file to boot Mongo server with the configuration of your choice.

Have a go hero configure MongoDB to run with non-default settings

Start MongoDB with the following settings, using a file-based configuration:

  • Default data directory at /usr/bin/mongo.

  • Default port address at 8888.

  • Messages will be logged at /var/logs/mongodb.log. The log file should be overwritten over time.

Stopping MongoDB

There are several ways you can shutdown a running MongoDB server.

Hitting Control + C

In the terminal window (or CMD prompt window in case you are on Windows) running the mongod process, hit Ctrl + C. This will signal the server to do a clean shutdown, flush, and close its data files.

From the mongo shell

From the mongo interactive shell, you can issue a shutdownServer() command, causing mongod to terminate:

>use admin
switched to db admin
>db.shutdownServer()

Sending INT or TERM signal in UNIX

In Linux/OS X, you can send a kill -2 <PID> signal to the process running mongod, which will cause the server to shutdown cleanly. You can get the PID by running the following command:

ps aef | grep mongod