knowledge-kitchen

MongoDB - Setup

Database Design

  1. Overview
  2. Server Setup
  3. Client Setup
  4. Automation
  5. Conclusions

Overview

The Plan

We will outline a few ways to gain access to a MongoDB server and use a MongDB client to issue commands to the database. Our discussion will include:

, remotely on NYU’s classes-mongodb database server

, and remotely using MongDB Atlas - a cloud-hosted solution.

, remotely on NYU’s i6 web server

, and using a driver with any of today’s popular programming languages

Server Setup

Choices of server

There are several easy ways to try out a MongoDB server.

We will use this.

Create an account on NYU CS Department’s MongoDB server

All students will need to initialize an account on our MongoDB server using the “Class MongoDB Manager”

Credentials

Once logged in, the Class MongoDB Manager will show you your password for the MongoDB database server - this is different from your i6 web server password.

You will need to save the following credentials somewhere.

Client setup

Choices of client

Once set up with a server, you must access it using a client. There are several common ways to use a MongoDB client:

Logging into NYU CS Department’s i6 server

You will need ssh to remotely log into most servers, including the CS Department’s i6 web server.

The command, ssh fb1258@i6.cims.nyu.edu will remotely log in to the i6 server.

Connecting to MongoDB from NYU CS Department’s i6 Web Server

Once logged into the i6 web server, you can use the mongosh (or an older version named just mongo) command line client to connect to the MongoDB database server.

This command can be automatically run every time you log into the i6 web server… details here.

Verify that you are truly logged in

Prove to yourself that you are logged into a Javascript command-line interface to MongoDB.

If so, you’re ready to start using your MongoDB database.

Automation

The problem: typing commands manually with every login

Every time you log into the i6 web server, you must run the module load mongodb-4.0 to load the MongoDB client before using it.

The solution: .bashrc

Commands placed within the .bashrc file on UNIX and Linux machines are automatically executed every time a user begins a new shell session.

To automatically run the module load mongodb-4.0 command from the .bashrc script:

Now this command will run automatically every time you log in.

The problem: entering passwords with ssh

It is tedious to remember and enter passwords every time one logs into a remote server.

The solution: public and private SSH keys

To automatically authenticate when remotely logging in, we will first generate a pair of linked cryptographic keys locally (a public key, which you can share and a matching private key which you should keep private) and then place a copy of the public key on the server.

The solution: public and private SSH keys (continued)

We must now copy the public key to the server. The ssh-copy-id command can do this automatically for us.

Assuming the command above worked successfully, you should now be able to log into i6 by simply entering, ssh fb1258@i6.cims.nyu.edu, where fb1258 is replaced with your own username. You will no longer be asked to enter a password. Try logging out and then logging in again.

Conclusions

Thank you. Good luck.