mongodb线上环境报错Db.prototype.authenticate method will...

项目部署到阿里云服务器ubuntu14.04时会发生一次重启,看日志说
Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
我在这里看到有人遇到同样的问题 https://github.com/Automattic...,说是mongoose的原因,我把mongoose升级到最新版本后还是报这个错。。。
这是我连mongodb的链接:

mongodb://blog_runner:safeblog@127.0.0.1:27017/blog-app

blog_runner是具有读写权限的。如何解决呢?

阅读 7.8k
3 个回答

是与mongoose的版本有关系,我用4.7.0是没有这个提示的,升级到最新4.10.6就提示了

/**
 * 报错代码:
 */
mongoose.connect(DB_URL)

报错提示:
clipboard.png

/**
 * 新增Option,特别是useMongoClient这一项后,无报错
 */
const options = {
    useMongoClient: true,
    reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
    reconnectInterval: 500, // Reconnect every 500ms
    poolSize: 10, // Maintain up to 10 socket connections
    // If not connected, return errors immediately rather than waiting for reconnect
    bufferMaxEntries: 0
};

mongoose.connect(DB_URL,options);

此时DB_URL的写法也需要更改

/**
 * 原先错误代码
 */
DB_URL='mongodb://admin:admin@localhost:27017/mongodb?authSource=admin'

/**
 * 正确代码去掉?authSource=admin
 */
DB_URL='mongodb://admin:admin@localhost:27017/mongodb'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题