Installing MongoDB on OS/X

Install MongoDB

You can install MongoDB with Homebrew or manually. This section describes how to install with Homebrew.

Install MongoDB with Homebrew
Homebrew installs binary packages based on published “formulae.” This section describes how to update brew to the latest packages and install MongoDB. Homebrew requires some initial setup and configuration, which is beyond the scope of this document.

Update Homebrew’s package database.
In a system shell, issue the following command:

brew update

Install MongoDB.
You can install MongoDB with brew with several different options. Use one of the following operations:

Install the MongoDB Binaries
To install the MongoDB binaries, issue the following command in a system shell:

brew install mongodb

Build MongoDB from Source with SSL Support
To build MongoDB from the source files and include SSL support, issue the following from a system shell:

brew install mongodb --with-openssl

Install the Latest Development Release of MongoDB
To install the latest development release for use in testing and development, issue the following command in a system shell:

brew install mongodb --devel

Make sure that the OS X user that will run the server has write access to /data/db.

Now all you need to do is start the mongod server.

tzetterl$ mongod &

The ampersand after the command above will detach MongoDB from the terminal. The output from the command above should be something like this:

tzetterl$ mongod --help for help and startup options
Sat Oct 11 13:09:54.109 [initandlisten] MongoDB starting : pid=88491 port=27017 dbpath=/data/db/ 64-bit host=C02N519FG5RK
Sat Oct 11 13:09:54.110 [initandlisten] db version v2.4.8
Sat Oct 11 13:09:54.110 [initandlisten] git version: a350fc38922fbda2cec8d5dd842237b904eafc14
Sat Oct 11 13:09:54.110 [initandlisten] build info: Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Sat Oct 11 13:09:54.110 [initandlisten] allocator: system
Sat Oct 11 13:09:54.110 [initandlisten] options: {}
Sat Oct 11 13:09:54.119 [initandlisten] journal dir=/data/db/journal
Sat Oct 11 13:09:54.119 [initandlisten] recover begin
Sat Oct 11 13:09:54.120 [initandlisten] recover lsn: 275971
Sat Oct 11 13:09:54.120 [initandlisten] recover /data/db/journal/j._0
Sat Oct 11 13:09:54.121 [initandlisten] recover skipping application of section seq:0 < lsn:275971
Sat Oct 11 13:09:54.121 [initandlisten] recover skipping application of section seq:165721 < lsn:275971
Sat Oct 11 13:09:54.121 [initandlisten] recover skipping application of section seq:220831 < lsn:275971
Sat Oct 11 13:09:54.122 [initandlisten] recover cleaning up
Sat Oct 11 13:09:54.122 [initandlisten] removeJournalFiles
Sat Oct 11 13:09:54.123 [initandlisten] recover done
Sat Oct 11 13:09:54.166 [websvr] admin web console waiting for connections on port 28017
Sat Oct 11 13:09:54.166 [initandlisten] waiting for connections on port 27017

It should now be running. You can test your new database using the mongo command line client. Here’s an example:

tzetterl$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Sat Oct 11 13:12:39.284 [initandlisten] connection accepted from #1 (1 connection now open)

The list line (>) is the Mongo client’s prompt. we can type in commands there:

> help help on db methods help on collection methods sharding helpers replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce

show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, ‘global’ is default
use set current database list objects in collection foo { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell

Let’s see what databases are created by default:

> show dbs
local 0.078125GB
mydb 0.203125GB

By default, we are using the test database.

Creating Some Basic Data in MongoDB
The main MongoDB Quick Start guide provides information on how to get started, you can find the details here.

From here, you may want to go on and learn more about the MongoDB query language in the online docs (


Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Subscribe for updates

Enter your email address to subscribe to receive notifications of new content by email.

Join 5,882 other subscribers