I know there are lot of articles with the same issue and I explored every other article trying everything and still this error persists.
Here are the steps I followed:
use admin.
created an admin user with role: dbAdminAnyDatabase
use mydb.
created an admin user with different name for mydb
created a readonly user with:
db.createUser({user: "nameuser", pwd: "password", roles: [{role: "read", db: "mydb"}]});
setup a mongod.conf file with:
security: authorization: enabled
net: port: 12345 bindIp: 0.0.0.0 #default value is 127.0.0.1
then run the command:
sudo mongod --port 27017 --dbpath ~/data/mongodb --config /etc/mongod.conf
and then on localhost i run the command to check:
mongo -u nameuser -p password localhost:12345/mydb
then the error come as:
connecting to: mongodb://localhost:12345/mydb MongoDB server version: 3.6.3 2020-10-04T03:05:37.054+0530 E QUERY [thread1] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20 @(auth):6:1 @(auth):1:2 exception: login failed
and on server side the error looks like this:
2020-10-04T02:53:11.835+0530 I NETWORK [listener] connection accepted from 127.0.0.1:60240 #3 (1 connection now open) 2020-10-04T02:53:11.835+0530 I NETWORK [conn3] received client metadata from 127.0.0.1:60240 conn: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.6.3" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "18.04" } } 2020-10-04T02:53:11.836+0530 I ACCESS [conn3] SCRAM-SHA-1 authentication failed for nameuser on mydb from client 127.0.0.1:60240 ; UserNotFound: Could not find user nameuser@mydb 2020-10-04T02:53:11.837+0530 I NETWORK [conn3] end connection 127.0.0.1:60240 (0 connections now open)
Now please tell me if I can try anything new. I have tried so many articles from stackoverflow but none address the issue. Every resolved issue is the common mistake which I have checked already and I am not doing any of these. Tell me if you have any suggestion to work this out.
db.getUsers()
in each database to see which user is where.