Error writin in a replicaset with nodejs

67 views
Skip to first unread message

gre...@gmail.com

unread,
Apr 13, 2016, 9:55:35 AM4/13/16
to mongodb-user
i am working withn nodejs, I have a replicaset with three nodes, in three diferent machines, i had problems for reading but at the end i add readPreference=secondaryPreferred and i can read
 
var mongodb = require('mongodb');

    mongodb.connect('mongodb://user:userpass@ipOne:27017,iptwo:27018,iptree:27017/test?replicaset=test-db&readPreference=secondaryPreferred', function(err,db){
        assert.equal(null, err);
        assert.ok(db != null);
        if(err) {
            console.error((new Date()).toISOString(), 'error: ', err);
            myEmiter.emit('error', err);
        }
        dbData = db;
        console.log((new Date()).toISOString(),'Connected');

    });


The problem is that when i try to write in the replicaset i have the next error

error { [MongoError: not master]
  name: 'MongoError',
  message: 'not master',
  ok: 0,
  errmsg: 'not master',
  code: 10107 }


Any clue,
thanks




William Byrne III

unread,
Apr 28, 2016, 12:48:39 AM4/28/16
to mongodb-user

GreyCRD,

If you don’t specify a value for readPreference, the default is for applications to attempt to connect to the replica set Primary only. Given you could not read documents (or even connect I suspect) until you added readPreference=secondaryPreferred, I believe that this replica set has no accessible Primary.

I suggest you connect directly using just the mongo shell and run rs.status() to confirm the status of all nodes, and then use the replica sets troubleshooting guide to resolve the issues you find.

The error you see trying to write documents even after you have connected using readPreference=secondaryPreferred and read documents successfully means you are connected to a Secondary (or slave) node, not a Primary (or master) node. MongoDB does not support multi-master replication - all inserts/deletes/updates must be executed against the primary node.

Note that there are five different readPreferences. I think that primaryPreferred might suit your purposes better. It will connect to a Primary if one is available, or a Secondary if not. In contrast, the mode you are using (secondaryPreferred) will only connect to a Primary if there are no available Secondaries.

III


William Byrne III

Reply all
Reply to author
Forward
0 new messages