mongodb设置用户账号密码登录

背景

今天有个同事的项目中mongodb 被黑客攻占了,然后数据库被清掉,留下了一堆“案发现场”,还整了一个段话,附上给大家伙瞧瞧。。。。

All your data is a backed up. You must pay 0.021 BTC to
1Di1cM1QgTxZuwsxp9nRBc6UXUAhbMN7YX 48 hours for recover it. After 48
hours expiration we will leaked and exposed all your data. In case of
refusal to pay, we will contact the General Data Protection
Regulation, GDPR and notify them that you store user data in an open
form and is not safe. Under the rules of the law, you face a heavy
fine or arrest and your base dump will be dropped from our server! You
can buy bitcoin here, does not take much time to buy
https://localbitcoins.com or https://buy.moonpay.io/ After paying
write to me in the mail with your DB IP: [email protected]
and you will receive a link to download your database dump.

排查出的问题就不说,说了丢人,我看这位同事的端口限制也是针对的几个ip连接,其余的漏洞都堵的差不多了,就写一下mongodb 的创建账户密码的过程吧,这个他没做,感觉也是光着屁股在裸奔,不过所幸让他们上线前执行了我的脚本,数据马上恢复过来了,没丢失。唉。
再一个为啥要写这个博客,因为我那同事搞了一下午都没搞定创建账户密码,我觉得还是自己写一下,记录一下,以免后面自己也记不清了。。。。

  • 由于我们这个是宝塔安装的mongodb,所以进入宝塔的默认目录

1,进入宝塔的默认目录。

cd /www/server/mongodb/bin

2,进入mongodb环境

命令: mongo

3,进入 admin 数据库

命令: use admin

4, 设置admin 用户密码

db.createUser({user: 'root', pwd: 'pwr23', roles: ['root']})

5,验证刚刚创建的账户是否成功

db.auth('root', 'pwr23') 

返回1:成功
0:失败

6,创建一个新的数据库 test

创建成功后,退出命令行,重新进入

7,进入mongodb环境

命令: mongo

8,进入 admin 数据库

命令: use admin

9,创建一个用户,并且赋予权限,

db.createUser({ user: 'rootDev', pwd: 'wddmin23', roles: [{ role: 'root', db: 'admin' }] })

10,修改mongodb,配置文件

authorization 修改为 enabled 开启认证

在这里插入图片描述

11,重启一下 mongodb 就行辣

12,连接mongodb服务器

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38420688/article/details/124390256