1. 创建一个超级用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
use admin
db.createUser(
  {
    user: "adminUserName",
    pwd: "userPassword",
    roles:
    [
      {
        role: "userAdminAnyDatabase",
        db: "admin"
      }
    ]
  }
)

超级用户的role有两种,userAdmin或者userAdminAnyDatabase(比前一种多加了对所有数据库的访问)。

db是指定数据库的名字,admin是管理数据库。

2. 用新创建的用户登录

1
mongo --host xxx -u adminUserName -p userPassword --authenticationDatabase admin

不能用admin数据库中的用户登录其他数据库

3. 查看当前用户的权限

1
2
3
4
5
6
db.runCommand(
  {
    usersInfo:"userName",
    showPrivileges:true
  }
)

注:只能查看当前数据库中的用户,哪怕当前数据库admin数据库,也只能查看admin数据库中创建的用户。

4. 创建一般用户,也是用createUser

1
2
3
4
5
6
7
8
9
10
11
12
use db01
db.createUser(
  {
    user:"oneUser",
    pwd:"12345",
    roles:[
      {role:"read",db:"db01"},
      {role:"read",db:"db02"},
      {role:"read",db:"db03"}
    ]
  }
)

5. 创建一个不受访问限制的超级用户

1
2
3
4
5
6
7
8
use admin
db.createUser(
  {
    user:"superuser",
    pwd:"pwd",
    roles:["root"]
  }
)

6. 修改密码

1
2
use admin
db.changeUserPassword("username", "xxx")

7. 查看用户信息

1
db.runCommand({usersInfo:"userName"})

8. 修改密码和用户信息

1
2
3
4
5
6
7
db.runCommand(
  {
    updateUser:"username",
    pwd:"xxx",
    customData:{title:"xxx"}
  }
)

注:

1. 和用户管理相关的操作基本都要在admin数据库下运行,要先use admin;

2. 如果在某个单一的数据库下,那只能对当前数据库的权限进行操作;

3. db.addUser是老版本的操作,现在版本也还能继续使用,创建出来的user是带有root role的超级管理员。

最新文章

  1. Android之debug---menu的getActionView()return null
  2. JavaScript数组的方法
  3. Hadoop概括——学习笔记<一>
  4. We Know What @You #Tag: Does the Dual Role Affect Hashtag Adoption-20160520
  5. SQLServer如何删除字段中的某个字符串,或者替换为空格?
  6. java thread reuse(good)
  7. 16. leetcode 404. Sum of Left Leaves
  8. Hello World -- 第一篇博客
  9. Android ANR异常解决方案
  10. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
  11. Add AI feature to Xamarin.Forms app
  12. Python-接口自动化(六)
  13. 从零开始编译Poco C++和VS2015环境配置
  14. Unable to docker login through CLI - unauthorized: incorrect username or password
  15. Linux 中用 dd 命令来测试硬盘读写速度
  16. 如何把开源项目发布到Jcenter
  17. inet超级服务器和守护进程
  18. 设置一个div网页滚动时,使其固定在头部,当页面滚动到距离头部300px时,隐藏该div,另一个div在底部,此时显示;当页面滚动到起始位置时,头部div出现,底部div隐藏
  19. 监督学习——AdaBoost元算法提高分类性能
  20. HTML5 Geolocation API地理定位整理(一)

热门文章

  1. 无法重新组织表 "ty_wf_ex_local_process_info" 的索引 "idx_prc_act_id" (分区 1),因为已禁用页级锁定。
  2. Codeforces 132E Bits of merry old England 【最小费用最大流】
  3. sqoop安装
  4. 列出当前ARM开发板系统加载的模块
  5. sopcinfo路径改变,nios工程该怎么办?
  6. 共享池之八:软解析、硬解析、软软解析 详解一条SQL在library cache中解析涉及的锁
  7. Android——Dialog
  8. OC基础(26)
  9. autoit UIA获取Listview的信息
  10. iOS7 iOS8 UITableviewCell处于编辑状态,dismiss或者back崩溃