To verify that replication is working, we will create some test data on the primary member, and then try to read it from one (or more) of the secondary members.

Connect to the primary with the mongo shell

On the primary replicat set member (as determined by the output from rs.staus() in the previous section), start the mongo shell again by running the command

casdev-srv01# mongo -u mongoadmin -p --authenticationDatabase admin
MongoDB shell version v3.6.0
Enter password:
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.0
rs0:PRIMARY> 

and entering the correct password (“changeit”). The mongo shell prompt now displays the replica set name and member status.

Create some test data

Enter the commands below to create a new database called testDatabase (databases are created automatically the first time they are used) and store some documents in a collection called testCollection:

rs0:PRIMARY> use testDatabase
switched to db testDatabase
rs0:PRIMARY> for (var i=1; i <= 10; i++) db.testCollection.insert( { val: i } )
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> 

Then retrieve the documents just created by running the command

rs0:PRIMARY> db.testCollection.find()
{ "_id" : ObjectId("5a298797050075eeef5df805"), "val" : 1 }
{ "_id" : ObjectId("5a298797050075eeef5df806"), "val" : 2 }
{ "_id" : ObjectId("5a298797050075eeef5df807"), "val" : 3 }
{ "_id" : ObjectId("5a298797050075eeef5df808"), "val" : 4 }
{ "_id" : ObjectId("5a298797050075eeef5df809"), "val" : 5 }
{ "_id" : ObjectId("5a298797050075eeef5df80a"), "val" : 6 }
{ "_id" : ObjectId("5a298797050075eeef5df80b"), "val" : 7 }
{ "_id" : ObjectId("5a298797050075eeef5df80c"), "val" : 8 }
{ "_id" : ObjectId("5a298797050075eeef5df80d"), "val" : 9 }
{ "_id" : ObjectId("5a298797050075eeef5df80e"), "val" : 10 }
rs0:PRIMARY> 

Connect to a secondary with the mongo shell

Now connect with the mongo shell on one of the secondary members (e.g., casdev-srv02) by running the command

casdev-srv02# mongo -u mongoadmin -p --authenticationDatabase admin
MongoDB shell version v3.6.0
Enter password:
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.0
rs0:SECONDARY>

and entering the correct password (“changeit”).

Enable secondary member read operations

By default, reads from secondary members are not allowed; this is to ensure that a query does not retrieve stale data. Even simple commands like show dbs and show collections will fail with an error. But in this case we want to read from the secondary, to make sure the data got copied from the primary. To enable this, run the command

rs0:SECONDARY> db.getMongo().setSlaveOk()

which will enable reading from the secondary for duration of this connection.

Check that everything was replicated

Run the commands

rs0:SECONDARY> show dbs
admin         0.000GB
config        0.000GB
local         0.000GB
testDatabase  0.000GB
rs0:SECONDARY> use testDatabase
switched to db testDatabase
rs0:SECONDARY> show collections
testCollection
rs0:SECONDARY> 

to verify that testDatabase and testCollection are indeed available on the secondary. Then run the command

rs0:SECONDARY> db.testCollection.find()
{ "_id" : ObjectId("5a298797050075eeef5df805"), "val" : 1 }
{ "_id" : ObjectId("5a298797050075eeef5df807"), "val" : 3 }
{ "_id" : ObjectId("5a298797050075eeef5df808"), "val" : 4 }
{ "_id" : ObjectId("5a298797050075eeef5df806"), "val" : 2 }
{ "_id" : ObjectId("5a298797050075eeef5df80a"), "val" : 6 }
{ "_id" : ObjectId("5a298797050075eeef5df80c"), "val" : 8 }
{ "_id" : ObjectId("5a298797050075eeef5df80d"), "val" : 9 }
{ "_id" : ObjectId("5a298797050075eeef5df80b"), "val" : 7 }
{ "_id" : ObjectId("5a298797050075eeef5df80e"), "val" : 10 }
{ "_id" : ObjectId("5a298797050075eeef5df809"), "val" : 5 }
rs0:SECONDARY> 

to check that, indeed, the data inserted on the primary is also present on the secondary. The data may not appear in numerical order, since the command did not attempt to sort them, but they should all be present.

Delete the test database

Exit the mongo shell on the secondaries, and then run the commands

rs0:PRIMARY> show dbs
admin         0.000GB
config        0.000GB
local         0.000GB
testDatabase  0.000GB
rs0:SECONDARY> use testDatabase
switched to db testDatabase
rs0:SECONDARY> db.dropDatabase()
{
        "dropped" : "testDatabase",
        "ok" : 1,
        "operationTime" : Timestamp(1512671689, 2),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1512671689, 2),
                "signature" : {
                        "hash" : BinData(0,"CpHBrLKG56+ehaMf8Uk5QlzeSX8="),
                        "keyId" : NumberLong("6496845223040122881")
                }
        }
}
rs0:PRIMARY> 

on the primary to remove the test database and its contents.

References