MongoDB4.28开启权限认证配置用户密码登录功能

目录
  • 1、查看是否开启认证登录
  • 2、开启用户名和密码认证(创建用户均需进入admin数据库)
    • 2.1、为admin数据库创建管理员账号
    • 2.2、为数据库mytest创建普通用户
    • 2.3、配置文件开启用户名密码认证
  • 3、重启mongo服务
    • 4、mongo授权访问
      • 4.1、admin数据库授权登录
      • 4.1、mytest数据库授权登录

    mongodb默认不启用授权认证,只要能连接到该服务器,就可连接到mongod。若要启用安全认证,需要更改配置文件mongdb.conf中的参数auth。

    mongodb的用户是跟数据库相关联的,具体的数据库,需要有对应的用户,超级管理员也不能操作其他数据库的。

    mongodb存储所有的用户信息在admin 数据库的集合system.users中,保存用户名、密码和数据库信息。

    mongodb开启权限认证:配置用户名和密码认证登录,操作步骤:

    1、查看是否开启认证登录

    $cd /usr/local/mongodb/bin
    $cat mongodb.conf

    #数据文件存放目录
    dbpath = /usr/local/mongodb/data
    #日志文件存放目录
    logpath = /usr/local/mongodb/logs/mongodb.log
    logappend=true
    #端口
    port = 27017
    #以守护程序的方式启用,即在后台运行
    fork = true
    #认证模式(true代表开启认证登录,false代表未开启认证登录)
    auth=false   
    #远程连接
    bind_ip=0.0.0.0

    2、开启用户名和密码认证(创建用户均需进入admin数据库)

    2.1、为admin数据库创建管理员账号

    1、数据库admin创建管理员账号

    [root@hadoop-master bin]# mongo
    > use admin
    > db.createuser({user:"root",pwd:"lianshi",roles:["root"]})

    2、查看目前用户

    > show users

    2.2、为数据库mytest创建普通用户

    1、给数据库mytest创建cg用户

    >use mytest
    > db.createuser({user:"cg",pwd:"lianshi",roles:[{role:"readwrite",db:"mytest"}]})

    2、查看目前用户

    > show users
    >db.system.users.find()命令可以查看新创建的用户

    2.3、配置文件开启用户名密码认证

    #认证模式(true代表开启认证登录,false代表未开启认证登录)
    auth=true

    3、重启mongo服务

    [root@hadoop-master bin]# ps -ef |grep mongo
    [root@hadoop-master bin]# kill -9 15231
    $./mongod -f mongodb.conf

    4、mongo授权访问

    4.1、admin数据库授权登录

    1、mongo访问

    [root@hadoop-master bin]# mongo
    > use admin
    switched to db admin
    > show users
    2020-06-21t20:14:59.735+0800 e  query    [js] uncaught exception: error: command usersinfo requires authentication :
    _geterrorwithcode@src/mongo/shell/utils.js:25:13
    db.prototype.getusers@src/mongo/shell/db.js:1638:15
    shellhelper.show@src/mongo/shell/utils.js:883:9
    shellhelper@src/mongo/shell/utils.js:790:15
    @(shellhelp2):1:1 -->授权配置并重启后,此时查看用户,会发现没有权限

    2、用用户和密码登录

    > db.auth("root","lianshi")

    —>使用db.auth(“root”,”lianshi”)启用auth认证,看到返回的值为1,这就表示启动成功了,然后我们再使用命令查看用户和数据库。

    4.1、mytest数据库授权登录

    1、mongo访问

    > use mytest;
    switched to db mytest
    > show users
    2020-06-21t21:25:41.293+0800 e  query    [js] uncaught exception: error: command usersinfo requires authentication :
    _geterrorwithcode@src/mongo/shell/utils.js:25:13
    db.prototype.getusers@src/mongo/shell/db.js:1638:15
    shellhelper.show@src/mongo/shell/utils.js:883:9
    shellhelper@src/mongo/shell/utils.js:790:15
    @(shellhelp2):1:1 --->报错没有权限

    2、用户和密码登录用户

    > db.auth("cg","lianshi");

    使用db.auth(“cg”,”lianshi”)启用auth认证,看到返回的值为1,这就表示启动成功了,然后我们再使用命令查看用户和数据库。

    > show dbs
    mytest  0.000gb
    >  db.student.insert({"id":"2","name":"yxy"})
    writeresult({ "ninserted" : 1 })

    其他用户命令:

    1、创建普通用户(创建用户cg,对mytest数据库读写权限)

    > db.createuser({user:"cg",pwd:"lianshi",roles:[{role:"readwrite",db:"mytest"}]})

    2、删除用户>db.dropuser(“yonghu”)

    3、修改用户密码

    db.updateuser("cg",{pwd:"123456"})

    4、进入数据mytest,用户名密码认证

    > db.auth("cg","lianshi");

    5、客户端工具授权登录连接mongo数据库

    用户名和密码连接数据库

    到此这篇关于mongodb4.28开启权限认证配置用户密码登录功能的文章就介绍到这了,更多相关mongodb权限认证登录内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

    (0)
    上一篇 2022年3月21日
    下一篇 2022年3月21日

    相关推荐