在OS X上安装

Synopsis

This tutorial outlines the basic installation process for deploying MongoDB on Macintosh OS X systems. This tutorial provides two main methods of installing the MongoDB server (i.e. “mongod”) and associated tools: first using the community package management tools, and second using builds of MongoDB provided by 10gen.

也可以参考

The documentation of following related processes and concepts.

Other installation tutorials:

Installing with Package Management

Both community package management tools: Homebrew and MacPorts require some initial setup and configuration. This configuration is beyond the scope of this document. You only need to use one of these tools.

If you want to use package management, and do not already have a system installed, Homebrew is typically easier and simpler to use.

Homebrew

Homebrew installs binary packages based on published “formula.” Issue the following command at the system shell to update the brew package manager:

brew update

Use the following command to install the MongoDB package into your Homebrew system.

brew install mongodb

Later, if you need to upgrade MongoDB, you can issue the following sequence of commands to update the MongoDB installation on your system:

brew update
brew upgrade mongodb

MacPorts

MacPorts distributes build scripts that allow you to easily build packages and their dependencies on your own system. The compilation process can take significant period of time depending on your system’s capabilities and existing dependencies. Issue the following command in the system shell:

port install mongodb

Using MongoDB from Homebrew and MacPorts

The packages installed with Homebrew and MacPorts contain no control scripts or interaction with the system’s process manager.

If you have configured Homebrew and MacPorts correctly, including setting your PATH, the MongoDB applications and utilities will be accessible from the system shell. Start the mongod process in a terminal (for testing or development) or using a process management tool.

mongod

Then open the mongo shell by issuing the following command at the system prompt:

mongo

This will connect to the database running on the localhost interface by default. At the mongo prompt, issue the following two commands to insert a record in the “test” collection of the (default) “test” database and then retrieve that record.

> db.test.save( { a: 1 } )
> db.test.find()

也可以参考

mongo” and “mongo Shell JavaScript 快参

Installing from 10gen Builds

10gen provides compiled binaries of all MongoDB software compiled for OS X, which may provide a more straightforward installation process.

Download MongoDB

In a terminal session, begin by downloading the latest release. Use the following command at the system prompt:

curl http://downloads.mongodb.org/linux/mongodb-osx-x86_64-x.y.z.tgz > mongo.tgz

注解

The mongod process will not run on older Macintosh computers with PowerPC (i.e. non-Intel) processors.

Once you’ve downloaded the release, issue the following command to extract the files from the archive:

tar -zxvf mongo.tgz

Optional

You may use the following command to move the extracted folder into a more generic location.

mv -n mongodb-osx-[platform]-[version]/ /path/to/new/location/

Replace [platform] with i386 or x86_64 depending on your system and the version you downloaded, and [version] with 2.2.3 or the version of MongoDB that you are installing.

You can find the mongod binary, and the binaries all of the associated MongoDB utilities, in the bin/ directory within the archive.

Using MongoDB from 10gen Builds

Before you start mongod for the first time, you will need to create the data directory. By default, mongod writes data to the /data/db/ directory. To create this directory, and set the appropriate permissions use the following commands:

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

You can specify an alternate path for data files using the --dbpath option to mongod.

The 10gen builds of MongoDB contain no control scripts or method to control the mongod process. You may wish to create control scripts, modify your path, and/or create symbolic links to the MongoDB programs in your /usr/local/bin directory for easier use.

For testing purposes, you can start a mongod directly in the terminal without creating a control script:

mongod --config /etc/mongod.conf

注解

This command assumes that the mongod binary is accessible via your system’s search path, and that you have created a default configuration file located at /etc/mongod.conf.

Among the tools included with this MongoDB distribution, is the mongo shell. You can use this shell to connect to your MongoDB instance by issuing the following command at the system prompt from inside of the directory where you extracted mongo:

./bin/mongo

注解

The ./bin/mongo command assumes that the mongo binary is in the bin/ sub-directory of the current directory. This is the directory into which you extracted the .tgz file.

This will connect to the database running on the localhost interface by default. At the mongo prompt, issue the following two commands to insert a record in the “test” collection of the (default) “test” database and then retrieve that record:

> db.test.save( { a: 1 } )
> db.test.find()

也可以参考

mongo” and “mongo Shell JavaScript 快参