zoukankan      html  css  js  c++  java
  • MongoDB和Java(7):MongoDB用户管理

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装、客户端操作、安全认证、副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很大。特此记录,以备查看。

    文章目录:

    MongoDB和Java(1):Linux下的MongoDB安装

    MongoDB和Java(2):普通用户启动mongod进程

    MongoDB和Java(3):Java操作MongoB

    MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    MongoDB和Java(5):Spring Data整合MongoDB(注解配置)

    MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群

    MongoDB和Java(7):MongoDB用户管理

    本文记录如何开启MongoDB认证、添加用户

    1、MongoDB权限管理简单说明

    MongoDB中的每个数据库有一些用户(得创建),这些用户有的只能操作自己所属库的表,有的可以操作其他库的表,这取决于它拥有的角色。

    一个用户可以有多个角色,角色包含若干权限,权限又拥有资源、操作。

    简单来说就是 用户 — 角色 — 权限 的权限管理体系。

    关于角色、权限、资源等官网有文档:
    https://docs.mongodb.com/manual/reference/built-in-roles/
    https://docs.mongodb.com/manual/reference/resource-document/
    https://docs.mongodb.com/manual/reference/privilege-actions/

    权限资源就不做详细介绍了,因为创建用户使用内置角色就足够了,不太可能去自己去创建角色

    看一下内置角色

    Database User Roles
    read、readWrite

    Database Administration Roles
    dbAdmin、dbOwner、userAdmin,其中dbOwner权限最高

    Cluster Administration Roles
    clusterAdmin、clusterManager、clusterMonitor、hostManager

    Backup and Restoration Roles
    backup、restore

    All-Database Roles
    readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase

    Superuser Roles
    root

    2、创建超级管理员

     1 [root@xugf-test4 ~]# mongo
     2 > use admin
     3 switched to db admin
     4 > db.createUser({
     5 ... user: "admin",
     6 ... pwd: "123456",
     7 ... roles: [{role: "root", db: "admin"}],
     8 ... mechanisms: ["SCRAM-SHA-1"]
     9 ... })
    10 Successfully added user: {
    11         "user" : "admin",
    12         "roles" : [
    13                 {
    14                         "role" : "root",
    15                         "db" : "admin"
    16                 }
    17         ],
    18         "mechanisms" : [
    19                 "SCRAM-SHA-1"
    20         ]
    21 }

    修改mongo.conf配置文件,开启权限认证功能,auth属性设置true

    1 [mongo@xugf-test4 ~]$ cat /etc/mongo.conf 
    2 dbpath=/data/mongo/db/
    3 logpath=/data/mongo/log/mongodb.log
    4 bind_ip_all=true
    5 fork=true
    6 auth=true

    重启mongodb

    3、身份认证的两种方式

    再使用mongo连接,进行操作时会提示未认证

     1 [root@xugf-test4 ~]# mongo
     2 > show dbs
     3 2018-10-12T10:16:00.683+0800 E QUERY    [js] Error: listDatabases failed:{
     4         "ok" : 0,
     5         "errmsg" : "command listDatabases requires authentication",
     6         "code" : 13,
     7         "codeName" : "Unauthorized"
     8 } :
     9 _getErrorWithCode@src/mongo/shell/utils.js:25:13
    10 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
    11 shellHelper.show@src/mongo/shell/utils.js:876:19
    12 shellHelper@src/mongo/shell/utils.js:766:15
    13 @(shellhelp2):1:1

    此时,有两种方式进行客户端认证:
    1)在连接时使用--authenticationDatabase选项指定认证数据库,使用-u选项指定用户名,使用-p指定密码

    1 [root@xugf-test4 ~]# mongo -u admin -p --authenticationDatabase admin
    2 MongoDB shell version v4.0.2
    3 Enter password:
    4 MongoDB server version: 4.0.2
    5 > show dbs
    6 admin   0.000GB
    7 config  0.000GB
    8 local   0.000GB

    2)在连接后切换到认证数据库后,使用db.auth("username", "password")进行认证

     1 [root@xugf-test4 ~]# mongo
     2 MongoDB shell version v4.0.2
     3 connecting to: mongodb://127.0.0.1:27017
     4 MongoDB server version: 4.0.2
     5 > use admin
     6 switched to db admin
     7 > db.auth("admin", "123456")
     8 1
     9 > show dbs
    10 admin   0.000GB
    11 config  0.000GB
    12 local   0.000GB

    4、添加数据库管理员

    给test库添加一个数据库管理员testAdmin

     1 > use test
     2 switched to db test
     3 > db.createUser({
     4 ... user: "testAdmin",
     5 ... pwd: "123456",
     6 ... roles: [{role: "dbOwner", db: "test"}],
     7 ... mechanisms: ["SCRAM-SHA-1"]
     8 ... })
     9 Successfully added user: {
    10         "user" : "testAdmin",
    11         "roles" : [
    12                 {
    13                         "role" : "dbOwner",
    14                         "db" : "test"
    15                 }
    16         ],
    17         "mechanisms" : [
    18                 "SCRAM-SHA-1"
    19         ]
    20 }

    使用testAdmin连接

     1 [root@xugf-test4 ~]# mongo -u testAdmin -p --authenticationDatabase test
     2 > db
     3 test
     4 > db.getUsers()
     5 [
     6         {
     7                 "_id" : "test.testAdmin",
     8                 "user" : "testAdmin",
     9                 "db" : "test",
    10                 "roles" : [
    11                         {
    12                                 "role" : "dbOwner",
    13                                 "db" : "test"
    14                         }
    15                 ],
    16                 "mechanisms" : [
    17                         "SCRAM-SHA-1"
    18                 ]
    19         }
    20 ]

    5、添加数据库读写用户

    给test库添加一个xugf用户

     1 > use test
     2 switched to db test
     3 > db.createUser({
     4 ... user: "xugf",
     5 ... pwd: "123456",
     6 ... roles: [{role: "readWrite", db: "test"}],
     7 ... mechanisms: ["SCRAM-SHA-1"]
     8 ... })
     9 Successfully added user: {
    10         "user" : "xugf",
    11         "roles" : [
    12                 {
    13                         "role" : "readWrite",
    14                         "db" : "test"
    15                 }
    16         ],
    17         "mechanisms" : [
    18                 "SCRAM-SHA-1"
    19         ]
    20 }

    使用xugf连接

    1 [root@xugf-test4 ~]# mongo -u xugf -p 123456 --authenticationDatabase test
    2 MongoDB shell version v4.0.2
    3 connecting to: mongodb://127.0.0.1:27017
    4 MongoDB server version: 4.0.2
    5 >
    6 > db
    7 test
  • 相关阅读:
    bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
    bzoj 2157: 旅游 (LCT 边权)
    bzoj 3669: [Noi2014]魔法森林 (LCT)
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
    bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
    bzoj 3282: Tree (Link Cut Tree)
    模拟赛1
    Some tricks
    模拟赛简要题解与心得
    Noip 训练指南
  • 原文地址:https://www.cnblogs.com/xugf/p/9777140.html
Copyright © 2011-2022 走看看