Evanss
Evanss

Reputation: 23593

See / change local username and password for MongoDB?

Ive downloaded a project using MongoDB and im having trouble getting set up. From the terminal where ive run mongod I see this error:

2017-05-26T14:51:22.908+0800 I ACCESS   [conn21] SCRAM-SHA-1 authentication failed for user on wesbostest from client 127.0.0.1:51653 ; UserNotFound: Could not find user user@wesbostest

From my npm start terminal mongoose logs out this error: Authentication failed.

My environment file has this line:

DATABASE=mongodb://user:pass@localhost:27017/wesbostest

Ive got MongoDB Compass installed. It successfully connects with these settings:

Hostname: localhost
Port: 27017
Authentication: None
SSL: Off
SSH Tunnel: Off

So I think the user:pass part of the environment file is wrong? How can I see what local username and password are and/or set them if no authentication is set up?

Upvotes: 2

Views: 17451

Answers (1)

Evanss
Evanss

Reputation: 23593

I solved this by setting a username and password for MongoDB:

MongoDB what are the default user and password?

Procedure

  1. Start MongoDB without access control.

    mongod --port 27017 --dbpath /data/db1
    
  2. Connect to the instance.

    mongo --port 27017
    
  3. Create the user administrator.

    use admin
    db.createUser(
      {
        user: "myUserAdmin",
        pwd: "abc123",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )
    
  4. Re-start the MongoDB instance with access control.

    mongod --auth --port 27017 --dbpath /data/db1
    
  5. Authenticate as the user administrator.

    mongo --port 27017 -u "myUserAdmin" -p "abc123" \
      --authenticationDatabase "admin"
    

Upvotes: 3

Related Questions