【问题标题】:MongoDB Replica Set Connection not created on serverMongoDB 副本集连接未在服务器上创建
【发布时间】:2018-07-12 16:35:06
【问题描述】:

我正在使用 MongoDB 4.0。我有一个在我的机器和不同端口上运行的副本集:

127.0.0.1:27017 (Master)
127.0.0.1:27018 (Slave)
127.0.0.1:27019 (Arbiter)
My replica name is "xdr"

现在在我的 nodejs 代码中在 localhost 上创建连接时,它将创建连接,即

const options = {
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  autoReconnect : true
};

mongoose.connect('mongodb://localhost:27017, localhost:27018, localhost:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

在我的本地连接上一切都很好,但是当我将我的 mongodb 托管在 AWS 上的单独 EC2 实例上时,它不会连接到我的副本集。

假设我的 mongodb AWS IP 是 12.12.13.12。

因此,当我创建连接时,它将无法连接。我的代码是

const options = {
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  autoReconnect : true
};

mongoose.connect('mongodb://12.12.13.12:27017, 12.12.13.12:27018, 12.12.13.12:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

它会产生一个错误,即“”

如果我在没有副本集的情况下连接,那么它将只连接到主节点,即

mongoose.connect('mongodb://12.12.13.12:27017/my_db, options);

我的代码有什么做错了吗?

【问题讨论】:

  • 嗨,Abhay - 你遇到了什么错误?您的问题似乎缺少它,我想只是一个拼写错误?

标签: node.js mongodb mongoose replicaset


【解决方案1】:

您是否验证过 EC2 上的端口 27017 和 27018 已打开? 您无需指定仲裁者。

您必须通过 replset 选项通知 mongo 您使用副本集。

我将以下内容用于我的副本集:

var config = {
   db: "mongodb://myMasterIp:27017/myDb,mySlave1Ip:27018/myDb"
   options: {
            user:           config.db_user,// only if you use username/password for DB authentication
            pass:           config.db_pass,// only if you use username/password for DB authentication
            replset: {
                rs_name:    "MyReplicaSetName",
                ssl:        true,// only if you use ssl for your replicaset
                sslValidate:false,// only if you use ssl for your replicaset
                sslCA:      myDbCertificate,// only if you use ssl for your replicaset
                ca:         myDbCertificate,// only if you use ssl for your replicaset
                sslKey:     myDbKey,// only if you use ssl for your replicaset
                sslCert:    myDbKey // only if you use ssl for your replicaset
            },
            socketOptions : {
               keepAlive :         1,
               connectTimeoutMS :  5000
            },
            server: { // only if you use ssl for your replicaset
               ssl:        true,
               sslValidate:false,
               sslCA:      myDbCertificate,
               ca:         myDbCertificate
               sslKey:     myDbKey,
               sslCert:    myDbKey
            },
            auth: { // only if you use username/password for DB authentication
                authdb: 'myAuthenticationDatabse'
            }
        }
};

mongoose.connect(config.db, config.options);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多