Authentication failure via API and Compass

  1. I have standalone MongoDB on RHEL 8 with all default configuration.
  2. Accessed DB via Compass and API works fine.
  3. then created user with super admin role, enabled security in mongod.conf, restarted DB.
  • User created using below:
    db.createUser({user: "myusername", pwd: "mypassword",roles:[{role: "userAdminAnyDatabase" , db:"admin"},{ role: "readWriteAnyDatabase", db: "admin" }]})

  • Using the above connection string:
    conn_str = "mongodb://myusername:mypassword@{my_hostname}:27017/?authSource=admin"

Authentication failure when tried both via Compass or API. However I am able to login using mongosh with same user and password for the user created.

What am I missing here? Any help is much appreciated.

Does your password has any special characters?
If yes you have to escape those characters
If it is working in shell means you might have enclosed password in quotes

No I don’t have any special character, its just alpha numeric.

I have tried using it in quotes, no luck.

Post screenshots of every steps that you take. The exact context of what you do often reveals the issue more that just writing that it does not work.

If you can connect with myusername and mypassword using mongosh then the server is okay and the user is okay. So it you cannot connect via Compass or API then there is something you are doing wrong. For example, do you use

in Compass or the text between the double quotes? Do you really use braces around my_hostname or not? The value of host_name must match the bindIp from your configuration file. Since you

then may be both myhostname and {myhostname} are wrong in you connection string. Because, if I remember correctly, by default mongod only listen to 127.0.0.1 or localhost. Post screenshot that shows how you connect with mongosh. Most likely you simply type mongosh so you connect to localhost:27017 rather than {myhostname}:27017.

1 Like

here are screenshot @steevej

The only thing I can see is that you are most likely trying to connect to a different mongod.

I think that what ever address you are specifying in Compass (redacted in red) is not routed to the same host as your local host. Try localhost in Compass rather than the other address you are hiding.

@steevej mongod is installed on one of the RHEL server. It’s not my localhost. I am using the IP for RHEL server. I can connect compass to the same IP if I disable security in mongo.conf

Then the user is you are trying to connect with is not configured correctly on the RHEL server. So I wonder why you bother showing us that you can connect with that user on your local host. Unless of course, you are remotely connected to your RHEL server. You need to show us that you can connect with mongo or mongosh from the same machine that you try to connect with Compass.

I am starting to suspect that you have created the user in the wrong database.

Try the following:

  1. connect to your local host instance (presuming it is the RHEL server) like you did in the screenshot where you run db.auth().
  2. run and share the output of the command db.getUsers()
  3. run the command use admin
  4. and finally do like step 2, run and share the output of the command db.getUsers().

What I suspect is that the first db.getUsers() will list a user with _id:“test.mongosuperadmin” while the second one will produce an empty list.

You are right i get empty list when i switch to admin DB. But get bunch of list of users without doing switch. So I wonder where are these users getting created?

i get it they are getting on some test DB. Let me create create user in admin and try signing in Compass. I hope it works

How ever i used below query to create user which says specifically admin DB.

db.createUser({user: “mongosuperadmin”, pwd: “qFukZfzZlm8n9l”, roles:[{role: “userAdminAnyDatabase” , db:“admin”},{ role: “readWriteAnyDatabase”, db: “admin” }]})

I am able to connect now, thanks @steevej . But i did not get even after mentioning to create users in specific DB why the users got created in test DB

Users are created in the current database. When you connect with mongo and mongosh the current database is test. To create the users in the admin you have to run the command use admin first.

The course M103 from MongoDB University covers some of the aspects and M150 goes a little bit further.

1 Like

Please kindly mark one of my post as the solution.

Ad-Thanks-vance

1 Like