Installing, configuring, and running MongoDB

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.

Getting ready

Open a web browser and visit: https://www.mongodb.org/downloads.

How to do it…

Mac OS X

The following steps explain how to install, configure, and run MongoDB on Mac OS X:

  1. On the download page, click on the Mac OS X tab, and select the version you want.
  2. Click on the Download (TGZ) button to download MongoDB.
  3. 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.
  4. 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
      
  5. You should see the following output from Mongo:

    Mac OS X

Windows

The following steps explain how to install, configure, and run MongoDB on Windows:

  1. Click on the Windows tab, and select the version you want.
  2. Click on the Download (MSI) button to download MongoDB.
  3. 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

  4. Follow the instructions to complete the installation.
  5. Create a data folder at C:datadb. MongoDB needs this directory in order to run. This is where, by default, Mongo is going to store all its database files.
  6. Next, at the command prompt, navigate to the directory where Mongo was installed and run Mongo:
    cd C:Program FilesMongoDBServer3.0in
    Mongod.exe
    
  7. If you get any security warnings, give Mongo full access.
  8. You should see an output like the following screenshot from Mongo, letting you know it's working:

    Windows

Linux

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:

  1. Log in to your Linux box
  2. Import the public key:
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --  recv 7F0CEB10
    
  3. 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
    
  4. Update apt:
    sudo apt-get update
    
  5. Install the latest version of Mongo:
    sudo apt-get install -y mongodb-org
    
  6. Run Mongo with the following command:
    sudo service mongod start
  7. 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
  8. You can stop MongoDB by using the following mongod command:
    sudo service mongod stop
    
  9. Restart MongoDB with this command:
    sudo service mongod restart
    

    Note

    MongoDB log file location

    MongoDB stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb.

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset