Another possibility: When you created the user, you may have accidentally been use
ing a database other than admin
, or other than the one you wanted. You need to set --authenticationDatabase
to the database that the user was actually created under.
mongodb
seems to put you in the test
database by default when you open the shell, so you'd need to write --authenticationDatabase test
rather than --authenticationDatabase admin
if you accidentally were use
ing test
when you ran db.createUser(...)
.
Assuming you have access to the machine that's running the mongodb instance, y could disable authorization in /etc/mongod.conf
(comment out authorization
which is nested under security), and then restart your server, and then run:
mongo
show users
And you might get something like this:
{
"_id" : "test.myusername",
"user" : "myusername",
"db" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "mydatabasename"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
Notice that the db
value equals test
. That's because when I created the user, I didn't first run use admin
or use desiredDatabaseName
. So you can delete the user with db.dropUser("myusername")
and then create another user under your desired database like so:
use desiredDatabaseName
db.createUser(...)
Hopefully that helps someone who was in my position as a noob with this stuff.
db.changeUserPassword("admin", "password")
to change password for each databases. – laggingreflex