【问题标题】:mongodb mongoose connection open callback doesnt get calledmongodb mongoose 连接打开回调没有被调用
【发布时间】:2017-05-08 09:18:35
【问题描述】:

我有一个 MEAN 项目,这是我 server.js 中的一个 sn-p

var db = require('./config/db');
// url : 'mongodb://localhost/cdnserver'
// results are same for 127.0.0.1 or the machines ip
console.log(mongoose.connect(db.url));

mongoose.set('debug', true);
mongoose.connection.on('connected', function () {
  console.log('Mongoose default connection open to ' + db.url);
});

// If the connection throws an error
mongoose.connection.on('error',function (err) {
  console.log('Mongoose default connection error: ' + err);
});

// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
  console.log('Mongoose default connection disconnected');
});

这个设置已经运行了 3 个多月。现在我将我的整个 MEAN 堆栈与数据库一起复制到其他机器上。我拿了一个 mongodump 并做了 mongorestore。从终端通过 mongo 恢复的数据库设置看起来很好。

但是,当启动服务器时,连接打开回调没有被调用。如果我停止 mongodb 服务,则会调用断开连接和错误回调。我该如何进一步调试?

我正在附加两个设置的控制台输出。

设置 1:

    Mongoose {
  connections:
   [ NativeConnection {
       base: [Circular],
       collections: {},
       models: {},
       replica: false,
       hosts: null,
       host: 'localhost',
       port: 27017,
       user: undefined,
       pass: undefined,
       name: 'cdnserver',
       options: [Object],
       otherDbs: [],
       _readyState: 2,
       _closeCalled: false,
       _hasOpened: false,
       _listening: false,
       db: [Object] } ],
  plugins: [],
  models: {},
  modelSchemas: {},
  options: { pluralization: true } }
Server up on 80
Mongoose default connection open to mongodb://localhost/cdnserver
1
Mongoose: videos.find({}) { skip: 0, limit: 5, fields: undefined }

设置 2:

Mongoose {


connections:
   [ NativeConnection {
       base: [Circular],
       collections: {},
       models: {},
       replica: false,
       hosts: null,
       host: 'localhost',
       port: 27017,
       user: undefined,
       pass: undefined,
       name: 'cdnserver',
       options: [Object],
       otherDbs: [],
       _readyState: 2,
       _closeCalled: false,
       _hasOpened: false,
       _listening: false,
       db: [Object] } ],
  plugins: [],
  models: {},
  modelSchemas: {},
  options: { pluralization: true } }
Server up on 80
1
1
Mongoose default connection disconnected
Mongoose default connection error: Error: connection closed

cat /var/log/mongodb/mongodb.log 在两台机器上显示完全相同的输出。

更新 1:设置突然开始正常工作,但又停止了。我无法弄清楚是什么导致了这种情况发生。

【问题讨论】:

  • 你能从你的终端连接mongo吗?也许您已经在新数据库上启用了身份验证?
  • @GilZ 我可以连接,没有设置身份验证。另外,当系统间歇性工作时,我没有更改任何身份验证设置。
  • 当您的连接断开时,您是否经常在/var/log/mongodb/mongodb.log 中检查 mongodb 服务器日志?
  • @BertrandMartel 日志文件看起来很相似,只是 pId 发生了变化。

标签: mongodb mongoose mean-stack


【解决方案1】:

我终于弄明白了,新设置使用的是更新版本的 nodejs。

当我从 7.x 迁移到 6.x 时,它运行良好。我猜猫鼬,节点 7,mongodb 版本并没有很好地结合在一起。

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 2014-04-24
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多