装置参考

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 admindb.adminCommand({shutdown:1})或mongod -f mongodb.conf --shutdownkilling 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