未在服务器上创建MongoDB副本集连接

时间:2018-07-12 06:24:36

标签: node.js mongodb mongoose replicaset

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

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:'));

现在在我的nodejs代码中的本地主机上创建连接时,它将创建连接,即

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:'));

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

让我们假设我的mongodb AWS IP为12.12.13.12。

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

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

它将生成错误,即“”

如果我没有副本集就连接,那么它将仅连接到主要

errors='coerce'

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

1 个答案:

答案 0 :(得分:0)

您是否已验证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);