mongo icon indicating copy to clipboard operation
mongo copied to clipboard

Can't connect, user rights issue? - MongoError: not authorized on myapp to execute command { listIndexes: "sessions", cursor: {} }

Open augnustin opened this issue 11 years ago • 3 comments

So I'm struggling with connecting my mongodb container to my nodeJS one, through docker-compose.

Here's my docker-compose.yml:

db:
  image: mongo
  ports:
    - "27017:27017"
    - "27018:27018"
  command: mongod   --smallfiles --rest --auth --logpath=/dev/null
web:
  build: .
  command: node app.js
  volumes:
    - .:/myapp
  ports:
    - "3000:3000"
  links:
    - db
  environment:
    PORT: 3000

I'm good with this. Then my node app does:

mongoose.connect('mongodb://172.17.0.71:27017/myapp');

which supposedly is the correct URL. But I get the following error:

web_1 | /myapp/node_modules/connect-mongo/node_modules/mongodb/lib/utils.js:98
web_1 |     process.nextTick(function() { throw err; });
web_1 |                                         ^
web_1 | MongoError: not authorized on myapp to execute command { listIndexes: "sessions", cursor: {} }
web_1 |     at Function.MongoError.create (/myapp/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
web_1 |     at queryCallback (/myapp/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:282:36)
web_1 |     at Callbacks.emit (/myapp/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)
web_1 |     at null.messageHandler (/myapp/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:243:23)
web_1 |     at Socket.<anonymous> (/myapp/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)
web_1 |     at Socket.emit (events.js:107:17)
web_1 |     at readableAddChunk (_stream_readable.js:163:16)
web_1 |     at Socket.Readable.push (_stream_readable.js:126:10)
web_1 |     at TCP.onread (net.js:538:20)

Googling this error doesn't help much, but from my searching it means that I'm not authenticated to access the myapp database.

So the questions are:

  • which user am I authenticated with by default using the mongo docker image?
  • What rights are associated to it?
  • How can I fix my issue?

augnustin avatar Oct 07 '15 13:10 augnustin

Found the issue: in the line command: mongod --smallfiles --rest --auth --logpath=/dev/null I'd simply had to remove --auth which I copy/pasted somewhere else. Working now! :smile:

augnustin avatar Oct 07 '15 15:10 augnustin

won't getting rid of --auth render your database insecure? how do you manage to maintain security?

Streemo avatar Nov 06 '15 13:11 Streemo

I have the same issue and I cant disable auth in my configuration. Did you ever find a work around for this ?

kirtanshetty avatar May 17 '18 06:05 kirtanshetty