Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MEAN Web Development

You're reading from   MEAN Web Development Master real-time MEAN web application development and learn how to construct a MEAN application using a combination of MongoDB, Express, AngularJS, and Node.js

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher Packt
ISBN-13 9781783983285
Length 354 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Amos Q. Haviv Amos Q. Haviv
Author Profile Icon Amos Q. Haviv
Amos Q. Haviv
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to MEAN 2. Getting Started with Node.js FREE CHAPTER 3. Building an Express Web Application 4. Introduction to MongoDB 5. Introduction to Mongoose 6. Managing User Authentication Using Passport 7. Introduction to AngularJS 8. Creating a MEAN CRUD Module 9. Adding Real-time Functionality Using Socket.io 10. Testing MEAN Applications 11. Automating and Debugging MEAN Applications Index

Installing MongoDB

For MongoDB's stable versions, the official MongoDB website supplies linked binaries that provide the easiest way to install MongoDB on Linux, Mac OS X, and Windows. Notice that you need to download the right architecture version for your operating system. If you use Windows or Linux, make sure to download either the 32-bit or 64-bit version according to your system architecture. Mac users are safe to download the 64-bit version.

Note

The MongoDB versioning scheme works in such a way that only even version numbers mark stable releases, and so versions 2.2.x and 2.4.x are stable, while 2.1.x and 2.3.x are unstable releases and should not be used in production. The latest stable version of MongoDB is 2.6.x.

When you visit the download page at http://mongodb.org/downloads, you'll be offered a download of an archive containing the binaries you need to install MongoDB. After downloading and extracting the archive file, you will need to locate the mongod binary, which is usually located in the bin folder. The mongod process runs the main MongoDB server process, which can be used as a standalone server or a single node of a MongoDB replica set. In our case, we will use MongoDB as a standalone server. The mongod process requires a folder to store the database files in (the default folder is /data/db) and a port to listen to (the default port is 27017). In the following subsections, we'll go over the setup steps for each operating system; we'll begin with the common Windows installation process.

Note

It is recommended that you learn more about MongoDB by visiting the official documentation at https://mongodb.org.

Installing MongoDB on Windows

Once you have downloaded the right version, unpack the archive file, and move the folder to C:\mongodb. MongoDB uses a default folder to store its files. On Windows, the default location is C:\data\db, so in the command prompt, go to C:\ and issue the following command:

> md data\db

Tip

You can tell the mongod service to use an alternative path for the data files using the --dbpath command-line flag.

Once you've moved the MongoDB files to the right folder and finished creating the data folders, you'll get two options while running the main MongoDB service.

Running MongoDB manually

To run MongoDB manually, you will need to run the mongod binary. So, open the command prompt and issue the following command:

> C:\mongodb\bin\mongod.exe

The preceding command will run the main MongoDB service that starts listening to the default 27017 port. If everything goes well, you should see a console output similar to the following screenshot.

Running MongoDB manually

Running the MongoDB server on Windows

Depending on the Windows security level, a security alert dialog, which notifies you about the blocking of some service features, will be issued. If this occurs, select a private network and click on Allow Access.

Note

You should be aware that the MongoDB service is self-contained, so you can alternatively run it from any folder of your choice.

Running MongoDB as a Windows Service

The more popular approach is running MongoDB automatically after every reboot cycle. Before you begin setting up MongoDB as a Windows Service, it's considered good practice to specify a path for the MongoDB log and configuration files. Start by creating a folder for these files by running the following command in your command prompt:

> md C:\mongodb\log

Then, you'll be able to create a configuration file using the --logpath command-line flag, so in the command prompt, issue the following command:

> echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg

When you have your configuration file in place, open a new command prompt window with administrative privileges by right-clicking on the command prompt icon and clicking on Run as administrator. In the new command prompt window, install the MongoDB service by running the following command:

> sc.exe create MongoDB binPath= "\"C:\mongodb\bin\mongod.exe\" --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB 2.6" start= "auto"

