{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}) step3:有验证启动mongod服务 mongod --auth --port 27017 --dbpath /data/db1 step4:管理员mongo登录后新增用户,分配权限 mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin" use testdb.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}) step5:授权用户登录并操作表 mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test" db.foo.insert( { x: 1, y: 1 } ) 注:无权限访问时,将报如下错误
参考: https://docs.mongodb.com/v3.2/tutorial/enable-authentication/ 后语: 以上仅供对mongodb进行简单管理,不至于被外界直接用端口进行连接。但更系统的权限管理还请参考官网说明 https://docs.mongodb.com/v3.2/tutorial/manage-users-and-roles/