Test db access on replica set secondary

First Post here!!
I have a 3 members replica-set.
Created a testDB db in primary. And create a user, TestUser with role readWrite on testDB.
I can authenticate with this user in primary and when run show dbs, it show test + size, but I can’t authenticate with this user in secondary members.

I have used rs.printSecondaryReplicationInfo() in primary. It’s showing both secondary members are 0 seconds behind the primary. From this, I know the replica-set is working.

I have also deleted the whole /var/lib/mongodb dir and restarted mongod. It resynced successfully.

I’ve never developed an application that connects to mongodb. From my understanding, if primary fails, one of secondary becomes primary. But I don’t know how would the application access this testDB db, if I can’t authenticate with TestUser in secondary.

Do I need to grant this TestUser some specific roles, other than readWrite?
Or,can client application connect and access this test db with just readWrite access. Some explanation or useful link of how client application use replica set would be really appreciated.

I created a root user with role root on primary. It can authenticate on both primary and secondary.
But when I issue show dbs command, it only show test db in primary, but not in secondary. Is it because my replica-set is not running correctly?

I populated this test data using below functions.
var day = 1000 * 60 * 60 * 24 var randomDate = function () {
… return new Date(Date.now() - (Math.floor(Math.random() * day)))
var randomName = function() {
… return (Math.random()+1).toString(36).substring(2)
for (var i = 1; i <= 1000000; ++i) {
… db.test.insert({
… name: randomName(),
… creationDate: randomDate(),
… uid: i
… });
… }

I deleted this db and created again by importing json file with mongoimport. Now it show this db in both primary and secondary. I guess the data created by javascript function is the problem. But I don’t know how.

Hi @yehtet_mgmg welcome to the community!

In a replica set, all nodes are basically duplicates of each other, so what exists in the primary will also exist in all secondaries. This is because if the primary went down for whatever reason, a secondary will be elected as a new primary.

Note that “went down for whatever reason” also includes intentionally bringing down the node for maintenance purposes. A replica set will allow you to have uninterrupted access to the database even during maintenance times.

About your questions:

You should be able to. Mind posting the commands you tried and the error messages you’re seeing?

It is highly suspect for sure. Since they are supposed to be duplicates, this is not supposed to occur. If this is still an issue, please post the commands you used, and also the output of rs.status() and rs.conf().

I don’t think the data is the issue. Only primary can receive writes, and secondaries will replicate this write as quickly as they can. There might be some other settings that are missing or invalid.

If you’re interested to know more, I recommend checking out the free courses offered in MongoDB University, especially the course M103 Basic Cluster Administration. The courses available there will be able to get you started quickly in learning MongoDB.

Best regards

1 Like

Thanks for the reply. The replica set is working.

1 Like