Skip to content

Instantly share code, notes, and snippets.

@ericbolo
Last active September 26, 2016 10:53
Show Gist options
  • Save ericbolo/8890a517ba9c6b91bfaa087d50cadb69 to your computer and use it in GitHub Desktop.
Save ericbolo/8890a517ba9c6b91bfaa087d50cadb69 to your computer and use it in GitHub Desktop.
Installing, configuring and using Mongod on Ubuntu 16.04

Introduction

This is a guide to installing, configuring and using MongoDB on localhost

Installation & boot configuration

  • Import public key

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

  • Create source list file

    echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

  • Install

    sudo apt-get update sudo apt-get install -y mongodb-org

  • Start mongod daemon and add it as service to be started at boot time

    systemctl start mongod systemctl enable mongod

  • Check that mongo server is running on port 27017

    netstat -plntu

You should see a line that reads:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      2745/mongod 

Configuration

  • Open mongo shell

    mongo

  • Add root user to the admin db

    use admin db.createUser({user:"admin", pwd:"your_pwd_here", roles:[{role:"root", db:"admin"}]})

Restarting and connected with root user

sudo service mongod restart

mongo -u admin -p your_pwd_here --authenticationDatabase admin

Creating a db

use your_db_name

Note: immediately after this cmd, if you exec 'show dbs' you will not see the new db. Only when data is inserted into it will Mongodb consider the database to really exist.

Mongo shell commands

Refer to this cheat sheet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment