mongodb 添加用户名密码 创建管理员 use admin db.createUser( { user: "admin", pwd: "admin", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) 重启MongoDB实例 mongod --auth --port 27017 --dbpath <关联路径> 连接并认证 mongo --port 27017 -u "admin" -p "admin" --authenticationDatabase "admin" 添加额外权限用户 use test db.createUser( { user: "test", pwd: "test123", roles: [ { role: "readWrite", db: "test" } ] } ) 连接并认证 mongo --port 27017 -u "test" -p "test123" --authenticationDatabase "test" 注意:如果是配置文件/etc/mongod.conf 启动的记得 在配置文件中写一行 auth=true 客户端认真 db.auth("someAdminUser", password)