背景
MongoDB初始化未添加权限验证机制(用户),导致容易被攻击删库,如:😭
解决办法
一: 更改MongoDB的默认端口号,
修改mongod.conf的端口号配置
二: 为MongoDB数据库添加用户
- 开启数据库的安全选项 更改mongod.conf配置:
security:
authorization: enabled
-
为数据库添加用户
- 切换到需要操作的数据库
- db.createUser({user: "username", pwd: "password", roles: [role]}),角色如readWrite、dbOwner等,参考官网
- db.auth("username", "pwd")验证
- 重启mongodb服务
-
相应的需要更改server的mongodb连接,以node.js为例
使用MongoClient连接mongodblet MongoClient = require('mongodb').MongoClient; // 自己的链接地址 let url = 'mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb'; MongoClient.connect( url, { authSource: 'sword', auth: { user: 'username', password: '*******', }, }, callback );