0

当我在 Docker 中为我的 MongoDB 实例引入身份验证时,我遇到了可能与议程.js 尝试连接到 MongoDB 的方式有关的问题,因为连接字符串调用成功的日志以使猫鼬连接到数据库,因此我假设字符串应该是有效的。

一切正常,直到我将连接字符串更改为使用身份验证。我验证了用户已在数据库中正确创建,还尝试了连接字符串的变体和删除/安装节点模块。

以下输出我在运行 docker-compose up 时得到:

server    | /server/node_modules/agenda/node_modules/mongodb/lib/utils.js:133
server    |       throw err;
server    |       ^
server    | 
server    | MongoError: command createIndexes requires authentication
server    |     at Connection.<anonymous> (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/pool.js:466:61)
server    |     at Connection.emit (events.js:314:20)
server    |     at processMessage (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/connection.js:384:10)
server    |     at Socket.<anonymous> (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/connection.js:553:15)
server    |     at Socket.emit (events.js:314:20)
server    |     at addChunk (_stream_readable.js:297:12)
server    |     at readableAddChunk (_stream_readable.js:272:9)
server    |     at Socket.Readable.push (_stream_readable.js:213:10)
server    |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
server    | Emitted 'error' event on Agenda instance at:
server    |     at /server/node_modules/agenda/lib/agenda/db-init.js:23:14
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:76:14
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:63:27
server    |     at ClientSession.endSession (/server/node_modules/agenda/node_modules/mongodb/lib/core/sessions.js:135:41)
server    |     at executeCallback (/server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:59:17)
server    |     at handleCallback (/server/node_modules/agenda/node_modules/mongodb/lib/utils.js:129:55)
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/create_index.js:85:14
server    |     at handleCallback (/server/node_modules/agenda/node_modules/mongodb/lib/utils.js:129:55)
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/command.js:113:23
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/core/connection/pool.js:420:18
server    |     at processTicksAndRejections (internal/process/task_queues.js:79:11) {
server    |   ok: 0,
server    |   errmsg: 'command createIndexes requires authentication',
server    |   code: 13,
server    |   codeName: 'Unauthorized',
server    |   [Symbol(mongoErrorContextSymbol)]: {}
server    | }

包.json

"dependencies": {
    "agenda": "^4.1.2",
    "axios": "^0.21.1",
    "bcryptjs": "^2.4.3",
    "body-parser": "latest",
    "express": "^4.17.1",
    "infinite-timeout": "^0.1.0",
    "jsonwebtoken": "^8.5.1",
    "method-override": "^3.0.0",
    "mongoose": "^5.12.4",
    "mosca": "^2.8.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "websocket": "^1.0.34",
    "webstorm": "^1.0.0",
    "ws": "latest"
  },
  "devDependencies": {
    "@shelf/jest-mongodb": "^1.1.3",
    "@types/express": "^4.17.1",
    "cors": "^2.8.5",
    "jest": "^24.9.0"
  },
  "engines": {
    "node": "12.x"
  }

码头工人-compose.yml

version: "3.8"
services:
  server:
    container_name: server
    build: .
    restart: unless-stopped
    ports:
      - "9000:9000"
      - "1883:1883"
    links:
      - mongo
    networks:
      - app-network
    command: npm start
  mongo:
    container_name: mongo
    image: mongo:4.4.4
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=superSafePassword
    ports:
      - "27317:27017"
    volumes:
      - dbdata:/data/db
      - "./init.js/:/docker-entrypoint-initdb.d/init.js:ro"
    restart: unless-stopped
    networks:
      - app-network
    command: mongod
networks:
  app-network:
    driver: bridge
volumes:
  dbdata:
  node_modules:

创建用户 init.js 的脚本

db = db.getSiblingDB("dbName");
db.createUser(
    {
        user: "dbUser",
        pwd: "dbPassword",
        roles: [
            {
                role: "readWrite",
                db: "dbName"
            }
        ]
    }
);

连接字符串

// mongo refers to the docker container
    connectionString : 'mongodb://dbUser:dbPassword@mongo:27317/dbName',  

我如何初始化议程:

class mScheduler {

    constructor() {
        if(typeof mScheduler.instance === 'object')
            return mScheduler.instance;

        this.agenda = new Agenda({ db: {address: config.connectionString, collection: 'agendaJobs', options: { useNewUrlParser: true }}, processEvery: '2 seconds', maxConcurrency : MAX_CONC});
        process.on('SIGTERM', this.endJobs);
        process.on('SIGINT', this.endJobs);
        setImmediate(async ()=> await this.agenda.start());
        mScheduler.instance = this;
        return this;
    }

someOtherMethods()
}

module.exports = new mScheduler();
4

1 回答 1

0

尝试这个

使用 Mongoose 提供的选项解决这些弃用警告

const connectionOptions = {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  useUnifiedTopology: true,
  autoIndex: false
};
this.agenda = new Agenda({ db: {address: config.connectionString, collection: 'agendaJobs', options: connectionOptions}, processEvery: '2 seconds', maxConcurrency : MAX_CONC});
于 2021-04-20T00:33:50.480 回答