Failed to read MongoDB slave using Node.js

I saw a lot of questions in the same way, but somehow I still cannot successfully connect to the slave MongoDB.

I start my cluster on Kubernetes using mongo-k8s-sidecar , connecting my application directly to the wizard works fine, however whenever I try to connect to a slave, I cannot read, here is the error code:

MongoError: not master and slaveOk=false

I use Node.js for my application, and this is how I connect:

var mongodb = require('mongodb').Db;
var Server = require('mongodb').Server
var db = new mongodb('dbname', new Server("localhost",27017,{slaveOk: true}));

        db.open(function(err, conn){
             if (err){
                callback(err);
             }else {
             client=conn;
             client.createIndex("tablename", {field:1}
             , {background:true}, function(err, i) {
                 logger.info(err);
             });
//The rest of the code is trimmed

What am I missing here?

+4
source share
2 answers

You cannot run createIndex()in the Secondary member, even if you use slaveOk / ReadPreference.SECONDARY.

createIndex(), , , , , .

. . , , mongod, ( replSet), .

, .

+4

, v1.x , slaveOk: true db, :

var db = new mongodb('dbname', new Server("localhost",27017), {slaveOk: true});

DocRef

v2.x readPreference=ReadPreference.SECONDARY.

DocRef

+1

Source: https://habr.com/ru/post/1690028/


All Articles