Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the user's password in MongoDB

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to change the user's password in MongoDB. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Log in to the main database of MongoDB database cluster to create super administrative users

Db.createUser (

{

User: "firstset"

Pwd: "firstset"

Roles: [{role: "userAdminAnyDatabase", db: "admin"}]

}

);

2. Change the password of zhul, check the mogodb process, and pay attention to the port number

Ps-ef | grep mongod

Mongo 10836 10 09:02? 00:00:03 mongod-- dbpath / opt/mongo/data/dns_repset1-- port 10001-- replSet firstset-- oplogSize 512-- rest-- fork-- logpath / opt/mongo/logs/firstset/firstset.log-- logappend-- nojournal-- directoryperdb-- keyFile / opt/mongo/keyfile/keyfile

Mongo 10997 9767 0 09:09 pts/3 00:00:00 grep-color=auto mongod

3. Change the password of zhul to check whether the current database is secret-free.

[mongo@mongo1 keyfile] $mongo-- port 10001

MongoDB shell version: 3.2.11-49-g52b68fa

Connecting to: 127.0.0.1:10001/test

Firstset:PRIMARY > show dbs

2019-10-24T09:09:31.298+0800 E QUERY [thread1] Error: listDatabases failed: {

"ok": 0

"errmsg": "not authorized on admin to execute command {listDatabases: 1.0}"

"code": 13

}:

_ getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

ShellHelper.show@src/mongo/shell/utils.js:761:19

ShellHelper@src/mongo/shell/utils.js:651:15

@ (shellhelp2): 1:1

Firstset:PRIMARY > exit

Bye

Through verification, the current database has tried the keyfile security authentication check, and secret-free login will refuse to execute any commands.

4. Change the password of zhul. If you know the password of the user being changed, you can log in with your account password, or you can cancel the security restrictions on keyfile.

After the secret-free login changes the passwords of the relevant users, enable keyfile security authentication

[mongo@mongo1 keyfile] $mongo-u firstset-p firstset-- port 10001

MongoDB shell version: 3.2.11-49-g52b68fa

Connecting to: 127.0.0.1:10001/test

Server has startup warnings:

2019-10-24T09:02:45.827+0800 I CONTROL [main] * * WARNING:-- rest is specified without-- httpinterface

2019-10-24T09:02:45.827+0800 I CONTROL [main] * * enabling http interface

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: The server is started with the web server interface and access control.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * The web interfaces (rest, httpinterface and/or jsonp) are insecure

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * and should be disabled unless required for backward compatibility.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/enabled is' always'.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/defrag is' always'.

2019-10-24T09:02:46.024+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'

2019-10-24T09:02:46.024+0800 I CONTROL [initandlisten]

Firstset:PRIMARY > show dbs

Admin 0.000GB

Dns_testdb 0.004GB

Local 0.008GB

Firstset:PRIMARY > db.updateUser ("firstset", {pwd: "rootroot"})

Firstset:PRIMARY > exit

Bye

5. Verify password modification

-- wrong user name or password login denied

[mongo@mongo1 keyfile] $mongo-u firstset-p firstset-- port 10001

MongoDB shell version: 3.2.11-49-g52b68fa

Connecting to: 127.0.0.1:10001/test

2019-10-24T09:11:35.167+0800 E QUERY [thread1] Error: Authentication failed. :

DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20

@ (auth): 6:1

@ (auth): 1:2

Exception: login failed

-- the operation can be performed normally after logging in with the correct user name and password

[mongo@mongo1 keyfile] $mongo-u firstset-p rootroot-- port 10001

MongoDB shell version: 3.2.11-49-g52b68fa

Connecting to: 127.0.0.1:10001/test

Server has startup warnings:

2019-10-24T09:02:45.827+0800 I CONTROL [main] * * WARNING:-- rest is specified without-- httpinterface

2019-10-24T09:02:45.827+0800 I CONTROL [main] * * enabling http interface

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: The server is started with the web server interface and access control.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * The web interfaces (rest, httpinterface and/or jsonp) are insecure

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * and should be disabled unless required for backward compatibility.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/enabled is' always'.

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten]

2019-10-24T09:02:46.023+0800 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/defrag is' always'.

2019-10-24T09:02:46.024+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'

2019-10-24T09:02:46.024+0800 I CONTROL [initandlisten]

Firstset:PRIMARY > show dbs

Admin 0.000GB

Dns_testdb 0.004GB

Local 0.008GB

Firstset:PRIMARY > use admin

Switched to db admin

Firstset:PRIMARY > show collections

System.users

System.version

Firstset:PRIMARY > db.system.users.find ()

{"_ id": "test.firstset", "user": "firstset", "db": "test", "credentials": {"SCRAM-SHA-1": {"iterationCount": 10000, "salt": "ISdrb4a3Cc0A59vXEUxjOg==", "storedKey": "qPn44VuZrJ6QwWzOMBq90vZ5eAo=", "serverKey": "rz+CDSlpXHKvUDGg0PCnG2GZCjk="}}, "roles": [{"role": "root" "db": "admin"}]}

Firstset:PRIMARY > use dns_testdbuse dns_testdb

Switched to db dns_testdb

Firstset:PRIMARY > show collections

Test_collection

Firstset:PRIMARY > db.test_collection.findOne ()

{

"_ id": ObjectId ("5d8434a5b138ddafc446e13b")

"name": "cow"

"user_id": 48149

"boolean": false

Added_at: ISODate ("2019-09-20T02:08:37.116Z")

"number": 1743

}

Firstset:PRIMARY >

Firstset:PRIMARY > rs.status ()

{

"set": "firstset"

Date: ISODate ("2019-10-24T01:13:15.006Z")

"myState": 1

"term": NumberLong (7)

"heartbeatIntervalMillis": NumberLong (2000)

"members": [

{

"_ id": 0

"name": "192.168.192.251purl 10001"

"health": 1

"state": 1

"stateStr": "PRIMARY"

"uptime": 630

"optime": {

Ts: Timestamp (1571879490, 1)

"t": NumberLong (7)

}

OptimeDate: ISODate ("2019-10-24T01:11:30Z")

ElectionTime: Timestamp (1571879010, 1)

ElectionDate: ISODate ("2019-10-24T01:03:30Z")

"configVersion": 1

"self": true

}

{

"_ id": 1

"name": "192.168.192.252VR 10001"

"health": 1

"state": 2

"stateStr": "SECONDARY"

"uptime": 566

"optime": {

Ts: Timestamp (1571879490, 1)

"t": NumberLong (7)

}

OptimeDate: ISODate ("2019-10-24T01:11:30Z")

LastHeartbeat: ISODate ("2019-10-24T01:13:13.320Z")

LastHeartbeatRecv: ISODate ("2019-10-24T01:13:13.259Z")

"pingMs": NumberLong (0)

"syncingTo": "192.168.192.251purl 10001"

"configVersion": 1

}

{

"_ id": 2

"name": "192.168.192.250) 10001"

"health": 1

"state": 7

"stateStr": "ARBITER"

"uptime": 593

LastHeartbeat: ISODate ("2019-10-24T01:13:13.301Z")

LastHeartbeatRecv: ISODate ("2019-10-24T01:13:13.185Z")

"pingMs": NumberLong (0)

"configVersion": 1

}

]

"ok": 1

}

Firstset:PRIMARY >

The above is how to change a user's password in MongoDB. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report