mongoose 报错 scheduleTimeoutControl MongoError: command find requires authentication

可能原因是 mongoose 版本和mongodb 数据库版本不匹配,https://mongoosejs.com/docs/compatibility.html

 

 

我再mongose 升级后报错 MongoTimeoutError: Server selection timed out after 30000 ms

查了好多资料都不行,后来索性新建了一个项目只有链接数据库,居然可以没有报错.

想想区别就是app.js 里面引用了别的东西,一行一行注销测试发现和 "excel-export""^0.5.1" 不兼容

所以报这个错可以研究下是不是和别的东西不兼容

贴下我mongoose@5.7.0的链接代码

import mongoo = require('mongoose');
import config = require('../config');

// 设置连接池数量,并且超过后自动销毁
// auth,user,password
const options = {
    poolSize: 100,
    auto_reconnect: true,
    autoReconnect:true,
    keepAlive: 10,
    useNewUrlParser: true,
    useUnifiedTopology:true,
    reconnectTries: Number.MAX_VALUE// 总是尝试重新连接
    reconnectInterval: 500// 每500ms重新连接一次
    bufferMaxEntries: 0,
    connectTimeoutMS: 99999 // 99s后放弃重新连接
};
mongoo.set('useCreateIndex'true)
// 连接数据库
mongoo.connect(config.connectionoptions, (err=> {
    if (err) {
        console.log('Connection Error:' + err);
    } else {
        console.log('Connection success!');
    }
});

const db = mongoo.connection;
 
db.on('error', (err=> {
    console.log('mongoose connection error:' + err);
    mongoo.disconnect();
});
 
db.once('open', (err=> {
    if (err) {
        console.log('Mongoose open Error:' + err);
    } else {
        console.log('Mongoose open success!');
    }
});

db.on('connecting'function() {
    console.log('connecting to MongoDB...');
});

db.on('connected'function() {
    console.log('MongoDB connected!');
});

db.on('reconnected'function () {
    console.log('MongoDB reconnected!');
});

db.on('disconnected'function() {
    console.log('MongoDB disconnected!');
    // 连接数据库
    mongoo.connect(config.connectionoptions, (err=> {
        if (err) {
            console.log('Connection Error:' + err);
        } else {
            console.log('Connection success!');
        }
    });
});

module.exports = mongoo;






 

posted @ 2021-07-27 23:01  Aroundight  阅读(585)  评论(0编辑  收藏  举报