Query failed with error code 13 and error message 'not authorized on'

Viewed 15

I have developed a code using Spring boot. It was already communicating with my database without any problem, but it is not working anymore since I have used username and password for MongoDB. Here is my URI string:

uri: mongodb://username:mypassword@localhost:27017/mainDatabase?authSource=admin&retryWrites=true&w=majority

To set the password for MongoDB, I did the following:

  1. Changed the .conf file and added:

security: authorization: enabled 2) Created a user in the admin databases with access to any database. 3) restarted the mongodb server.

Also, I should mention that using mongodb Compass, I can connect to the database using the following string:

mongodb://username:password@localhost:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false

Could you please tell me what is wrong with my code? When I run the code, I get:

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'not authorized on mainDatabase to execute command  ...' on server localhost:27017

In the meantime, I have read all question on stackoverflow with similar errors. But none of them has helped. Actually, according to the answers to the other similar questions, my uri is already correct and it should work. I even tried adding other parameters like ssl = false, authMechanism=SCRAM-SHA-1. But none of them helped. Best Regards,

0 Answers
Related