MongoDB创建用户名,密码

# db.createUser({user:"用户名", pwd: "密码"}, roles:['root'])
# db.createUser({user:"sylar", pwd: "123456", roles:['root']})

# 创建完成后,需要退出 mongodb 然后修改配置文件

# mac 和 linux

# 1 dbpath=/usr/local/mongodb
# 2 port=27017
# 3 logpath=/usr/local/var/log/mongodb/mongo.log
# 4 fork=true
# 5 logappend=true
# 6 auth=true # 加上账户认证

# windows

# storage:
# dbPath: D:\MongoDB\Server\4.4\data
# journal:
# enabled:true

# systemLog:
# destination: file
# logAppend: true
# path: D:\MongoDB\Server\4.4\log\mongod.log

# net:
# port: 27017
# bindIp: 127.0.0.1

# 注意:security 要顶格,authorization 要空两格,enabled 前面要有个空格
# security:
# authorization: enabled

# 然后,需要重新启动mongodb的服务(linux和mac直接杀掉进程,重新启动即可,windows去系统服务里重启mongodb的服务)
# 然后重新打开mongo,先进入admin,登录超级管理员账号,进入到你想要单独创建管理员的数据库,然后创建出该数据库的管理员账号

# 1 > use admin
# 2 switched to db admin
# 3 > db.auth("sylar","123456")
# 4 1
# 5 > use ddd
# 6 switched to db ddd
# 7 > db.createUser({user:"ttt_admin", pwd:"123456", roles:["readWrite"]})

# 退出管理员账号,重新登录刚刚创建好的账号

# 1 > use ddd
# 2 switched to db ddd
# 3 > db.auth("ttt_admin", "123456")
# 4 1
# 5 > db.stu.insert({name:123, age:3})
# 6 WriteResult({"nInserted" : 1})
# 7 >

# 注意:每个账号只能管理自己的数据库(可以多个)




posted @ 2023-08-14 16:11  严永富  阅读(402)  评论(0)    收藏  举报