Add user name and password to mongodb database and reset forgotten password

One, add username and password

1. Run mongo after connecting to the remote server (or locally)

mongo

2. Configure the highest authority account password for mongodb

use admin
db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin"}]})

The actual operation result is as follows, successfully adding the admin user

3. Turn on the modification permissions, admin and 123456 are both the previous configuration

db.auth( "admin","123456")    

4. Add a user name and password to the specific database, take the blog database as an example

use blog
db.createUser({user:'blog',pwd:'123456',roles: [{role:'readWrite',db:'blog'}]})

The actual operation result is as follows, the blog user is successfully added

5.mongoos connect to mongodb database with password

mongoose.connect('mongodb://username:password@host:port/database')
//blog为例
mongoose.connect('mongodb://blog:123456@localhost:27017/blog')

 

2. Forgot your password to reset your password 

1. Find the configuration file of mongodb and comment out auth

Find the configuration file mongod.conf of mongodb through ps -ef|grep mongod

The configuration file can also be found through find / -name mongod.conf

I am directly modifying the configuration file of mongodb in the pagoda panel

2. Run mongo

No password is needed at this time, but there may be a series of warnings as follows, don’t worry about it

3. View and delete all users 

use admin 
db.system.users.find()
db.system.users.remove({})

The actual operation result is as follows, successfully deleted all users 

4. The first item of the reset operation has already been described and will not be repeated.

Guess you like

Origin blog.csdn.net/a1059526327/article/details/106878607