egg-mongoose auth登录验证有问题

Created on 19 Jan 2018  ·  11Comments  ·  Source: eggjs/egg

yarn run start命令后的错误日志

[egg-scripts] Starting egg application at /home/foodadmapi.bloging.top
[egg-scripts] Run node /home/foodadmapi.bloging.top/node_modules/egg-scripts/lib/start-cluster {"sticky":true,"title":"egg-server-foodapi.yg178.cn","framework":"/home/foodadmapi.bloging.top/node_modules/egg","baseDir":"/home/foodadmapi.bloging.top"} --title=egg-server-foodapi.yg178.cn
[egg-scripts] Save log file to /root/logs
[egg-scripts] Wait Start: 1...
[egg-scripts] 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.
[egg-scripts]
[egg-scripts] Start got error, see /root/logs/master-stderr.log
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

mongoose相关配置

let mongoUrl = process.env.NODE_ENV === "development" ? 'mongodb://localhost/wechat' : 'mongodb://localhost:27017/wechat'
  config.mongoose = {
    url: mongoUrl,
    options: {
      user: 'root1',
      pass: 'food11'
    }
  };

这是啥情况???

egg-mongoose

Most helpful comment

config.mongoose = {
    url: 'mongodb://localhost/wechat',
    options: {
      auth: { authSource: "admin" },
      user: 'admin',
      pass: '123456'
    }
  };

刚才去mongoose扫了一下Issues,说是要在options里加入auth: { authSource: "admin" },
答案链接
https://github.com/Automattic/mongoose/issues/5235
非常感谢,您的耐心帮助,谢谢。

All 11 comments

  • 这个不是 auth 的报错,start 脚本里面改为 egg-scripts start --ignore-stderr
  • 为什么不用 config.local.js 而用 process.env.NODE_ENV?

@atian25

yarn run v1.3.2
$ egg-scripts start --daemon --ignore-stderr
[egg-scripts] Starting egg application at /home/foodadmapi.bloging.top
[egg-scripts] Run node /home/foodadmapi.bloging.top/node_modules/egg-scripts/lib/start-cluster {"title":"egg-server-foodapi.yg178.cn","framework":"/home/foodadmapi.bloging.top/node_modules/egg","baseDir":"/home/foodadmapi.bloging.top"} --title=egg-server-foodapi.yg178.cn
[egg-scripts] Save log file to /root/logs
[egg-scripts] Wait Start: 1...
[egg-scripts] 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.
[egg-scripts] 2018-01-19 16:45:44,380 ERROR 6163 nodejs.MongoError: [egg-mongoose]Authentication failed.
[egg-scripts]     at Function.MongoError.create (/home/foodadmapi.bloging.top/node_modules/mongodb-core/lib/error.js:31:11)
[egg-scripts]     at /home/foodadmapi.bloging.top/node_modules/mongodb-core/lib/connection/pool.js:497:72
[egg-scripts]     at authenticateStragglers (/home/foodadmapi.bloging.top/node_modules/mongodb-core/lib/connection/pool.js:443:16)
[egg-scripts]     at Connection.messageHandler (/home/foodadmapi.bloging.top/node_modules/mongodb-core/lib/connection/pool.js:477:5)
[egg-scripts]     at Socket.<anonymous> (/home/foodadmapi.bloging.top/node_modules/mongodb-core/lib/connection/connection.js:333:22)
[egg-scripts]     at emitOne (events.js:116:13)
[egg-scripts]     at Socket.emit (events.js:211:7)
[egg-scripts]     at addChunk (_stream_readable.js:263:12)
[egg-scripts]     at readableAddChunk (_stream_readable.js:250:11)
[egg-scripts]     at Socket.Readable.push (_stream_readable.js:208:10)
[egg-scripts]     at TCP.onread (net.js:594:20)
[egg-scripts] name: 'MongoError'
[egg-scripts] message: '[egg-mongoose]Authentication failed.'
[egg-scripts] ok: 0
[egg-scripts] errmsg: 'Authentication failed.'
[egg-scripts] code: 18
[egg-scripts] codeName: 'AuthenticationFailed'
[egg-scripts] pid: 6163
[egg-scripts] hostname: iZwz929a4e4u8ptm1m34j1Z
[egg-scripts]
[egg-scripts]
[egg-scripts] Start got error, see /root/logs/master-stderr.log

添加了“--ignore-stderr”还是这样。

@atian25
[egg-scripts] message: '[egg-mongoose]Authentication failed.' 这句话提示,不就是说,验证失败了。

先确认下你的 config 正确了没,https://eggjs.org/zh-cn/faq.html#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E7%9A%84%E9%85%8D%E7%BD%AE%E4%B8%8D%E7%94%9F%E6%95%88

@atian25
这个是application_config.json里mongoose相关配置,pass咋变成这个了。

    "mongoose": {
      "url": "mongodb://localhost/wechat",
      "options": {
        "user": "root",
        "pass": "<String len: 12>"
      }
    },

agent_config.json 这个是application_config.json里mongoose相关配置。

    "mongoose": {
      "url": "mongodb://localhost/wechat",
      "options": {}
    },

说明你的配置可能写错了,看下 「配置文档」合并的那里吧,你最上面写的是 root1,但合并后是 root,某个地方写错了。pass 那个不用管,安全问题,不会打印出来。

不是了,我账号就是root,那个是我粘代码时故意加了个1,密码也是12个字符串。配置合并应该没有问题。

[egg-scripts] 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.

报错里上面这句话,说验证auth要用MongoClient.connect。

这个只是 warning 而已

这个是本地,我在本地也做了个测试。直接是报红色错误。
12

config.mongoose = {
    url: 'mongodb://localhost/wechat',
    options: {
      auth: { authSource: "admin" },
      user: 'admin',
      pass: '123456'
    }
  };

刚才去mongoose扫了一下Issues,说是要在options里加入auth: { authSource: "admin" },
答案链接
https://github.com/Automattic/mongoose/issues/5235
非常感谢,您的耐心帮助,谢谢。

config.mongoose = {
    url: 'mongodb://localhost/wechat',
    options: {
      auth: { authSource: "admin" },
      user: 'admin',
      pass: '123456'
    }
  };

刚才去mongoose扫了一下Issues,说是要在options里加入auth: { authSource: "admin" },
答案链接
https://github.com/Automattic/mongoose/issues/5235
非常感谢,您的耐心帮助,谢谢。

感谢您一家人,我也成功了

Was this page helpful?
0 / 5 - 0 ratings