简体   繁体   中英

Node.js “Error: read ECONNRESET”

I am trying to connect to mongo via Node.js and I'm getting the following error:

connection error: { Error: read ECONNRESET
at exports._errnoException (util.js:1022:11)
at TCP.onread (net.js:610:25) name: 'MongoError', message: 'read ECONNRESET' }
(node:3868) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: read ECONNRESET

Here is the code I'm using to connect:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

module.exports = (config) => {

        const fs = require('fs');

        let key = fs.readFileSync('../server/test/keys/cert_key.pem');
        let ca = [fs.readFileSync('../server/test/keys/ca.pem')];

        let o = {
            server: {
                sslValidate: true,
                sslCA: ca,
                sslKey: key,
                sslCert: key
            }
        };
        mongoose.connect(config.connectionString, o);


    let database = mongoose.connection;

    database.on('error', console.error.bind(console, 'connection error:'))

    database.once('open', (error) => {
        if (error) {
            console.log(error);
            return;
        }

        console.log('MongoDB ready!')
    });

    require('./../models/Role').initialize();
    require('./../models/User');
    require('./../models/Item');
    require('./../models/character/Character_item');
    require('./../models/Article');
    require('./../models/Friend_request');
    require('./../models/Code');
    require('./../models/Ring');
    require('./../models/Ring_shop');
    require('./../models/Comment');
};

If I try to connect to mongo from Robo3T, it works. So it seems the problem is in my Node code. Any suggestions?

Accordin mongoDB node driver documentation you must pass ssl: true in the server option in order to connect via SSL otherwise SSL is ignored.

My standard ssl db options is the following :

         server: {
                ssl:        true,
                sslValidate:true,
                sslCA:      ca,
                ca:         ca,
                sslKey:     key,
                sslCert:    key
            }

Confirm that your mongodb database's file is not damaged。If .wt file has damaged, you'll got that error. The .wt filen in your storage path. eg:

storage: dbPath: E:\\Data\\mongodb

You can use mongorestore command to repair data if you backuped before. otherwise you can drop the database and re-init a new one.

I was having the same issue as I was working on the cloud provided by MongoDB. The reason it was happening with me was that I have configured my home IP to be connected and not my work IP. After adding my work IP in the cloud settings I was able to connect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM