CMSI 486: Installing and Running Mongo

Download Mongo

  1. Go to the Mongo DB download page
  2. Download the appropriate Community Server version for your operating system

SPECIAL NOTE AS OF FALL 2019:

The Mac OS version called Catalina which was released October 7, 2019 BREAKS THE ABILITY to make a directory at the top level. You will need to install and run from your user directory, for both the mongo installation and the data directory. Also, depending if you have full access to everything or not, you may not even be able to do that. If you get stuck and nothing you try seems to work, get help right away or even see if you can use someone else's computer for this part of the assignment.

In addition, these instructions DO NOT use the brew or home brew utility, so if you decide to go that route for installation, you will need to find your own instructions. This page is STRICTLY for the manual installation method.

Installation on Mac

  1. The download should go into your downloads folder, but double-check where it goes so you can find it later
  2. Open a terminal window, then change directory to the Downloads folder
  3. Enter the command

    tar xzf mongodb-osx-x86_64-[version].tgz

    where [version] is the version number of the downloaded tarball; for example, the latest version at this writing is 3.6.5 so the file name would be mongodb-osx-ssl-x86_64-3.6.5.tgz
  4. Note that the tar command will untar the archived file; the xzf means extract from the zipped archive file named…
  5. Move the extracted file to the proper location for use with the command

    sudo mv mongodb-osx-x86_64-[version] /usr/local/mongodb

  6. Create a data directory for the mongo server to use with the command

    sudo mkdir -p /data/db

    the -p argument means make the parent directory if it's needed
  7. Make sure that your account owns the new data directory with the command

    sudo chown [username] /data/db

    where [username] is the name on the account you are using
  8. Add the new installation to your command path by creating and editing your bash profile file using the following command sequence:

    cd ~
    touch .bash_profile
    vi .bash_profile – this will open the vi editor so you can add the next two lines to the file
    export MONGO_PATH=/usr/local/mongodb
    export PATH=$PATH:$MONGO_PATH/bin
    Press the esc key to get the colon prompt for vi, then enter wq to save these changes to the .bash_profile
    source ./.bash_profile to make the changes take effect in this terminal window

  9. Verify that the path variable contains the mongo directory

    echo $PATH

  10. Verify that the mongo installation is installed correctly and that you can start up the mongo server and client using the following command

    mongo -version

  11. You should see MongoDB shell version: [version] if all is well

Installation on Windows

  1. The download should go into your downloads folder, but double-check where it goes so you can find it later
  2. Open an explorer window, then change directory to the Downloads folder
  3. Double-click on the msi file that you downloaded; Windows will start the self-installation process, with which you are undoubtedly familiar…
  4. Use all the default options for the installation; you will need to be the administrator for these operations, or you may need to run as administrator to make things work
  5. Create a data directory for the mongo server to use with the command

    mkdir C:\data\db

  6. By default, mongo is installed in C:\Program Files\MongoDB\Server\[version] where [version] is the version number of mongo, which at this writing is 3.6.4
  7. Add the new installation to your command path by adding the installation directory to your search path environment using the following commands:

    Click start, then click settings; the system settings window will appear
    Click System. then in the system settings window enter environment in the search box
    Click on Edit the system environment variables; a new dialog window will appear
    Click on the bottom button in the dialog, labeled Environment Variables; a new dialog window will appear
    In the lower pane of that window, click on the variable labeled Path, then click on the Edit…button; YET ANOTHER new dialog window will appear
    Click the button labeled New to make a new entry, then type the following into the text line entry that shows up at the bottom of the list:

    C:\Program Files\MongoDB\Server\3.6\bin

    Note the 3.6 is the version number for the Mongo you installed; make sure this is the correct version for the path you are installing!
    Click OK for all the open dialog windows in succession to get out of them, then close the settings window
  8. Verify that the path variable contains the mongo directory

    path

  9. Verify that the mongo installation is installed correctly and that you can start up the mongo server and client by opening a cmd window and entering the command

    mongo -version

  10. You should see MongoDB shell version: [version] if all is well

Testing your installation

In either a terminal window [on a mac] or a cmd window [on windows], start the mongo server by using the command

mongod

Then open a second terminal window [or another tab] or a second cmd window and start a client by using the command

mongo

If all is well, you should see LOTS of stuff scroll by in the first window [the server] and a good bit of stuff appear in the second window [the client] which will be followed there by the mongo prompt which is a single angle bracket [the greater than sign >]. At this point, you can enter a mongo command. The easiest one to use would be show dbs to see the names of the databases that are part of the shipped installation for mongo.

To exit the client, type the command exit in the client window, then close that window [it's the same command on either mac or windows]. To stop the server, use the control+c command, then close that window as well.

REMEMBER than mongo is set up to be a command line interface by default. There are many tools that you can find to add a graphical or GUI front end to it for management purposes, but you should try to get familiar with running it from the command line. Doing so will really connect you with the way mongo things work, so that when it comes time to do a full stack development effort you'll understand what you're doing.

Next Steps…

The next thing to do is install some data into your mongo application. The following steps will help you install a copy of the Northwind database, which is kind of a standard demonstration thing to show you some of the concepts. BTW, you'll need this anyhow for an upcoming homework assignment, so you may as well bite the bullet and install it soon.

Here are the steps:

  1. go to the page https://github.com/tmcnab/northwind-mongo/archive/master.zip and get the zip or tar file containing the Northwind database files. This is a collection of csv files [read comma-separated values] which will have the data for the database.
  2. copy or move the zip/tar file to your mongo data directory, wherever you put it. For Windows, it is likely in C:\data\db. For mac, it is likely in /data/db.
  3. unzip or untar the file directly into that directory; you should see a bunch of files with the file extension .csv
  4. start the mongo server using a terminal window or cmd prompt window and the mongod command
  5. execute the following commands to import the data into the database [you can probably copy and paste these lines directly into the terminal window]:

    mongoimport -d Northwind -c categories --type csv --file categories.csv --headerline
    mongoimport -d Northwind -c customers --type csv --file customers.csv --headerline
    mongoimport -d Northwind -c employee-territories --type csv --file employee-territories.csv --headerline
    mongoimport -d Northwind -c employees --type csv --file employees.csv --headerline
    mongoimport -d Northwind -c northwind --type csv --file northwind.csv --headerline
    mongoimport -d Northwind -c order-details --type csv --file order-details.csv --headerline
    mongoimport -d Northwind -c orders --type csv --file orders.csv --headerline
    mongoimport -d Northwind -c products --type csv --file products.csv --headerline
    mongoimport -d Northwind -c regions --type csv --file regions.csv --headerline
    mongoimport -d Northwind -c shippers --type csv --file shippers.csv --headerline
    mongoimport -d Northwind -c suppliers --type csv --file suppliers.csv --headerline
    mongoimport -d Northwind -c territories --type csv --file territories.csv --headerline

  6. start a new terminal window [or tab] or a new cmd prompt window and change to the directory where you unzipped the Northwind csv files, then start a mongo client with the mongo command
  7. in the mongo client window, execute the show dbs command and verify that the Northwind database exists
  8. again in the mongo client window, execute the use Northwind command, the execut the show collections command to verify there are collections available
  9. finally, in the mongo client window, execute the db.shippers.find()command to verify that there is actually data in at least one of the collections
  10. pat yourself on the back, you have successfully installed mongo and the Northwind sample database!
  11. exit the mongo client using the exit command, then close that window or terminal tab
  12. exit the mongod server using the control+C interrupt command, then close that window as well
  13. Hey, we're all done!!