Hi there. I've got a pretty reference implementation spinning up in a docker-compose.yml. I'm getting MongoError: Authentication failed when attempting to connect with the example connection string provided in the readme: 'mongodb://127.0.0.1/agenda'. Here are my files. They're quite simple:
service.js:
#!/usr/bin/env node
const Agenda = require('agenda');
const cconfig = require('cconfig');
const log = require('llog');
const {
MONGO_URI
} = cconfig();
log.info(`connecting to mongo at ${MONGO_URI}`);
const agenda = new Agenda({
db: { address: MONGO_URI }
});
agenda.define('delete old users', (job, done) => {
console.log('bla')
});
(async function() { // IIFE to give access to async/await
await agenda.start();
await agenda.every('1 minutes', 'delete old users');
})();
config.js:
module.exports = {
MONGO_URI: 'mongodb://root:password@mongo:27017/agenda',
development: {},
staging: {},
production: {},
}
docker-compose.yml:
version: '3.4'
services:
job-service:
image: patrickleet/node-alpine-gyp:9
depends_on:
- mongo
networks:
- mongo
volumes:
- .:/usr/src/service
restart: always
working_dir: /usr/src/service
environment:
LOG_LEVEL: ${LOG_LEVEL}
NODE_ENV: development
command: npm start
mongo:
image: mongo
networks:
- mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
mongo-express:
image: mongo-express
restart: always
networks:
- mongo
depends_on:
- mongo
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: password
networks:
mongo:
relevant logs:
mongo-express_1 | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1 | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1 | basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
mongo_1 | 2018-10-19T16:36:24.643+0000 I NETWORK [listener] connection accepted from 192.168.32.3:50862 #2 (1 connection now open)
mongo_1 | 2018-10-19T16:36:24.663+0000 I NETWORK [conn2] received client metadata from 192.168.32.3:50862 conn2: { driver: { name: "nodejs", version: "2.2.24" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.93-linuxkit-aufs" }, platform: "Node.js v8.12.0, LE, mongodb-core: 2.1.8" }
mongo-express_1 | Database connected
mongo_1 | 2018-10-19T16:36:24.764+0000 I ACCESS [conn2] Successfully authenticated as principal root on admin
mongo-express_1 | Admin Database connected
mongo_1 | 2018-10-19T16:36:24.772+0000 I NETWORK [listener] connection accepted from 192.168.32.3:50864 #3 (2 connections now open)
mongo_1 | 2018-10-19T16:36:24.848+0000 I ACCESS [conn3] Successfully authenticated as principal root on admin
job-service_1 | [nodemon] 1.18.4
job-service_1 | [nodemon] to restart at any time, enter `rs`
job-service_1 | [nodemon] watching: *.*
job-service_1 | [nodemon] starting `node ./bin/job-service`
job-service_1 | {"level":30,"time":1539966986570,"msg":"connecting to mongo at mongodb://root:password@mongo:27017/agenda","pid":31,"hostname":"a445e8c07ed3","v":1}
job-service_1 | (node:31) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
mongo_1 | 2018-10-19T16:36:26.587+0000 I NETWORK [listener] connection accepted from 192.168.32.4:40686 #4 (3 connections now open)
mongo_1 | 2018-10-19T16:36:26.592+0000 I NETWORK [conn4] received client metadata from 192.168.32.4:40686 conn4: { driver: { name: "nodejs", version: "3.1.8" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.93-linuxkit-aufs" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.1.7" }
mongo_1 | 2018-10-19T16:36:26.593+0000 I ACCESS [conn4] Supported SASL mechanisms requested for unknown user 'root@agenda'
mongo_1 | 2018-10-19T16:36:26.602+0000 I ACCESS [conn4] SASL SCRAM-SHA-1 authentication failed for root on agenda from client 192.168.32.4:40686 ; UserNotFound: Could not find user root@agenda
mongo_1 | 2018-10-19T16:36:26.606+0000 I NETWORK [conn4] end connection 192.168.32.4:40686 (2 connections now open)
job-service_1 | /usr/src/service/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
job-service_1 | throw err;
job-service_1 | ^
job-service_1 |
job-service_1 | MongoError: Authentication failed.
job-service_1 | at /usr/src/service/node_modules/mongodb-core/lib/connection/pool.js:581:63
job-service_1 | at authenticateStragglers (/usr/src/service/node_modules/mongodb-core/lib/connection/pool.js:504:16)
job-service_1 | at Connection.messageHandler (/usr/src/service/node_modules/mongodb-core/lib/connection/pool.js:540:5)
job-service_1 | at emitMessageHandler (/usr/src/service/node_modules/mongodb-core/lib/connection/connection.js:310:10)
job-service_1 | at Socket.<anonymous> (/usr/src/service/node_modules/mongodb-core/lib/connection/connection.js:453:17)
job-service_1 | at Socket.emit (events.js:159:13)
job-service_1 | at addChunk (_stream_readable.js:265:12)
job-service_1 | at readableAddChunk (_stream_readable.js:252:11)
job-service_1 | at Socket.Readable.push (_stream_readable.js:209:10)
job-service_1 | at TCP.onread (net.js:598:20)
job-service_1 | [nodemon] app crashed - waiting for file changes before starting...
Changing the connection string to not specify the db makes everything work:
config.js
module.exports = {
MONGO_URI: 'mongodb://root:password@mongo:27017',
development: {},
staging: {},
production: {},
}
relevant logs:
job-service_1 | [nodemon] restarting due to changes...
job-service_1 | [nodemon] starting `node ./bin/job-service`
job-service_1 | {"level":30,"time":1539967333026,"msg":"connecting to mongo at mongodb://root:password@mongo:27017","pid":42,"hostname":"a445e8c07ed3","v":1}
job-service_1 | (node:42) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
mongo_1 | 2018-10-19T16:42:13.044+0000 I NETWORK [listener] connection accepted from 192.168.32.4:40688 #5 (3 connections now open)
mongo_1 | 2018-10-19T16:42:13.050+0000 I NETWORK [conn5] received client metadata from 192.168.32.4:40688 conn5: { driver: { name: "nodejs", version: "3.1.8" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.93-linuxkit-aufs" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.1.7" }
mongo_1 | 2018-10-19T16:42:13.088+0000 I ACCESS [conn5] Successfully authenticated as principal root on admin
mongo_1 | 2018-10-19T16:42:13.100+0000 I STORAGE [conn5] createCollection: admin.agendaJobs with generated UUID: a2f60f70-fc9f-4aca-b9fd-6f32641eb053
mongo_1 | 2018-10-19T16:42:13.121+0000 I INDEX [conn5] build index on: admin.agendaJobs properties: { v: 2, key: { name: 1, nextRunAt: 1, priority: -1, lockedAt: 1, disabled: 1 }, name: "findAndLockNextJobIndex", ns: "admin.agendaJobs" }
mongo_1 | 2018-10-19T16:42:13.121+0000 I INDEX [conn5] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongo_1 | 2018-10-19T16:42:13.122+0000 I INDEX [conn5] build index done. scanned 0 total records. 0 secs
mongo_1 | 2018-10-19T16:42:13.133+0000 I NETWORK [listener] connection accepted from 192.168.32.4:40690 #6 (4 connections now open)
mongo_1 | 2018-10-19T16:42:13.135+0000 I ACCESS [conn6] Successfully authenticated as principal root on admin
job-service_1 | bla
@mateodelnorte I had similar issue, this is the difference between
{ useNewUrlParser: true } and false
True requires database (/agenda in URI).
False (or default/missing parameter) is the opposite.
The correct new (useNewUrlParser: true) connection string is
new Agenda({db: { address: 'mongodb://root:password@mongo:27017/agenda', collection : "collectionname", options: { useNewUrlParser: true }}})
I had to append '?authSource=admin' to the end of my connection string.
I had to append '?authSource=admin' to the end of my connection string.
This worked for me when connecting as root.
Why would you connect to the admin database? If someone could steal your admin credentials your mongodb will be totally compromised.
Most helpful comment
@mateodelnorte I had similar issue, this is the difference between
{ useNewUrlParser: true }and falseTrue requires database (/agenda in URI).
False (or default/missing parameter) is the opposite.
The correct new (useNewUrlParser: true) connection string is
new Agenda({db: { address: 'mongodb://root:password@mongo:27017/agenda', collection : "collectionname", options: { useNewUrlParser: true }}})