How to Install and Configure MongoDB on Ubuntu 14.04 x64

In this howto we’ll setup and configure MongoDB server on Ubuntu 14.04 x64 (it’s only available for x64 LTS distributions). To begin we have to setup custom repository:

MongoDB installation

Add repo key:

root@mongodb:~# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create apt source.list config:

root@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

Update apt repo list and install MongoDB:

root@mongodb:~# apt-get update; apt-get install -y mongodb-org

MongoDB access configuration

Now let’s setup access to MongoDB.

Run MongoDB shell and switch to admin user:

root@mongodb:~# mongo
MongoDB shell version: 3.0.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> use admin
switched to db admin
>

Create root user and exit the shell:

> db.createUser({user:"admin", pwd:"root", roles:[{role:"root", db:"admin"}]})
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
> exit
bye
root@mongodb:~#

Enable authentication in MongoDB config and restart MongoDB service:

root@mongodb:~# sed -i "s/#auth = true/auth = true/g" /etc/mongod.conf
root@mongodb:~# service mongod restart

Test the connection to database with new credentials:

root@mongodb:~# mongo -u admin -p root --authenticationDatabase admin
MongoDB shell version: 3.0.5
connecting to: test
>

That’s all, your MongoDB is setup.