Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Establish a connection to MongoDB via mongo CLI - See Connect to MongoDB with mongo CLI

  2. The user information is configured globally for the entire MongoDB within the admin DB, so you have to switch to this DB.
    List existing dbs with show dbs command

    No Format
    PS C:\Users\XXX> mongo --authenticationDatabase admin -u bvq -p P@ssw0rd
    MongoDB shell version v3.4.4
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.4
    > show dbs
    admin       0.000GB
    bvq         0.401GB
    local       0.000GB
  3. Switch to admin db with use admin

    No Format
    > use admin
    switched to db admin
  4. Use db.changeUserPassword() to change the password for the user logged in

    No Format
    db.changeUserPassword("bvq", "NEW_PASSWORD")

    In this example password is changed to NEW_PASSWORD for user bvq

  5. After executing this command the password will be changed

    No Format
    PS C:\Users\XXX> mongo --authenticationDatabase admin -u bvq -p P@ssw0rd
    MongoDB shell version v3.4.4
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.4
    2017-12-05T18:50:07.771+0100 E QUERY    [thread1] Error: Authentication failed. :
    DB.prototype._authOrThrow@src/mongo/shell/db.js:1459:20
    @(auth):6:1
    @(auth):1:2
    exception: login failed
    PS C:\Users\fschenke> mongo --authenticationDatabase admin -u bvq -p NEW_PASSWORD
    MongoDB shell version v3.4.4
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.4
    > exit
    bye
    PS C:\Users\XXX>

List existing user

...

  1. Establish a connection to MongoDB via mongo CLI - See Connect to MongoDB with mongo CLI

  2. Switch to admin db with use admin

    No Format
    > use admin
    switched to db admin
  3. Use db.getUsers() to list existing users

    No Format
    > db.getUsers()
    [
            {
                    "_id" : "admin.bvq",
                    "user" : "bvq",
                    "db" : "admin",
                    "roles" : [
                            {
                                    "role" : "root",
                                    "db" : "admin"
                            }
                    ]
            }
    ]