网站首页 文章专栏 mongodb开启认证创建用户的正确方式Localhost Exception机制
mongodb开启认证创建用户的正确方式Localhost Exception机制
编辑时间:2019年12月14日 18:36 作者:赵彦昌 浏览量:2732

不得不说,搞两年mongodb 了,也是今天才知道。mongodb 竟然有这么一个机制叫 LocalHost Exception 


全网得mongodb 部署教程,都是千篇一律的让我们先关闭 认证(auth) 去创建用户再打开认证去启动。但官网

已经给了我们解决这一脱裤子放屁的功能,那就是"LocalHost Exception" 机制。


先看官网的解释


The localhost exception allows you to enable access control and then create the first user in the system. With the localhost exception, after you enable access control, connect to the localhost interface and create the first user in the admin database. The first user must have privileges to create other users, such as a user with the userAdmin or userAdminAnyDatabase role.


译文:


LocalHost Exception允许您在启用访问控制的同时,在系统中创建第一个用户。对于LocalHost Exception,启用访问控制后,连接到localhost接口并在 admin 数据库中创建第一个用户。第一个用户必须具有创建其他用户的权限,例如具有userAdminuserAdminAnyDatabase角色权限的用户。


大意是,这个机制可以在mongodb 开启 auth 参数的时候,使用 shell 执行 mongo --host localhost --port 27017 在 admin 数据库创建一个用户。在创建一个用户后,该机制就会失效。后续的操作请再打开一个终端。用刚才创建的用户想怎么连接就连接去执行其它需要相应权限的操作。


这一机制适合,单机实例、副本集、分片集群


  • 副本集需要在主节点上执行
  • 占位符
  • 分片集群可以通过任一 mongos  路由执行



来说两句吧
最新评论