1.1 Ubuntu Gnu/linux
First off, there is a version of MongoDB available in the regular Ubuntu repositories, but this version is somewhat out of date and we will be using some features that require at least version 2.x of MongoDB.
With this in mind, we will install MongoDB from the 10gen maintained repositories for Ubuntu. Most of you will be using a relatively new version of Ubuntu, which will support upstart, so we will concentrate on that.
In order to avoid GPG key errors when updating and working with software sources, we need to import the 10gen public GPG key. In a terminal window, type:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
This may fail if your internet connection is bad, but keep on trying until it succeeds. You will not be able to take advantage of automatic updates and bug fixes through apt if you do not import the key. You will, however, still be able to install MongoDB from the apt repositories.
Edit the file
/etc/apt/sources.list.d/10gen.list
and add the line
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
to it.
You should now do a
sudo apt-get update
to refresh your apt sources and download the headers.
Afterwards, you can simply install the packages with
sudo apt-get install mongodb-10gen
The service should start automatically, but if not, you can control it using Upstart in the following manner
sudo service mongodb start sudo service mongodb stop sudo service mongodb restart
If everything has gone well, you should be able to start the mongo shell with the mongo command.
If there is an issue in starting the service, you may need to manually create the
/data/db
directory which Mongo defaults to using. You can also specify the directory by passing
the
--dbpath
directive to point to another directory, or configuring the dbpath in
/etc/mongodb
1.2 Mac OSX
MongoDB on Mac OSX can be installed in one of two ways, using the package management tools MacPorts or Homebrew.
Using Homebrew is probably easier, as you simply need to open up a system terminal, and type in the following commands
brew update
to update your package manager,
brew install mongodb
and then, at a later stage to upgrade MongoDB
brew update brew upgrade mongodb
Installing using MacPorts is also relatively simple, but due to the fact that the code will need to be compiled on your system, it may take some time. To start the build using MacPorts issue the command
port install mongodb
Bear in mind that neither MacPorts or Homebrew come with any of the control scripts, but if your PATH is configured correctly they will be in your system path. You will need to start the mongod process manually and connect to it via mongo.
You may also choose to build MongoDB from the 10gen sources, in which case, you simply need to download, extract and fire up the mongod process, and you are ready to go!
1.3 Windows
Installing MongoDB on Windows is as simple as any of the other platforms. Please ensure that you download the latest stable version for your platform (64bit or 32bit)
MongoDB is self-contained and does not have any other system dependencies. You can run MongoDB from any folder you choose. You may install MongoDB in any directory (e.g. D:\database\mongodb)
Start up a command prompt by selecting the Start button, All Programs, Accessories and then Command Prompt. As MongoDB requires a data directory, please create one using
md data md data\db
You may then start the mongod process specifying the data directory.
C:\mongodb\bin\mongod.exe
If you have created your db directory in an alternative location, please use the –dbpath parameter to specify where it is
C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongoDBdata"
Do not allow mongod.exe to be accessible to public networks without running in “Secure Mode” MongoDB is designed to be run in “trusted environments” and the database does not enable authentication or “Secure Mode” by default.
MongoDB can also be set up as a Windows service. First, configure the system with
md C:\mongodb\log
Then create a configuration file for the logpath
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg
Then, install and run the mongoDB service
C:\mongodb\bin\mongod.exe --config C:\mongodb\mongod.cfg --install net start MongoDB
If you have followed all of the instructions on your particular platform, ensure that you can start the MongoDB shell with the mongo command. This will connect the shell to your running local server. If your server was installed on a different server, you may connect to it by passing the IP address or fully qualified domain name of the MongoDB server into the mongo shell command. e.g.
mongo 192.168.1.101:27017
Where 27017 is the standard port number that MongoDB process runs on.
You can always check which version of the server that you have connected to by entering
db.version()
into the shell and hopefully you will see the version number of the server that you have just intstalled.
Take note that the shell is a modified Javascript shell. That means that some global commands will always be valid, like help and exit. The global database identifier is the db keyword, and can be used to get additional help on the database that you are currently using. A good example of this would be to get the status on a database with db.status() which will return some useful information about the database.





