• MongoDB之用户与权限管理、备份与恢复管理以及客户端工具的使用


    用户、权限管理

    MongoDB默认不使用权限认证方式启动,但是需要设置权限以保证数据安全。

    内置角色

    1. 数据库用户角色:read、readWrite 
    
    2. 数据库管理角色:dbAdmin、dbOwner、userAdmin
    
    3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
    
    4. 备份恢复角色:backup、restore
    
    5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
    
    6. 超级用户角色:root	 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    创建超级管理员

    MongoDB是没有默认管理员账号,所以要先添加管理员账号,并且MongoDB服务器需要在运行的时候开启验证模式

    用户只能在用户所在数据库登录(创建用户的数据库),包括管理员账号
    
    管理员可以管理所有数据库,但是不能直接管理其他数据库,要先认证后才可以
    
    • 1
    • 2
    • 3

    语法格式:

    mongo>db.createUser(
    { user: "",
      pwd: "",
      customData: { <any information> },
      roles: [
        { role: "", db: "" } | "",
        ...
      ]}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    创建root用户,角色为root

    # 进入mongo shell
    mongod
    
    # 使用admin数据库(超级管理员账号必须创建在该数据库上)
    use admin 
    
    # 创建超级用户
    db.createUser( { "user":"root", "pwd":"123456", "roles":["root"] } )
    
    # 账号登录
    use admin
    # 认证成功会返回1,失败返回0
    db.auth('用户名','密码')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    创建普通用户

    在使用的数据库上创建普通用户

    # 选择需要创建用户的数据库
    use demo
    
    #  db: 设置访问数据库
    #  role:设置访问数据库权限:read, write, readWrite
    db.createUser( {"user": "db01", "pwd": "123456", "roles": [{"db": "demo", "role": "read"}]} )
    
    db.createUser( {"user": "db02", "pwd": "123456", "roles": [{"db": "demo", "role": "readWrite"}]} )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    认证登录

    若需要连接Mongodb进行认证登录,则需要打开Mongodb的认证开关

    编辑D:\program files\mongodb\Server\4.4\bin\monogod.cfg然后找到 #security:去掉#号,开启安全认证。该mongodb.cfg文件是 yaml 文件格式,缩进应该使用空格进行缩进。

    monogod.cfg配置文件可以是yaml格式也可以是键值对格式。

    A.键值对
    		在mono.conf中设置 auth=true
    
    B.yaml格式
    		#security:
    			security:
    			  authorization: enabled
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    重启Mongodb

    C:\WINDOWS\system32>net stop mongodb
    MongoDB Server (MongoDB) 服务正在停止.
    MongoDB Server (MongoDB) 服务已成功停止。
    
    
    C:\WINDOWS\system32>net start mongodb
    MongoDB Server (MongoDB) 服务正在启动 .
    MongoDB Server (MongoDB) 服务已经启动成功。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    使用账号和密码连接数据库

    mongo.exe ‐u root ‐p root‐‐authenticationDatabase admin
    
    • 1

    在这里插入图片描述

    查询用户

    查询admin库下所有用户:

    use admin
    
    show users
    
    • 1
    • 2
    • 3

    修改用户

    语法格式:

    db.updateUser(
      "",
      {
        customData : { <any information> },
        roles : [
                  { role: "", db: "" } | "",
                  ...
                ],
        pwd: ""
        },
        writeConcern: { <write concern> })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    创建test用户:

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

    修改test用户的角色为readWriteAnyDatabase

    use admin
    db.updateUser("test",{roles:[{role:"readWriteAnyDatabase",db:"admin"}]})
    
    • 1
    • 2

    修改密码

    语法格式:

    db.changeUserPassword("username","newPasswd") 
    
    • 1

    修改 test用户的密码为123

    se admin 
    db.changeUserPassword("test","123")
    
    • 1
    • 2

    删除用户

    # 进入账号数据所在的数据库
    use admin
    
    db.dropUser('用户名')
    
    • 1
    • 2
    • 3
    • 4

    备份与恢复

    MongoDB备份与恢复可以通过mongodump和mongorestore命令行工具来完成。

    注意:一定要退出mongo环境,然后执行操作。

    备份

    mongodump -h dbhost -d dbname -o dbdirectory
    
    mongodump -h 127.0.0.1 -d demo -o /backup
    
    # 备份所有数据
    mongodump
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    恢复

    mongorestore -h dbhost -d dbname --dir dbdirectory
    
    mongorestore -h 127.0.0.1 -d demo --dir /backup
    
    • 1
    • 2
    • 3

    定时备份

    使用crontab 定义执行脚本备份数据库

    打开当前用户的crontab文件

    crontab -e
    
    • 1

    在打开文件中添加定时任务

    # mysql 定时备份脚本
    mysqldump -u用户名 -p密码 dbname | gzip > /backup/$(date +%Y%m%d_%H%M%S).sql.gz
    
    # mongodump命令进行备份  gzip压缩输出重定向到以当前时间为名的压缩文件中
    mongodump -h dbhost -d dbname -o dbdirectory | gzip > /backup/$(date +%Y%m%d_%H%M%S).sql.gz
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MongoDB操作工具

    mongo shell

    使用MongoDB自带命令行工具 mongo,进入MongoDB shell中操作

    mongo [--host IP地址] [--port 端口]
    
    • 1
    root@d8110bf377a7:/# mongo
    MongoDB shell version v5.0.5
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("a703ae81-c58c-4021-9f18-e10a1270d0bc") }
    MongoDB server version: 5.0.5
    ================
    Warning: the "mongo" shell has been superseded by "mongosh",
    which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
    an upcoming release.
    For installation instructions, see
    https://docs.mongodb.com/mongodb-shell/install/
    ================
    > show dbs;
    admin   0.001GB
    config  0.000GB
    demo    0.000GB
    local   0.000GB
    > 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    MongoDB Compass

    官网地址: https://www.mongodb.com/try/download/tools

    MongoDB Compass是官方提供的免费 GUI 管理工具,包含如下功能

    数据管理(增删改查)
    
    Schema 管理
    
    索引管理
    
    性能排查
    
    实时性能监控
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    连接Atlas免费MongoDB云数据库:输入连接: mongodb+srv://账户:密码@cluster0.ruiuw.mongodb.net/myFirstDatabase
    在这里插入图片描述
    测试

    添加数据库:demo
    
    添加集合:user
    
    添加一条数据
    
    • 1
    • 2
    • 3
    • 4
    • 5

    选择集合user即能看到MongoDB Compass提供的功能,如:Documents,Aggregations,Schema…
    在这里插入图片描述

    Studio 3T

    Studio 3T是mongodb优秀的客户端工具。

    Studio 3T官网: https://studio3t.com/download/#windows
    在这里插入图片描述
    双击运行,直接安装
    在这里插入图片描述
    创建一个连接,同时填写连接信息。
    在这里插入图片描述
    选择配置好的一个连接
    在这里插入图片描述
    连接成功
    在这里插入图片描述

  • 相关阅读:
    中国智能网联汽车信息安全分析2022案例征集
    值改变事件(SMART PLC梯形图FC)
    博客摘录「 hyperf使用jwt的redis储存驱动实现用户token认证」2024年4月9日
    IDEA踩坑记录:查找用法 找到的不全怎么办
    linux快速安装nodejs与pm2
    Pandas列中的字典/列表拆分为单独的列
    concat() 、concat_ws()和group_concat()
    Python每日一练(牛客新题库)——第20天:字典练习
    WPF之MVVM模式
    Linux------一篇博客了解Linux最常用的指令
  • 原文地址:https://blog.csdn.net/qq_38628046/article/details/129170820