Installing the MongoDB server

MongoDB can also be easily downloaded by visiting the official MongoDB website and accessing the Downloads section at http://www.mongodb.org/downloads.

Note

Depending on the version of MongoDB you downloaded, you will want to replace <version> in the following sections with the appropriate version number that matches your file.

Once there, be sure to download the correct version depending on your OS and CPU (32 or 64 bit). You should have determined this in the previous steps when you downloaded and installed Node. For Windows users, you can opt to download the MSI installer file, which will make installation much simpler.

Mac OS X installation instructions

After completing the download, open and extract the contents of the .tgz file. You will want to move the extracted contents to a /mongodb destination folder. You can do this either via the Finder or the command line, whichever you prefer.

Note

Alternatively, MongoDB can be very easily installed using Homebrew. Homebrew is referred to as the missing package manager for OS X. If you don't have Homebrew installed, you can do so by visiting http://brew.sh and following the guide. The remainder of this section of the chapter assumes you are not using Homebrew, but if you do, you can skip most of it by simply executing:

$ brew update
$ brew install mongodb

Then, you can proceed directly to the testing section of this chapter for MongoDB.

The following commands will create a mongodb folder, and copy the contents of the extracted tgz file to that folder:

$ sudo mkdir -p /mongodb
$ cd ~/Downloads
$ cp -R -n mongodb-osx-x86_64-<version>/ /mongodb

You will want to ensure that the location of the MongoDB binaries is configured in your environment PATH so that you can execute mongod and mongo from any working directory. To do this, edit the .bash_profile file in your home folder (~/) and append the location for MongoDB to it. Your .bash_profile file should look something like the following code:

export PATH=~/bin:/some/of/my/stuff:/more/stuff:/mongodb/bin:$PATH

If you don't have this line or are missing .bash_profile completely, you can create one easily by executing the following line:

$ touch .bash_profile
$ [edit] .bash_profile
export PATH=$PATH:/mongodb/bin

You will more than likely have a lot more than what I have in the preceding line. The important thing is that you append:/mongodb/bin: before $PATH at the end. The : is a delimiter between different paths (so it's likely that you will be adding your path to the end of an existing list but before the trailing $PATH).

Next, you need to create a default data folder that MongoDB will use to store all data documents. From the command line, execute the following:

$ sudo mkdir -p /data/db
$ chown 'id -u' /data/db

Once the files have been properly extracted to the /mongodb folder and the two data folders created, you can start the MongoDB database server by executing the following command from the command line:

$ mongod
...
Sun Mar 16 12:26:58.885 [initandlisten] waiting for connections on port 27017

This will dump a bunch of log statements while the server starts up, but ultimately it ends with a line that says it's waiting for connections on port 27017.

That's it! Your MongoDB server is up and running. You can type in Ctrl-C to cancel and shut down the server.

Note

It's important to note that because you are performing local development on your development machine and not a production server, you don't require the MongoDB server to be always up and running. This would be unnecessary strain on your machine during the majority of the time you're not developing against the server. Because of this, throughout the remainder of this book, you will always be required to manually launch the server every time you launch code that expects to connect to a MongoDB server. If you want, you can certainly configure MongoDB to run locally as a service and be "always up", but instructions to do so are beyond the scope of this chapter.

Windows 7 or 8 installation instructions

After completing the download, the MongoDB website will automatically redirect you to a landing page with a link to the Windows Quick Start guide: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/.

It is highly recommended that you follow this guide as it will be the most up to date and will generally be more detailed than what I can provide here.

Windows 7 or 8 installation instructions

During the installation, MongoDB will be installed to C:Program FilesMongoDB 2.6 Standard by default—feel free to change this location to c:mongodb as that will make it easier to launch MongoDB from the command line in future.

Next, you need to create a default data folder that MongoDB will use to store all data documents. Using Windows Explorer or the command prompt, whichever you are most comfortable with, create the C:data folder and then C:datadb:

$ md data
$ md datadb

You can now start the MongoDB database server by executing the following command from a prompt:

$ c:mongodbinmongod.exe...
Sun Mar 16 16:58:05.182 [initandlisten] waiting for connections on port 27017

Tip

As you will manually be executing this command quite a lot throughout the remainder of this book (as well as the remainder of your MongoDB development in general), it will be easier to configure this path in your environment variables so that you can simply execute mongod without requiring the full path. You can learn more about this by visiting http://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/.

This should dump a bunch of log statements while the server starts up, but ultimately it should end with waiting for connections on port 27017.

That's it! Your MongoDB server is up and running. You can type in Ctrl-C to cancel and shutdown the server.

Note

It's important to note that as you are performing local development on your development machine and not a production server, you don't need the MongoDB server to always be up and running. This will be unnecessary strain on your machine for the majority of the time you're not developing against the server. Because of this, throughout the remainder of this book, it will always be a requirement that you manually launch the server every time you launch code that expects to connect to a MongoDB server. If you want, you can certainly configure MongoDB to run locally as a service and be "always up", but instructions to do so are beyond the scope of this chapter.

Linux installation instructions

Once again we are faced with a slightly more challenging installation process with Linux versus Windows or Mac. The official website has great instructions on how to install MongoDB on a number of different Linux distributions: http://docs.mongodb.org/manual/administration/install-on-linux/.

We will continue to use Ubuntu as our flavor of choice, and use the APT package manager for the installation:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
$ sudo apt-get update
$ sudo apt-get install mongodb-10gen

Once these steps are completed, MongoDB should be installed and ready to run on your system. Execute the following command in a terminal to be sure:

$ mongod
Sun Mar 16 12:04:20 [initandlisten] waiting for connections on port 27017

Success! Your MongoDB server is up and running. You can type in Ctrl-C to cancel and shut down the server.

Note

It's important to note that you are performing local development on your development machine and not a production server, you don't need the MongoDB server to be always up and running. This would be unnecessary strain on your machine during the majority of the time you're not developing against the server. Because of this, throughout the remainder of this book, it will always be a required that you manually launch the server every time you launch code that expects to connect to a MongoDB server. If you want, you can certainly configure MongoDB to run locally as a service and be "always up" but instructions to do so are beyond the scope of this chapter.

Confirming successful MongoDB installation

Now that MongoDB has been installed on your system, let's run a quick test to ensure everything is working properly.

Access the command line via your terminal program and execute the following command:

$ mongod --version
db version v2.4.8
Sun Mar 16 14:17:18.280 git version: a123b456c789d012e345f678
$ mongo --version
MongoDB shell version 2.4.8

Assuming your MongoDB installation was successful, you should see the version number that was installed in the output on the screen right below the command you executed.

Note

Note that your version numbers will most likely be more recent than those printed in the preceding example.

Bookmarking the online documentation

Be sure to point your browser to the following online documentation for MongoDB and bookmark it, as it will undoubtedly become a resource that you will want to access on a regular basis:

http://docs.mongodb.org/manual/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.205