Winston MongoDB - 传递数据库时出现身份验证错误

Winston MongoDB - Authentication error when passing the database

我打开这个问题是因为我不确定这是否只发生在我身上。

我在 mongo url 通过数据库时连接到数据库时遇到问题。我使用的结构与此类似:

const logger = winston.createLogger({
  levels: customLevels,
  format: winston.format.combine(
    winston.format.errors({ stack: true }),
    winston.format.json(),
    winston.format.timestamp(),
  ),
  transports: [
    new winston.transports.MongoDB({
      db: `mongodb://user:pass@host:port/database`,
      tryReconnect: true,
      collection: 'logs',
      options: { useNewUrlParser: true, useUnifiedTopology: true }
    }),
  ],
});

但是在尝试连接时,出现以下错误:

winston-mongodb: will try reconnecting in 10 seconds
winston-mongodb: error initialising logger MongoError: Authentication failed.

当我从 mongodb 的连接字符串中删除数据库时,它会连接并在 mongo 上创建一个名为“test”的数据库。用户名和密码正确,因为我输入了相同的数据来创建我要连接的数据库。

还有其他人经历过类似的事情吗?

您很可能需要在您的 uri 中添加身份验证数据库,如下所示:

 db: `mongodb://user:pass@host:port/database?authSource=admin`

否则 mongo 将在您提供的“数据库”中搜索用户 ...