search

Home  >  Q&A  >  body text

MongoServerError: Authentication failed on Ubuntu

I installed Mongodb on Ubuntu 22.04. I use this line of code to setup admin on Mongodb

test\> use admin

Switch to database administrator

admin\> db.createUser({user:"john", pwd:"alx", roles:\[{role:"root", db:"admin"}\]})

{ ok: 1 }

Enable MongoDB authentication in /lib/systemd/system/mongod.service by adding this line of code

ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Then I run this command

mongosh -u demo -p --authenticationDatabase admin

This error occurs after entering alx as password

enter password:

Current Mongosh log ID: 64af5b02457d8abc538e7e4f

Connect to: mongodb://@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh 1.10.1

MongoServerError: Authentication failed

I try to log in to the Mongodb database via authentication

P粉627427202P粉627427202443 days ago528

reply all(1)I'll reply

  • P粉035600555

    P粉0356005552023-09-14 10:10:59

    1. Please use the correct username
    1. Please check the mongo version, I think the root role does not include any access to collections in versions prior to 3.07. If it is a previous version, you can use the following command.
    use admin
        db.addUser( { user: "<username>",
                  pwd: "<password>",
                  roles: [ "userAdminAnyDatabase",
                           "dbAdminAnyDatabase",
                           "readWriteAnyDatabase"
        ] } )

    reply
    0
  • Cancelreply