Skip to content

Ubuntu MongoDB

Cooper Filby edited this page Oct 9, 2017 · 2 revisions

MONGODB

Adding MongoDB to the repository:

More Info

  1. Adding MongoDB repository to apt:

    a. Import Key for MongoDB: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

    b. Enter sudo password.

    c. Add MongoDB repository details to apt:

    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

    d. Update apt-get: sudo apt-get update

Installing and Verifying MongoDB:

  1. Install MongoDB: sudo apt-get install -y mongodb-org
  2. Create a unit file describing the service: sudo nano /etc/systemd/system/mongodb.service
  3. Paste in the following content then save and close file:
    /etc/systemd/system/mongodb.service
    [Unit]
    Description=High-performance, schema-free document-oriented database
    After=network.target

    [Service]
    User=mongodb
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

    [Install]
    WantedBy=multi-user.target
  1. To save and close:

    a. Ctrl + x to exit

    b. Y to save

    c. Enter to complete the process

  2. Start the newly created service: sudo systemctl start mongodb

  3. Make sure that MongoDB starts automatically at reboot: sudo systemctl enable mongod

  4. Check MongoDB status to verify its running: sudo systemctl status mongodb

Creating MongoDB User / Securing the database:

More Info

  1. Connect to Mongo shell: mongo
  2. Connect to admin database: use admin
  3. Create user:

db.createUser ( { user: “<username>”, pwd: “<password>”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )

a.	It will output successful when done.
  1. To exit: exit and press Enter

Enable MongoDB Authentication:

Note: MongoDB Authentication is not required, but it is highly recommended.

  1. Open configuration file: sudo nano /etc/mongod.conf
  2. In the #security section, remove the # in front of security to enable the stanza. Press enter at the end of security and type: authorization: “enabled”
  3. It should look like:
    mongodb.conf
    . . .
    security:
      authorization: "enabled"
    . . .
  1. Restart MongoDB: sudo systemctl restart mongod
  2. Check status: sudo systemctl status mongod
  3. If MongoDB failes to start, restart the server.

To verify authentication:

  1. Connect to Mongo shell: mongo
  2. Test access: show dbs a. The output should have an error with listDatabases failed.
  3. Type: exit and Enter

Verifying the Administrative User’s Access:

  1. Connect as the admin: mongo –u <username> -p --authenticationDatabase admin
  2. Enter password for admin account and Enter.
  3. To see the databases: show dbs a. It should show you the list of databases
  4. Exit: exit and press Enter.

Create User/Database for the Program

  1. Connect to the Mongo shell using admin account: mongo –u <username> -p --authenticationDatabase admin
  2. Enter password for admin
  3. Create database : use <mydb>
  4. Create user for database: db.createUser( { user: “<username>”, pwd: “<password>”, roles: [ { role: "dbOwner", db: "<mydb>" } ] } )
  5. Exit: exit and press Enter

Adjust / Setup Firewall to allow external connections (Optional):

This is not recommended or required unless your MongoDB Service is hosted separately from your NodeJS service.

Warning: Enabling this will expose your MongoDB database to the entire network! Without authentication enabled your data will not be safe.

The firewall is set to only allow OpenSSH which is what you need.

  1. To allow MongoDB on its default port of 27017: sudo ufw allow 27017

  2. To allow MongoDB access by another server: sudo ufw allow from <your_other_server_ip_address>/32 to any port 27017

  3. Verify change in status: sudo ufw status

    You should see traffic to port 27017 allowed.

Next Steps

Now that MongoDB is installed you'll want to install and configure Nginx as a reverse proxy.