官术网_书友最值得收藏!

Installing MongoDB

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

Once there, be sure to download the correct version depending on your OS and CPU (32 or 64 bit). For Windows users, you can opt to download the MSI installer file, which will make the installation much simpler.

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.

Mac OS X installation instructions

If you are using the Homebrew package manager, MongoDB can be installed using the following two commands:

$ brew update
$ brew install mongoDB

The remainder of this chapter assumes you are not using Homebrew and need to install MongoDB manually. If you are installing MongoDB via Homebrew, you can proceed directly to the testing MongoDB installation section.

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

$ mkdir -p /mongodb
$ cd ~/Downloads
$ cp -R -n mongodb-osx-x86_64-2.4.9/ mongodb

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

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 command:

$ 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 lines of code. The important thing is that you append :/mongodb/bin before the $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).

Note

Here, mongod refers to the MongoDB server instance that you need to invoke and mongo refers to the Mongo shell, which will be the console through which you will be interacting with the database.

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:

$ mkdir -p /data/db
$ chown `id -u` /data/db

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

$ mongod

This should dump a bunch of log statements while the server starts up, but ultimately ends with:

Sun Mar 16 12:26:58.885 [initandlisten] waiting for connections onport 27017

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

Windows 7 / Windows 8 installation instructions

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

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

Extract the ZIP file that was downloaded to the root c:\ destination. By default, this should extract a folder named mongodb-osx-x86_64-2.4.9. Depending on the tool you are using for extraction, you can leave this as is or change the destination folder to simply mongodb. If you don't change the destination during extraction, you should rename the folder once complete. Either way, be sure that the files that are extracted reside in a folder named c:\mongodb.

Next, you need to create a default data folder that MongoDB will use to store all data documents. Using Windows Explorer or Command Prompt, whichever you are most comfortable with, create the folder c:\data and then c:\data\db by using the following command:

$ md data
$ md data\db

Once the files have been properly extracted to the c:\mongodb folder and both the data folders subsequently created, you can now start the MongoDB database server by executing the following command from a prompt:

$ c:\mongodb\bin\mongod.exe

This should dump a bunch of log statements while the server starts up, but will ultimately end with:

Sun Mar 16 16:58:05.182 [initandlisten] waiting for connections on port 27017

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

Linux installation instructions

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

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 the terminal to be sure. This gets the MongoDB daemon up and running, listening for connections:

$ 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 Ctrl-C to cancel and shut down 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 an 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 the 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 a 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
Mongod shell version 2.4.8

Assuming that your MongoDB installation was successful, you should see the version number that was installed as an 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 earlier.

Bookmark the online documentation

You'll want to be sure to point your browser to the online documentation for MongoDB available at http://docs.mongodb.org/manual/ and bookmark it as it will undoubtedly become a resource that you will want to access on a regular basis.

主站蜘蛛池模板: 福清市| 军事| 息烽县| 苗栗市| 渝中区| 台南市| 新宁县| 布拖县| 宝应县| 蒲江县| 若羌县| 林州市| 兴义市| 新竹市| 兰坪| 长子县| 池州市| 望城县| 宁远县| 黎平县| 林西县| 凭祥市| 江北区| 江永县| 鲁山县| 孝昌县| 尖扎县| 阿图什市| 宜宾县| 竹山县| 衢州市| 南和县| 镇巴县| 南和县| 海口市| 元阳县| 屯昌县| 河津市| 陆丰市| 江都市| 沐川县|