美文网首页
mongodb之auth

mongodb之auth

作者: SeanJX | 来源:发表于2019-07-25 17:27 被阅读0次

Mongodb 权限认证

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

在默认情况下,MongoDB 不会进行身份验证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,如果你在一个面向公网的服务器上使用它,那么这的确是一个问题。

作为数据库软件,我们肯定不想谁都可以访问,为了确保数据的安全,MongoDB 也会像其他的数据库软件一样可以采用用户验证的方法,那么该怎么做呢?其实很简单,MongoDB 提供了 addUser 方法,该方法包含三个参数:

  • user - 字符串,表示用户名

  • password - 字符串,对应的密码

  • readOnly - boolean,可选参数,默认值为 false,表示是否是只读用户

可以将 MongoDB 的用户分为两类:超级用户和数据库用户。超级用户拥有root权限,可以对所有数据库进行任意操作,超级用户储存在 admin 数据库中,刚安装的 MongoDB 中 admin 数据库是空的;数据库用户存储在单个数据库中,只能访问对应的数据库。另外用户信息保存在 db.system.users 中。

关于用户和权限有以下特性:

  1. 数据库是由超级用户来创建的,一个数据库可以包含多个用户,一个用户只能在一个数据库下,不同数据库中的用户可以同名

  2. 如果在 admin 数据库中不存在用户,即使 mongod 启动时添加了 –auth 参数,此时不进行任何认证还是可以做任何操作

  3. 在 admin 数据库创建的用户具有超级权限,可以对 MongoDB 系统内的任何数据库的数据对象进行操作

  4. 特定数据库比如 test1 下的用户 test_user1,不能够访问其他数据库 test2,但是可以访问本数据库下其他用户创建的数据

  5. 不同数据库中同名的用户不能够登录其他数据库。比如数据库 test1 和 test2 都有用户 test_user,以 test_user 登录 test1 后,不能够登录到 test2 进行数据库操作

Auth

开启登录控制并且确定一个认证机制。可以使用mongodb的默认授权机制或者第三方的框架。授权访问需要所有的clients和servers端都提供一个有效的凭证,在他们需要连接这个系统时。在集群部署时,需要开启每一个server的授权认证。
用户需要认证自身去,登录一个部署了强制认证的mongodb,并且他们只能在他们的roles定义的权限内执行动作。

  • User Administrator

