zoukankan      html  css  js  c++  java
  • 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;






  • 相关阅读:
    初次学习Vue,输出Hello Vue!
    js的let语句在安卓手机端的QQ浏览器出错的问题
    前端框架的对比
    Vue环境搭建及node安装过程整理
    快速排序与冒泡排序(面试题)
    判断一个字符串中出现次数最多的字符并统计其出现的次数(面试题)
    Go_18: Golang 中三种读取文件发放性能对比
    GO_05_2:Golang 中 panic、recover、defer 的用法
    Go_17:GoLang中如何使用多参数属性传参
    Go_16:GoLang中flag标签使用
  • 原文地址:https://www.cnblogs.com/aroundight/p/15068354.html
Copyright © 2011-2022 走看看