更新
sudo apt update && sudo apt upgrade -y
安装mongodb
sudo apt install mongodb
查看状态
sudo systemctl status mongodb
修改mongodb状态
sudo systemctl status mongodb
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodb
你也可以修改 MongoDB 是否自动随系统启动(默认:启用):
sudo systemctl disable mongodb
sudo systemctl enable mongodb
链接mongodb
mongo
创建用户
设置管理员账号和密码
use admin
db.createUser({
user: 'admin', // 用户名(自定义)
pwd: 'Abc123++', // 密码(自定义)
roles:[{
role: 'root', // 使用超级用户角色
db: 'admin' // 指定数据库
}]
})
# 设置完成,可以通过指令 show users 查看是否设置成功
目前3.6测试不需要修改配置文件
找到MongoDB安装目录下的bin目录中的mongod.cfg文件,开启权限验证功能:
whereis mongodb
# mongodb: /etc/mongodb.conf
vim /etc/mongodb.conf
添加如下代码
security:
authorization: enabled
重启mongodb
sudo systemctl restart mongodb
sudo systemctl status mongodb
链接数据库
mongo --port 27017 -u "admin" -p "Abc123++" --authenticationDatabase "admin"