In this section, you'll see how to install, configure, and run MongoDB on all the major operating systems—Mac OS X, Windows, and Linux.
Open a web browser and visit: https://www.mongodb.org/downloads.
The following steps explain how to install, configure, and run MongoDB on Mac OS X:
On the download page, click on the Mac OS X tab, and select the version you want.
Unpack the downloaded file and copy to any directory that you like. I typically create an
Applications
folder in my home directory where I install apps like this.For our purpose, we're going to set up a single instance of MongoDB. This means there is literally nothing to configure. To run MongoDB, open a command prompt and do the following:
At the root of your computer, make a data directory:
sudo mkdir data
Make your user the owner of the directory using the
chown
command:chown your_user_name:proper_group data
Go to the directory where you have MongoDB.
Go to the MongoDB directory.
Type the following command:
./mongod
You should see the following output from Mongo:
The following steps explain how to install, configure, and run MongoDB on Windows:
Once downloaded, browse to the folder where Mongo was downloaded, and double-click on the installer file.
When asked which setup type you want, select Complete
Follow the instructions to complete the installation.
Create a data folder at
C:\data\db. MongoDB
needs this directory in order to run. This is where, by default, Mongo is going to store all its database files.Next, at the command prompt, navigate to the directory where Mongo was installed and run Mongo:
cd C:\Program Files\MongoDB\Server\3.0\bin Mongod.exe
You should see an output like the following screenshot from Mongo, letting you know it's working:
The easiest way to install MongoDB in Linux is by using apt
. At the time of writing, there are apt
packages for 64-bit long-term support Ubuntu releases, specifically 12.04 LTS and 14.04 LTS. Since the URL for the public key can change, please visit the Mongo Installation Tutorial to ensure that you have the most recent one: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/.
Install Mongo as follows:
Log in to your Linux box
Import the public key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 -- recv 7F0CEB10
Create a list file for MongoDB:
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
Update
apt:
sudo apt-get update
Install the latest version of Mongo:
sudo apt-get install -y mongodb-org
Run Mongo with the following command:
sudo service mongod start
Verify that MongoDB is running by checking the contents of the log file at
/var/log/mongodb/mongod.log
for a line that looks like this:[initandlisten] waiting for connections on port 27017
You can stop MongoDB by using the following
mongod
command:sudo service mongod stop
Restart MongoDB with this command:
sudo service mongod restart
MongoDB's document data model makes it easy for you to store data of any structure and to dynamically modify the schema. In layman's terms, MongoDB provides a vast amount of flexibility when it comes to storing your data. This comes in very handy when we import our data. Unlike with an SQL database, we won't have to create a table, set up a scheme, or create indexes—all of that will happen automatically when we import the data.