MongoDB ReplSetTest()
replSetTest is an internal command that is not enabled by default. replSetTest must be enabled by using –setParameter enableTestCommands=1 on the mongod command line. replSetTestcannot be enabled during run-time.
> var rst = new ReplSetTest( { name: ‘rsTest’, nodes: 3 } )
Starting new replica set rsTest
>
> rst.startSet()
ReplSetTest starting set
ReplSetTest n is : 0
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20000,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 0,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-0’
2016-03-03T06:38:21.438+0530 I – [thread1] shell: started program (sh3866): mongod –oplogSize 40 –port 20000 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-0 –setParameter enableTestCommands=1
d20000| 2016-03-03T06:38:21.463+0530 I CONTROL [initandlisten] MongoDB starting : pid=3866 port=20000 dbpath=/data/db/rsTest-0 64-bit host=srinivas-HP-G62-Notebook-PC
d20000| 2016-03-03T06:38:21.463+0530 I CONTROL [initandlisten] options: { net: { port: 20000 }, replication: { oplogSizeMB: 40, replSet: “rsTest” }, setParameter: { enableTestCommands: “1” }, storage: { dbPath: “/data/db/rsTest-0”, mmapv1: { preallocDataFiles: false, smallFiles: true } } }
[ connection to srinivas-HP-G62-Notebook-PC:20000 ]
ReplSetTest n is : 1
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20001,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 1,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-1’
2016-03-03T06:38:22.851+0530 I – [thread1] shell: started program (sh3897): mongod –oplogSize 40 –port 20001 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-1 –setParameter enableTestCommands=1
d20001| 2016-03-03T06:38:22.875+0530 I CONTROL [initandlisten] MongoDB starting : pid=3897 port=20001 dbpath=/data/db/rsTest-1 64-bit host=srinivas-HP-G62-Notebook-PC
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001
]
ReplSetTest n is : 2
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20002,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 2,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-2’
2016-03-03T06:38:24.267+0530 I – [thread1] shell: started program (sh3928): mongod –oplogSize 40 –port 20002 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-2 –setParameter enableTestCommands=1
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001,
connection to srinivas-HP-G62-Notebook-PC:20002
]
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001,
connection to srinivas-HP-G62-Notebook-PC:20002
]
>
>
>
> var rst = new ReplSetTest( { name: ‘rsTest’, nodes: 3 } )
Starting new replica set rsTest
>
> rst.startSet()
ReplSetTest starting set
ReplSetTest n is : 0
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20000,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 0,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-0’
2016-03-03T06:38:21.438+0530 I – [thread1] shell: started program (sh3866): mongod –oplogSize 40 –port 20000 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-0 –setParameter enableTestCommands=1
d20000| 2016-03-03T06:38:21.463+0530 I CONTROL [initandlisten] MongoDB starting : pid=3866 port=20000 dbpath=/data/db/rsTest-0 64-bit host=srinivas-HP-G62-Notebook-PC
d20000| 2016-03-03T06:38:21.463+0530 I CONTROL [initandlisten] options: { net: { port: 20000 }, replication: { oplogSizeMB: 40, replSet: “rsTest” }, setParameter: { enableTestCommands: “1” }, storage: { dbPath: “/data/db/rsTest-0”, mmapv1: { preallocDataFiles: false, smallFiles: true } } }
[ connection to srinivas-HP-G62-Notebook-PC:20000 ]
ReplSetTest n is : 1
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20001,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 1,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-1’
2016-03-03T06:38:22.851+0530 I – [thread1] shell: started program (sh3897): mongod –oplogSize 40 –port 20001 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-1 –setParameter enableTestCommands=1
d20001| 2016-03-03T06:38:22.875+0530 I CONTROL [initandlisten] MongoDB starting : pid=3897 port=20001 dbpath=/data/db/rsTest-1 64-bit host=srinivas-HP-G62-Notebook-PC
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001
]
ReplSetTest n is : 2
{
“useHostName” : true,
“oplogSize” : 40,
“keyFile” : undefined,
“port” : 20002,
“noprealloc” : “”,
“smallfiles” : “”,
“replSet” : “rsTest”,
“dbpath” : “$set-$node”,
“restart” : undefined,
“pathOpts” : {
“node” : 2,
“set” : “rsTest”
}
}
ReplSetTest Starting….
Resetting db path ‘/data/db/rsTest-2’
2016-03-03T06:38:24.267+0530 I – [thread1] shell: started program (sh3928): mongod –oplogSize 40 –port 20002 –noprealloc –smallfiles –replSet rsTest –dbpath /data/db/rsTest-2 –setParameter enableTestCommands=1
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001,
connection to srinivas-HP-G62-Notebook-PC:20002
]
[
connection to srinivas-HP-G62-Notebook-PC:20000,
connection to srinivas-HP-G62-Notebook-PC:20001,
connection to srinivas-HP-G62-Notebook-PC:20002
]
>
>
>
>
Sometimes I need an experimental mongodb replica set, to test-drive something I’m working on, or to simulate a scenario I see in a production environment.
What I don’t want to do is screw up my local development environment. At the same time, I don’t want to drag around another environment that has to be updated, maintained, etc.
Enter: ReplSetTest().
The ReplSetTest function allows you to quickly spin up a mongodb environment suitable for testing or rapid prototyping, and tear down the environment when completed. In this post, we’ll cover how to use the ReplSetTest function to quickly set up a mongodb replica set.
The ReplSetTest function requires two parameters to get it started; name and nodes. The name parameter is really as simple as it sounds: the name of the ReplicaSet you want to create. Nodes have a few more options– at a minimum, it requires the number of nodes you want to create. Optionally, it contains the names of the nodes to be created, and any specific parameters to be used. To spin up a new replica set, do the following from a terminal window:
var rst = new ReplSetTest( { name: ‘rsTest’, nodes: 3 } )
You’ll notice that we’ve given the name “rsTest” to our replica set and declared 3 nodes to be created. Now type:
rst.startSet()
and watch the magic happen. After a few minutes, open another terminal window and you’ll see three instances of mongod running: running mongod instances
Now that the instances are running, we still have to initiate the replica set and add the nodes to the replica set ourselves. This can be done with:
rs.initiate()
rs.add(“Wills-MacBook-Pro.local:31001”)
rs.add(“Wills-MacBook-Pro.local:31002”)
Your replica set should now sync up and be fully available: replicaset status There is one other feature I want to cover on this.
Let’s say we call ReplSetTest with the following options:
var rst = new ReplSetTest( { name: ‘rsTest’, nodes: { node1: {}, node2: {}, arb: {arbiter: true} } } )
This time, we’ve specified the nodes we want rather than just supplying an integer. You will also see that we’ve named our third node “arb” and passed in an object with “arbiter: true”. This will do exactly what it looks like: it will create the 3rd node as an arbiter. We can initiate
the replica set and add the nodes:
rs.initiate()
rs.add(“Wills-MacBook-Pro.local:31001”)
rs.addArb(“Wills-MacBook-Pro.local:31002”)
And inspect after the replica set initiates: replicaset status with arbiter A few final notes on the ReplSetTest function:
You can control many of the node options by adding additional options inside the node object. Examples include smallfiles, port number, rest interface and the db path
Any node starting with the letter “a” in its name is automatically configured as an arbiter (eliminating the need to specify “arbiter: true”)
Many more features exist, allowing for quite a bit of creativity and control. For more information, see http://api.mongodb.org/js/current/
>