The MEAN stack pt.2: Mongodb

The MEAN stack is awesome, I’m loving using it.
This post will walk you through getting MongoDB (and optionally replica sets) up and running for your MEAN stack on a fresh install of Ubuntu 12.04.3.

Install MongoDB: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

$ sudo apt-get update

$ sudo apt-get install -y mongodb-10gen

This is as far as you need to go if you don’t need replica sets. The mean stack will work fine without them.

You can go to part 3, or continue on with replica sets.

 

Configuring MongoDB for replica sets: http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

You should have at least 3 machines that can all see one another to set up a replica set.

Step 1: Create an admin user: http://docs.mongodb.org/manual/tutorial/add-user-administrator/

Log into the first server using the localhost exception by running ‘mongo’.

$ mongo
> db = db.getSibilngDB('admin')
> db.addUser( {user: "<username>",
                pwd: "<password>",
              roles: [ "clusterAdmin",
                       "dbAdminAnyDatabase",
                       "userAdminAnyDatabase",
                       "readWriteAnyDatabase"]})

// you can use  [ctl-d] to exit mongo when you are done

2. Generate the secret key:

server1$ openssl rand -base64 741 > key1
server1$ sudo chmod 600 key1

#send the key to the other machines
server1$ scp key1 hostname.domain.tld:/home/username/key1

every_server$ sudo chown mongodb:nogroup key1
every_server$ sudo mv key1 /var/lib/mongodb/key1

3. Edit the MongoDB config file.

#add these lines:

keyFile = /var/lib/mongodb/key1
auth = true
setParameter = enableLocalhostAuthBypas=0
replSet = <your_replica_set_name>
port = 27017
smallfiles = true

Only replSet and keyFile are needed for replica sets, but smallfiles helps save space on dev machines, and the other options increase security. Set up each machine in your set with the same replica set name.

Restart each mongodb server.

$ sudo service mongodb restart

4. Start the cluster:

login to mongo on server 1 (where you made the admin user)

server1$ mongo
> use admin
switched to db admin

> db.auth("<username>","<password>")
1

> rs.initiate()
{ bunch of stuff....
.....}

wait about a minute then press enter to get the cluster prompt.

You will need to let your new cluster know who it’s members are. Make sure that all machines can resolve the names that you add to the cluster. But first make sure the current host is correct:

your_replica_set:PRIMARY> cfg = rs.conf()

your_replica_set:PRIMARY> cfg.members[0].host="this_host.domain.tld:27017"
this_host.domain.tld:27017

your_replica_set:PRIMARY> rs.reconfig(cfg)
blah blah failed
blah blah trying reconnect
blah blah reconnect blah ok
reconnected to server after rs command (which is normal)

now add the other hosts one at a time

your_replica_set:PRIMARY> rs.add("host.domain.tld:27017")
{ "ok" : 1 }

your_replica_set:PRIMARY> rs.add("other_host.domain.tld:27017")
{ "ok" : 1 }

As each host is added, it brings itself up and joins the the cluster.

Enjoy your new, awesome MongoDB replica set!

Go to part 3: Node

Leave a Reply