env : - Spring boot 2.1.0 (with default spring dependencies).
- MongoDB 3.4.9 (replicaset : 2 worker node + 1 arbitrator), 1 database with 2 user (admin and specific user for this db with readWrite role)
properties config : data: mongodb: uri: mongodb://$USER:$PWD@HOST:27017,HOST:27018/$DATABASE
One of my pojo object use Index(unique=true), i use mongo repository for CRUD operation.
So, when spring boot start, i've this stack :
com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'not authorized on DATABASE to execute command { createIndexes: "XXX
a", indexes: [ { key: { oid: 1 }, name: "oid", ns: "XXXX", unique: true } ] }' on server XXXX:27017. The full response is { "ok" : 0.0, "errmsg"
: "not authorized on XXX to execute command { createIndexes: \"XXXX\", indexes: [ { key: { oid: 1 }, name: \"oid\", ns: \"XXXXX\", unique:
true } ] }", "code" : 13, "codeName" : "Unauthorized" }
Log from mongo.log :
2018-11-16T10:19:44.897+0100 I NETWORK [thread1] connection accepted from 127.0.0.1:46678 #169 (5 connections now open)
2018-11-16T10:19:44.938+0100 I NETWORK [conn169] received client metadata from 127.0.0.1:46678 conn169: { driver: { name: "mongo-java-driver", version: "3.8.2" }, os
: { type: "Linux", name: "Linux", architecture: "amd64", version: "2.6.32-642.15.1.el6.x86_64" }, platform: "Java/Oracle Corporation/1.8.0_77-b03" }
2018-11-16T10:19:46.062+0100 I NETWORK [thread1] connection accepted from 127.0.0.1:46680 #170 (6 connections now open)
2018-11-16T10:19:46.062+0100 I NETWORK [conn170] received client metadata from 127.0.0.1:46680 conn170: { driver: { name: "mongo-java-driver", version: "3.8.2" }, os
: { type: "Linux", name: "Linux", architecture: "amd64", version: "2.6.32-642.15.1.el6.x86_64" }, platform: "Java/Oracle Corporation/1.8.0_77-b03" }
2018-11-16T10:19:46.077+0100 I ACCESS [conn170] Unauthorized: not authorized on xxx to execute command { createIndexes: "xxxx", indexes: [ { key: { oid:
1 }, name: "oid", ns: "xxxxx", unique: true } ] }
2018-11-16T10:19:46.200+0100 I - [conn170] end connection 127.0.0.1:46680 (6 connections now open)
2018-11-16T10:19:46.200+0100 I - [conn169] end connection 127.0.0.1:46678 (5 connections now open)
When i use the mongo shell directly :
mongo mongodb://$USER:$PWD@HOST:27017,HOST:27018/$DATABASE
I can execute the createIndexes without error.
Log from mongo.log :
2018-11-16T10:21:16.448+0100 I NETWORK [thread1] connection accepted from 127.0.0.1:46682 #171 (5 connections now open)
2018-11-16T10:21:16.448+0100 I NETWORK [conn171] received client metadata from 127.0.0.1:46682 conn171: { driver: { name: "MongoDB Internal Client", version: "3.4.9"
}, os: { type: "Linux", name: "Red Hat Enterprise Linux Server release 6.8 (Santiago)", architecture: "x86_64", version: "Kernel 2.6.32-642.15.1.el6.x86_64" } }
2018-11-16T10:21:16.485+0100 I ACCESS [conn171] Successfully authenticated as principal XX on XX
2018-11-16T10:21:16.486+0100 I NETWORK [thread1] connection accepted from XXX:59698 #172 (6 connections now open)
2018-11-16T10:21:16.486+0100 I NETWORK [conn172] received client metadata from XX:59698 conn172: { driver: { name: "MongoDB Internal Client", version: "3.4
.9" }, os: { type: "Linux", name: "Red Hat Enterprise Linux Server release 6.8 (Santiago)", architecture: "x86_64", version: "Kernel 2.6.32-642.15.1.el6.x86_64" } }
2018-11-16T10:21:16.522+0100 I ACCESS [conn172] Successfully authenticated as principal XX on XX
2018-11-16T10:21:16.523+0100 I NETWORK [thread1] connection accepted from XXX:59702 #173 (7 connections now open)
2018-11-16T10:21:16.524+0100 I NETWORK [conn173] received client metadata from XX:59702 conn173: { application: { name: "MongoDB Shell" }, driver: { name:
"MongoDB Internal Client", version: "3.4.9" }, os: { type: "Linux", name: "Red Hat Enterprise Linux Server release 6.8 (Santiago)", architecture: "x86_64", version: "
Kernel 2.6.32-642.15.1.el6.x86_64" } }
**2018-11-16T10:21:16.528+0100 I ACCESS [conn173] Successfully authenticated as principal XX on XX
2018-11-16T10:21:16.533+0100 I ACCESS [conn173] Successfully authenticated as principal XX on XX**
2018-11-16T10:21:16.538+0100 I ACCESS [conn173] Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings" }
2018-11-16T10:21:16.541+0100 I ACCESS [conn173] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }
2018-11-16T10:21:22.190+0100 I -[conn173] end connection XXX:59702 (7 connections now open)
2018-11-16T10:21:22.195+0100 I -[conn171] end connection 127.0.0.1:46682 (6 connections now open)
2018-11-16T10:21:22.195+0100 I -[conn172] end connection XXX:59698 (6 connections now open)
With mongoshell i've "Successfully authenticated as principal" before execute the createIndexes.
So why it's the case with spring boot ?
I dont have error without replicaset.
Comment From: wilkinsona
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: javaHelper
Does anyone knows how to fix this issue ?