Go教程

MongoDB添加普通用户

本文主要是介绍MongoDB添加普通用户,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1. 没有管理员用户的话就先建一个

    # /usr/local/mongodb/bin/mongo --host localhost --port 27017
    
    rs:PRIMARY> use admin
    switched to db admin
    rs:PRIMARY> db.createUser({
    ...    "user": "root", 
    ...    "pwd": "密码", 
    ...    "roles": [
    ...            {
    ...                    "role": "userAdminAnyDatabase", 
    ...                    "db": "admin"
    ...            }, 
    ...            {
    ...                    "role": "readWriteAnyDatabase", 
    ...                    "db": "admin"
    ...            }, 
    ...            {
    ...                    "role": "dbAdmin", 
    ...                    "db": "admin"
    ...            }, 
    ...            {
    ...                    "role": "root", 
    ...                    "db": "admin"
    ...            }
    ...    ]
    ... });
    
  2. 生成协同文件传输到集群每个节点

    # openssl rand -base64 512 > /usr/local/mongodb/access.key
    # chmod 0600 /usr/local/mongodb/access.key
    
  3. 修改配置文件

    cat >> /usr/local/mongodb/mongod.conf << EOF
    auth=true
    keyFile=/usr/local/mongodb/access.key
    EOF
    
  4. 重启MongoDB

  5. root用户登录验证

    # /usr/local/mongodb/bin/mongo localhost:27017/admin -u root -p
    
  6. 添加普通用户

    rs:PRIMARY> use 库名
    rs:PRIMARY> db.createUser({
    ...  user:"用户名",
    ...  pwd:"密码",
    ...  roles:[
    ...   {role:"readWrite",db:"库名"}
    ...  ]
    ... });
    
这篇关于MongoDB添加普通用户的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!