tips: 开启权限控制时,请确保admin库里有一个user具有[userAdmin](https://docs.mongodb.com/manual/reference/built-in-roles/#userAdmin)``或者``[userAdminAnyDatabase](https://docs.mongodb.com/manual/reference/built-in-roles/#userAdminAnyDatabase)的权限。这个user可以管理用户和role,如创建、授权和废除用户以及创建和修改roles。

在单独的mongodb server上

  1. 开启server服务

  2. 开启client shell 直连server

  3. 创建 user administrator:


db.createUser(

{

user: "myUserAdmin",

pwd: "abc123",

roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

}

)

NOTE
The database where you create the user (in this example, admin) is the user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

  1. 重启server和client

  2. 使用认证登录:

(1)Use the command-line authentication options ([--username], [--password], and [--authenticationDatabase]) when connecting to the [mongod] or [mongos] instance
(2)Connect first to the [mongod] or [mongos] instance, and then run the [authenticate] command or the[db.auth()] method against the [authentication database]`

tips: Authenticating multiple times as different users does not drop the credentials of previously-authenticated users. This may lead to a connection having more permissions than intended by the user, and causes operations within a logical session to raise an error.

集中管理用户数据

For users created in MongoDB, MongoDB stores all user information, including [name](https://docs.mongodb.com/manual/reference/system-users-collection/#admin.system.users.user), [password](https://docs.mongodb.com/manual/reference/system-users-collection/#admin.system.users.credentials), and the [user's authentication database](https://docs.mongodb.com/manual/reference/system-users-collection/#admin.system.users.db), in the system.users collection in the admin database.

Do not access this collection directly but instead use the user management commands.

Users:

MongoDB employs role-based access control (RBAC) to determine access for users. A user is granted one or more rolesthat determine the user’s access or privileges to MongoDB resourcesand the actionsthat user can perform. A user should have only the minimal set of privileges required to ensure a system of least privilege.

Built-in Roles:

mongodb通过role授权数据获取和命令,提供了数据库系统的内建的roles来应对不同层次的获取权限。也可已创建新的自定义roles。

一个role授权已定义资源上的一系列动作。这种控制粒度甚至可以到collection级别

mongodb提供built-in的database user和database administrator roles,用于每一个数据库。内建的Admin库上使用所有的built-in roles

Database User Roles

Database Administration Roles

Cluster Administration Roles

Backup and Restoration Roles

All-Database Roles

Superuser Roles

Internal Role

配置文件

systemLog:
   # verbosity: 0  #日志等级,0-5,默认0
   # quiet: false  #限制日志输出,
   # traceAllExceptions: true  #详细错误日志
   # syslogFacility: user #记录到操作系统的日志级别,指定的值必须是操作系统支持的,并且要以--syslog启动
   path: /Users/mhq/projects/db/mongo/logs/log.txt  #日志路径。
   logAppend: false #启动时,日志追加在已有日志文件内还是备份旧日志后,创建新文件记录日志, 默认false
   logRotate: rename #rename/reopen。rename,重命名旧日志文件,创建新文件记录;reopen,重新打开旧日志记录,需logAppend为true
   destination: file #日志输出方式。file/syslog,如果是file,需指定path,默认是输出到标准输出流中
   timeStampFormat: iso8601-local #日志日期格式。ctime/iso8601-utc/iso8601-local, 默认iso8601-local
   # component: #各组件的日志级别
   #    accessControl:
   #       verbosity: <int>
   #    command:
   #       verbosity: <int>

processManagement:
   fork: true #以守护进程运行 默认false
   # pidFilePath: <string> #PID 文件位置

net:
   port: 27017 #监听端口,默认27017
   bindIp: 127.0.0.1 #绑定监听的ip,deb和rpm包里有默认的配置文件(/etc/mongod.conf)里面默认配置为127.0.0.1,若不限制IP,务必确保认证安全,多个Ip用逗号分隔
   maxIncomingConnections: 65536 #最大连接数,可接受的连接数还受限于操作系统配置的最大连接数
   wireObjectCheck: true #校验客户端的请求,防止错误的或无效BSON插入,多层文档嵌套的对象会有轻微性能影响,默认true
   ipv6: false #是否启用ipv6,3.0以上版本始终开启
   unixDomainSocket: #unix socket监听,仅适用于基于unix的系统
      enabled: false #默认true
      pathPrefix: /tmp #路径前缀,默认/temp
      filePermissions: 0700 #文件权限 默认0700
   http: #警告 确保生产环境禁用HTTP status接口、REST API以及JSON API以防止数据暴露和漏洞攻击
      enabled: false #是否启用HTTP接口、启用会增加网络暴露。3.2版本后停止使用HTTP interface
      JSONPEnabled: false #JSONP的HTTP接口
      RESTInterfaceEnabled: false #REST API接口
   # ssl: #估计用不到,所以没有自己看
   #    sslOnNormalPorts: <boolean>  # deprecated since 2.6
   #    mode: <string>
   #    PEMKeyFile: <string>
   #    PEMKeyPassword: <string>
   #    clusterFile: <string>
   #    clusterPassword: <string>
   #    CAFile: <string>
   #    CRLFile: <string>
   #    allowConnectionsWithoutCertificates: <boolean>
   #    allowInvalidCertificates: <boolean>
   #    allowInvalidHostnames: <boolean>
   #    disabledProtocols: <string>
   #    FIPSMode: <boolean>

security:
   authorization: enabled # enabled/disabled #开启客户端认证
   javascriptEnabled:  true #启用或禁用服务器端JavaScript执行
   transitionToAuth: true # 启用中间状态登录,可以使用空密码登录,auth开启是建议关闭
   # keyFile: <string> #密钥路径
   # clusterAuthMode: <string> #集群认证方式
   # enableEncryption: <boolean>
   # encryptionCipherMode: <string>
   # encryptionKeyFile: <string>
   # kmip:
   #    keyIdentifier: <string>
   #    rotateMasterKey: <boolean>
   #    serverName: <string>
   #    port: <string>
   #    clientCertificateFile: <string>
   #    clientCertificatePassword: <string>
   #    serverCAFile: <string>
   # sasl:
   #    hostName: <string>
   #    serviceName: <string>
   #    saslauthdSocketPath: <string>
   

# setParameter: #设置参数
#    <parameter1>: <value1>
#    <parameter2>: <value2>

storage:
   dbPath: /Users/mhq/projects/db/mongo/test/ #数据库,默认/data/db,如果使用软件包管理安装的查看/etc/mongod.conf
   indexBuildRetry: true #重启时,重建不完整的索引
   # repairPath: <string>  #--repair操作时的临时工作目录,默认为dbPath下的一个_tmp_repairDatabase_<num>的目录
   journal: 
      enabled: true #启动journal,64位系统默认开启,32位默认关闭
      # commitIntervalMs: <num> #journal操作的最大时间间隔,默认100或30
   directoryPerDB: false #使用单独的目录来存储每个数据库的数据,默认false,如果需要更改,要备份数据,删除掉dbPath下的文件,重建后导入数据
   # syncPeriodSecs: 60 #使用fsync来将数据写入磁盘的延迟时间量,建议使用默认值
   engine: wiredTiger #存储引擎,mmapv1/wiredTiger/inMemory 默认wiredTiger
   # mmapv1:
   #    preallocDataFiles: <boolean>
   #    nsSize: <int>
   #    quota:
   #       enforced: <boolean>
   #       maxFilesPerDB: <int>
   #    smallFiles: <boolean>
   #    journal:
   #       debugFlags: <int>
   #       commitIntervalMs: <num>
   # wiredTiger:
   #    engineConfig:
   #       cacheSizeGB: <number>  #缓存大小
   #       journalCompressor: <string> #数据压缩格式 none/snappy/zlib
   #       directoryForIndexes: <boolean> #将索引和集合存储在单独的子目录下,默认false
   #    collectionConfig:
   #       blockCompressor: <string> #集合数据压缩格式 
   #    indexConfig:
   #       prefixCompression: <boolean> #启用索引的前缀压缩
   # inMemory:
   #    engineConfig:
   #       inMemorySizeGB: <number>
 
operationProfiling: #性能分析
   slowOpThresholdMs: 100 #认定为查询速度缓慢的时间阈值,超过该时间的查询即为缓慢查询,会被记录到日志中, 默认100
   mode: off #operationProfiling模式 off/slowOp/all 默认off

# replication: #复制集相关
#    oplogSizeMB: <int>
#    replSetName: <string>
#    secondaryIndexPrefetch: <string>
#    enableMajorityReadConcern: <boolean>
# sharding: #集群分片相关
#    clusterRole: <string>
#    archiveMovedChunks: <boolean>

# auditLog:
#    destination: <string>
#    format: <string>
#    path: <string>
#    filter: <string>

# snmp:
#    subagent: <boolean> #当设置为true,SNMP作为代理运行
#    master: <boolean> #当设置为true,SNMP作为主服务器运行

# basisTech:
#    rootDirectory: <string>

相关文章

  • mongodb之auth

    Mongodb 权限认证 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富...

  • Mongodb

    忘记密码vim /etc/mongodb.conf # 修改 mongodb 配置,将 auth...

  • MongoDB

    1、windows下面安装MongoDB服务 D:\MongoDB\bin\mongod.exe -auth --...

  • MongoDB auth

  • {MongoDB} MongoDB 3.2 auth

    MongoDB创建用户时提供了各种权限,默认的内置权限参见文档:here一般常见的权限是 readWrite, u...

  • mongodb创建带auth的用户

    mongodb库是否进行auth校验是根据启动时是否开启auth为依据。如果一个库启动的时候没有开启auth,及时...

  • mongodb忘记密码的解决方法

    忘记mongodb设置的密码怎么办 修改配置文件 修改 mongodb 配置,将 auth = true 注释掉,...

  • mongodb auth认证

    mongo默认是没有密码的,这样很容易被攻击。(当然,安全的方式不只添加密码,还可以设置ip过滤,如果用的云mon...

  • MongoDB replica模式auth

    MongoDB版本更新快,且3.x版本跟2.x版本相比有很多不同点,小编这里跟大家讲一下MongoDB3.4.7版...

  • MongoDB副本集情况下如何添加验证包括SpringBoot整

    1.先把mongodb.cfg配置文件先把auth=true注释掉 2.启动mongodb并初始化副本集 3.us...

网友评论

      本文标题:mongodb之auth

      本文链接:https://www.haomeiwen.com/subject/tixkrctx.html