If the service was successfully created, you will get the following log message:

[SC] CreateService SUCCESS

Notice that the install process will only succeed if your configuration file is set correctly and contains the logpath option. After installing your MongoDB service, you can run it by executing the following command in the administrative command prompt window:

> net start MongoDB

Tip

Downloading the example code

You can download the example code files for all the 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 to you.

Be aware that the MongoDB configuration file can be modified to accommodate your needs. You can learn more about it by visiting http://docs.mongodb.org/manual/reference/configuration-options/.

Installing MongoDB on Mac OS X and Linux

In this section, you'll learn the different ways of installing MongoDB on Unix-based operating systems. Let's begin with the simplest way to install MongoDB, which involves downloading MongoDB's precompiled binaries.

Installing MongoDB from binaries

You can download the right version of MongoDB using the download page at http://www.mongodb.org/downloads. Alternatively, you can do this via CURL by executing the following command:

$ curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.6.4.tgz

Notice that we have downloaded the Mac OS X 64-bit version, so make sure you alter the command to fit the version suitable for your machine. After the downloading process is over, unpack the file by issuing the following command in your command-line tool:

$ tar -zxvf mongodb-osx-x86_64-2.6.4.tgz

Now, change the name of the extracted folder to a simpler folder name by running the following command:

$ mv mongodb-osx-x86_64-2.6.4 mongodb

MongoDB uses a default folder to store its files. On Linux and Mac OS X, the default location is /data/db, so in your command-line tool run the following command:

$ mkdir -p /data/db

Note

You may experience some troubles creating this folder. This is usually a permission issue, so use sudo or super user when running the preceding command.

The preceding command will create the data and db folders because the –p flag creates parent folders as well. Notice that the default folder is located outside of your home folder, so do make sure you set the folder permission by running the following command:

$ chown -R $USER /data/db

Now that you have everything prepared, use your command-line tool and go to the bin folder to run the mongod service as follows:

$ cd mongodb/bin
$ mongod

This will run the main MongoDB service, which will start listening to the default 27017 port. If everything goes well, you should see a console output similar to the following screenshot:

Installing MongoDB from binaries

Running the MongoDB server on Mac OS X

Install MongoDB using a package manager

Sometimes the easiest way to install MongoDB is by using a package manager. The downside is that some package managers are falling behind in supporting the latest version. Luckily, the team behind MongoDB also maintains the official packages for RedHat, Debian, and Ubuntu, as well as a Hombrew package for Mac OS X. Note that you'll have to configure your package manager repository to include the MongoDB servers to download the official packages.

To install MongoDB on Red Hat Enterprise, CentOS, or Fedora using Yum, follow the instructions at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/.

To install MongoDB on Ubuntu using APT, follow the instructions at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/.

To install MongoDB on Debian using APT, follow the instructions at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/.

To install MongoDB on Mac OS X using Homebrew, follow the instructions at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/.

Using the MongoDB shell

MongoDB archive file includes the MongoDB shell, which allows to you to interact with your server instance using the command line. To start the shell, navigate to the MongoDB bin folder and run the mongo service as follows:

$ cd mongodb/bin
$ mongo

If you successfully installed MongoDB, the shell will automatically connect to your local instance, using the test database. You should see a console output similar to the following screenshot:

Using the MongoDB shell

Running the MongoDB shell on Mac OS X

To test your database, run the following command:

> db.articles.insert({title: "Hello World"})

The preceding command will create a new article collection and insert a JSON object containing a title property. To retrieve the article object, execute the following command:

> db.articles.find()

The console will output the following message:

{ _id : ObjectId("52d02240e4b01d67d71ad577"), title: "Hello World " }

Congratulations! This means your MongoDB instance is working properly and you have successfully managed to interact with it using the MongoDB shell. In the upcoming chapters, you'll learn more about MongoDB and how to use the MongoDB shell.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime