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:

[email protected]:~# apt-key adv --keyserver hkp:// --recv 7F0CEB10

Create apt source.list config:

[email protected]:~# echo "deb "$(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:

[email protected]:~# 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:

[email protected]:~# 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
Questions? Try the support group
> 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
[email protected]:~#

Enable authentication in MongoDB config and restart MongoDB service:

[email protected]:~# sed -i "s/#auth = true/auth = true/g" /etc/mongod.conf
[email protected]:~# service mongod restart

Test the connection to database with new credentials:

[email protected]:~# mongo -u admin -p root --authenticationDatabase admin
MongoDB shell version: 3.0.5
connecting to: test

That’s all, your MongoDB is setup.