连接到 MongoDB - 命令 'createIndexes' 需要身份验证(可能与 Agenda.js 相关?)
Connecting to MongoDB - Command 'createIndexes' requires authentication (Maybe Agenda.js related?)
当我在 Docker 中为我的 MongoDB 实例引入身份验证时,我 运行 遇到了可能与 agenda.js 尝试连接的方式有关的问题MongoDB,由于连接字符串调用了 mongoose 连接到数据库的成功日志,因此我认为该字符串应该有效。
一切正常,直到我将连接字符串更改为使用身份验证。我验证了用户已在数据库中正确创建,还尝试了连接字符串和 deleting/installing 节点模块的变体。
我得到以下输出 运行 docker-撰写:
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 | }
package.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"
}
docker-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();
试试这个
使用 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});
当我在 Docker 中为我的 MongoDB 实例引入身份验证时,我 运行 遇到了可能与 agenda.js 尝试连接的方式有关的问题MongoDB,由于连接字符串调用了 mongoose 连接到数据库的成功日志,因此我认为该字符串应该有效。
一切正常,直到我将连接字符串更改为使用身份验证。我验证了用户已在数据库中正确创建,还尝试了连接字符串和 deleting/installing 节点模块的变体。
我得到以下输出 运行 docker-撰写:
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 | }
package.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"
}
docker-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();
试试这个
使用 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});