关于mongodb:MongoDB安装启动关闭授权

10次阅读

共计 1313 个字符,预计需要花费 4 分钟才能阅读完成。

装置参考

https://docs.mongodb.com/v5.0…

配置文件

vim /etc/mongod.conf

启动 MongoDB

systemctl start mongod.service
或
mongod -f /etc/mongod.conf

MongoDB 的敞开形式

  1. kill 过程模式 (不倡议应用)

    kill -2 PID #原理:- 2 示意向 mongod 过程发送 SIGINT 信号
    或者
    kill -4 PID #原理:- 4 示意向 mognod 过程发送 SIGTERM 信号 
  2. 自带模式

    use admin
    db.adminCommand({shutdown:1})
    或
    mongod -f mongodb.conf --shutdown
    killing process with pid: 1621

    留神:
    mongod 过程收到 SIGINT 或 SIGTERM 信号,会做一些解决
    切忌应用 kill -9

查看日志

/var/log/mongodb/mongod.log

设置 MongoDB 开机启动

systemctl enable mongod.service

创立帐号

创立管理员帐号

# 进入 mongo shell 命令
mongo
# 切换到 admin 库
use admin
# 创立 root 帐号
db.createUser({user: "root", pwd: "SADwerWSrTbdh", roles: [{ role: "root", db: "admin"}] })
# 验证用户,返回 1 示意胜利
db.auth('root', 'SADwerWSrTbdh')

批改配置

vim /etc/mongod.conf
# 启用权限管制
security:
  authorization: enabled

重启 mongodb

新建各个库的管理员帐号密码

# 验证数据库用 admin 库
use admin
# 新建管理员账号
db.createUser({user: "api", pwd: "xxxxxxx", roles: [{ role: "dbOwner", db: "spy"}] })
# 批改用户
db.updateUser('api', { roles: [ { role: "dbOwner", db: "spy"}, {role: "dbOwner", db: "spy99"}]})
# 新建读写帐号
db.createUser({user: "youruser2", pwd: "yourpassword2", roles: [{ role: "readWrite", db: "yourdatabase"}] })

# 显示用户
show users
# 删除用户
db.dropUser("spider_api")
# 导出 spy99 库到 backed 目录
mongodump --host 127.0.0.1 --port 33017 --username 'api' --password 'xxxxxxxxxxx' --authenticationDatabase admin -d spy99 -o backed
# 导入 backed 目录到 spy99 库
mongorestore --host 127.0.0.1 --port 33017 --username 'api' --password 'xxxxxx' --authenticationDatabase admin -d spy99 backed
正文完
 0