Now, we are going to learn how to download and run the MongoDB server on a Linux box:
1. Fire up the terminal program. Type in the following command and hit Enter
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.3.tgz > mongo.tgz
2. Extract the downloaded archive by using the following command:
tar xzf mongo.tgz
3. Rename the extracted directory by using the following command:
mv mongodb-linux-i686-1.8.3 mongodb
4. Create the data directory
/data/db
by using the following command:sudo mkdir p /data/db sudo chown `id -u` /data/db
5. Startup the server by running the following command:
./mongodb/bin/mongod
6. Open another tab in the terminal and run the next command:
./mongodb/bin/mongo
7. Type
show dbs
into the shell and hit Enter.
In step 1, we downloaded the latest stable release of MongoDB 32-bit version for Linux using the wget
program, and stored it as a GZIP tarball named mongo.tgz
on your machine.
Note
At the time of this writing, the latest production release for MongoDB is 1.8.3. So when you try this, if a newer production release is available, you should download that version instead.
In steps 2 and 3, we extracted the tarball and renamed the extracted directory to mongodb
for convenience. In step 4, we created the data directory /data/db
for MongoDB, and gave it permission to read from and write to that directory. In step 5, we startup the MongoDB server by executing the mongodb/bin/mongod
script.
In step 6, after we have successfully launched the server, we start the mongo interactive shell:
$./mongodb/bin/mongo MongoDB shell version: 1.8.1 url: test connection to test type "help" for help >
Once the shell has started, we issue the command show dbs
to list all the pre-loaded databases in the server:
>show dbs local (empty) admin (empty) >
The databases listed here are special databases pre-built within the server. They are used for administration and authentication purposes. We do not need to concern ourselves with them right now.
Note
Installing MongoDB using package managers
You can use the package manager of your Linux distribution (apt for Debian/Ubuntu, yum for Fedora/CentOS) to install MongoDB. To get distro-specific instructions, Ubuntu/Debian users should visit http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages. Users of CentOS and Fedora should visit http://www.mongodb.org/display/DOCS/CentOS+and+Fedora+Packages. The advantage of using a package manager, other than being able to install with fewer commands, is that you can launch the Mongo server and the client just by typing mongod
and mongo
respectively in the shell.
The instructions for installing MongoDB on an OS X powered Mac machine are the same as those for Linux. You have to download the OS X specific binaries for Mongo (available at http://www.mongodb.org/downloads), and follow the same steps to execute them.
Alternatively, if you have package managers installed on your OS X (Homebrew or MacPorts), you can use them to install MongoDB.
To install MongoDB with HomeBrew use the following command:
$ brew update $ brew install mongodb
To use MacPorts to install MongoDB use the following command:
$ sudo port install mongodb
When we launched the mongod
program, it booted up with some default configuration settings, such as the path to the data directory (C:\data\db on Windows or /data/db
on Unix). In real world deployments, we want to be able to specify these settings ourselves. There are two ways to achieve that. We can either modify them by supplying command-line parameters to the mongod
program at invocation, or by using file-based configurations.
We can override the default MongoDB settings by passing command-line parameters to the mongod
program. For example, the next command tells MongoDB to use C:\mongodb_data
as data directory by sending it as a --dbpath
argument:
C:\>mongodb\bin\mongod --dbpath C:\mongodb_data
The following table lists some useful command-line parameters and their functions:
Parameter |
What it does |
---|---|
|
Path to the directory for storing data files. |
|
IP address that the |
|
Port address that |
|
Full file path to the log file where the MongoDB messages will be written. By default all messages are written to standard output. |
|
Setting this option to true appends the messages at the end of the log file. Setting it to |
We can see the full list of command-line options by running mongod
with the--help option:
C:\>mongodb\bin\mongod -help
An alternative to sending all those command-line parameters to mongod
manually is to put the required configuration settings in a file and then pass the path of the file as a--config option. For example, consider the following sample configuration file:
dbpath = D:\mongodb_data logpath = D:\mongodb.log logappend = true
We store this file to a location, say C:\mongodb.conf
. Now, to start MongoDB with the these settings, we have to enter the next command in the CMD prompt:
C:\>mongodb\bin\mongod --config C:\mongodb.conf
mongod
will be loaded with these configuration settings. Note that file-based parameters are the same as those for command-line options.
Start MongoDB with the following settings, using a file-based configuration:
Default data directory at
/usr/bin/mongo
.Default port address at
8888
.Messages will be logged at
/var/logs/mongodb.log
. The log file should be overwritten over time.
There are several ways you can shutdown a running MongoDB server.
In the terminal window (or CMD prompt window in case you are on Windows) running the mongod
process, hit Ctrl + C. This will signal the server to do a clean shutdown, flush, and close its data files.