Archive for the ‘MongoDB’ Category

Convert a Standalone MongoDB to a Replica Set

I already have standalone mongod instance (called svr01) and want to convert into four member replica set (1 primary, 2 secondary, 1 arbiter). My objective is to distribute read query through secondaryPreferred read preference.

Create siteRootAdmin user to manage replica set.

#connect as admin
mongo --host localhost --port 27001 -u admin -p admin --authenticationDatabase admin

#in mongo shell
use admin
db.createUser( {
    user: "siteRootAdmin",
    pwd: "admin",
    roles: [ { role: "root", db: "admin" } ]

Create key file, this file will be use to internal authentication (between mongod instance). For simplicity I cerate file filled with string “password” named keyFile and save to /var/. Set permission to 600 (owner only). Read more…

Categories: MongoDB

MongoDB Learning Notes – Simulate automate failover

February 22, 2015 Leave a comment

To simulate automate failover I kill the primary node with Ctrl+C or use command db.shutdownServer(). When the primary killed, the secondary become primary because a majority of the original nodes (the arbiter and the secondary) are still able ping each other.

myapp:PRIMARY> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
myapp:PRIMARY> use admin
switched to db admin
myapp:PRIMARY> db.shutdownServer()

And in secondary node log:

2015-02-22T13:58:33.558+0700 [rsHealthPoll] replSet member bluething-PC:20000 is now in state DOWN
2015-02-22T13:58:34.391+0700 [conn501] replSet info voting yea for bluething-PC:20001 (1)
2015-02-22T13:58:36.565+0700 [rsHealthPoll] warning: Failed to connect to, reason
: errno:10061 No connection could be made because the target machine actively refused it.
2015-02-22T13:58:36.565+0700 [rsHealthPoll] replset info bluething-PC:20000 heartbeat failed, retrying
2015-02-22T13:58:36.632+0700 [rsHealthPoll] replSet member bluething-PC:20001 is now in state PRIMARY

Connect to secondary and check status Read more…

MongoDB Learning Notes – Replication Overview and Setup replica set

February 22, 2015 Leave a comment

MongoDB provides two flavors of replication: master-slave replication and replica sets. For both, a single primary node receives all writes, and then all secondary nodes read and apply those write to themselves asynchronously. Replica sets additionally ensure automated failover: if the primary goes offline for any reason, then one of the secondary nodes will automatically be promoted to primary, if possible.

The minimum recommended replica sets configuration consists of three nodes. Two of these nodes serve as first-class, persistent mongod instance. Either can act as the replica set primary, and both have a full copy of the data. The third node in the set is an arbiter, which doesn’t replicate data, but merely acts as a kind of neutral observer, when failover is required, the arbiter helps to elect a new primary node.

1. Creating a data directory
I’m using windows and data directory is C:\data

mkdir C:\data\node1
mkdir C:\data\node2
mkdir C:\data\arbiter

2. Start each node as separate mongod (in separate terminal) Read more…

Categories: MongoDB, NoSQL Tags: , ,