MongoDB shell version v3.4.24
WARNING: Access control is not enabled for the database.
Read and write access to data and configuration is unrestricted.
1)未启用访问控制
2)读写访问不受限制
D:\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2023-11-29T16:56:25.314+0800 I CONTROL [initandlisten]
2023-11-29T16:56:25.314+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-11-29T16:56:25.314+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2023-11-29T16:56:25.314+0800 I CONTROL [initandlisten]
>
> db
test
>
>
MongoDB: Server has startup warnings ''Access control is not enabled for the database'' - Stack Overflow
Mongodb v3.4
You need to do the following to create a secure database:
Make sure the user starting the process has permissions and that the directories exist (/data/db in this case).
1) Start MongoDB without access control.
mongod --port 27017 --dbpath /data/db
2) Connect to the instance.
mongo --port 27017
3) Create the user administrator (in the admin authentication database).
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/db
5) Connect and authenticate as the user administrator.
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
6) Create additional users as needed for your deployment (e.g. in the test authentication database).
use test
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)
7) Connect and authenticate as myTester.
mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"
I basically just explained the short version of the official docs here: https://docs.mongodb.com/master/tutorial/enable-authentication/