mongodb配置和授权

2023年 7月 15日 33.6k 0

  • 重启之前要打开认证,也就是

    security:
     authorization: enabled
    验证登录

    1,登录开启认证后就不会提示警告信息

    [root@LinuxEA-VM-Node146 /usr/local/mongodb]# mongo
    MongoDB shell version v4.0.0
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 4.0.0

    2,进入库,如果验证用户密码就会报错

    > use linuxea
    switched to db linuxea
    > db.user.find()
    Error: error: {
      "ok" : 0,
      "errmsg" : "command find requires authentication",
      "code" : 13,
      "codeName" : "Unauthorized"
    }

    2,验证登录

    > db.auth("linuxea","123456")
    1
    > db.user.find()
    > 

    直接授权方式

    进入admin表

    > use admin
    switched to db admin

    授权

    >db.createUser({"user":"admin","pwd":"admin","roles":["root"]})

    登录

    > db.auth("admin","admin")
    1

    创建用户dashboarduser密码dbpassword,库名dashboarddb

    >  db.createUser(
    ... {
    ...   user: "dashboarduser",
    ...   pwd: "dbpassword",
    ...   roles: [
    ...   {role: "readWrite", db: "dashboarddb"}
    ...   ]
    ... })
    Successfully added user: {
      "user" : "dashboarduser",
      "roles" : [
          {
              "role" : "readWrite",
              "db" : "dashboarddb"
          }
      ]
    }
    > 

    登录dashboarddb,用户dashboarduser密码dbpassword

    > use dashboarddb
    switched to db dashboarddb
    >  db.auth("dashboarduser","dbpassword")
    1
    > show users;
    {
      "_id" : "dashboarddb.dashboarduser",
      "user" : "dashboarduser",
      "db" : "dashboarddb",
      "roles" : [
          {
              "role" : "readWrite",
              "db" : "dashboarddb"
          }
      ],
      "mechanisms" : [
          "SCRAM-SHA-1",
          "SCRAM-SHA-256"
      ]
    }
    > 

    登录验证,创建数据

    [root@LinuxEA-VM-Node146 /data/mongodb]# mongo
    MongoDB shell version v4.0.0
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 4.0.0
    > use dashboarddb
    switched to db dashboarddb
    > db.linuxea.insert({"name":1,"age":18})
    WriteCommandError({
      "ok" : 0,
      "errmsg" : "command insert requires authentication",
      "code" : 13,
      "codeName" : "Unauthorized"
    })
    > db.auth("dashboarduser","dbpassword")
    1
    @(shell):1:20
    > db.linuxea.insert({"name":1,"age":18})
    WriteResult({ "nInserted" : 1 })
    > show dbs;
    dashboarddb  0.000GB
    > 

    快速安装脚本

    系统centos7,mongodb版本如本章所示rhel70-4.0.0

    curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-mongodb/master/scripts/install_mongodb.sh|bash

    robo3t下载

    如果翻墙不了,这里提供下载地址Robo 3T(以前称为Robomongo)是MongoDB免费轻量级GUI。仍选择其一即可

    https://github.com/LinuxEA-Mark/docker-mongodb/blob/master/robo3t-1.2.1-windows-x86_64-3e50a65.exe
    https://github.com/LinuxEA-Mark/docker-mongodb/blob/master/robo3t-1.2.1-windows-x86_64-3e50a65.zip
  • 相关文章

    Oracle如何使用授予和撤销权限的语法和示例
    Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
    下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
    社区版oceanbase安装
    Oracle 导出CSV工具-sqluldr2
    ETL数据集成丨快速将MySQL数据迁移至Doris数据库

    发布评论