更新时间:2021-10-05 23:30:29
默认情况下,mongodb没有启用访问控制,因此没有默认用户或密码.
By default mongodb has no enabled access control, so there is no default user or password.
要启用访问控制,请使用命令行选项--auth
或security.authorization配置文件设置.
To enable access control, use either the command line option --auth
or security.authorization configuration file setting.
您可以使用以下过程,或在网站上的启用身份验证中. MongoDB文档.
You can use the following procedure or refer to Enabling Auth in the MongoDB docs.
在没有访问控制的情况下启动MongoDB.
Start MongoDB without access control.
mongod --port 27017 --dbpath /data/db1
连接到实例.
Connect to the instance.
mongo --port 27017
创建用户管理员.
Create the user administrator.
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
使用访问控制重新启动MongoDB实例.
Re-start the MongoDB instance with access control.
mongod --auth --port 27017 --dbpath /data/db1
以用户管理员身份进行验证.
Authenticate as the user administrator.
mongo --port 27017 -u "myUserAdmin" -p "abc123" \
--authenticationDatabase "admin"