MongoDB Redis 外部访问加验证

本文详细介绍了如何在MongoDB中添加root用户并配置安全授权,包括无密码启动、创建超级管理员账户、修改配置文件以启用身份验证,并提供了一个完整的配置示例。此外,还提到了Redis的安全设置,如修改配置接收所有IP请求及设置强密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mongodb

首先默认无密码启动
1 添加root用户:

db.createUser(
   {
     user: "root",
     pwd: "password",
     
     roles: [{"role":"root","db":"admin"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
    // authenticationRestrictions: [ {
    //     clientSource: ["192.168.0.0"],
    //     serverAddress: ["xxx.xxx.xxx.xxx"]
    //  } ],

    //mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ], 

    //passwordDigestor: "<server|client>"
   }
)

2 修改配置:

security:
  authorization: enabled
  javascriptEnabled: false
net:
  port: 27017
  bindIp: 0.0.0.0

完整配置如下:

## content
systemLog:
  destination: file
  logAppend: true
  path: /www/server/mongodb/log/config.log
 
# Where and how to store data.
storage:
  dbPath: /www/server/mongodb/data
  directoryPerDB: true

  journal:
    enabled: true
# how the process runs
processManagement:
  fork: true
  pidFilePath: /www/server/mongodb/log/configsvr.pid
 
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
 
#operationProfiling:
#replication:
#    replSetName: bt_main   
security:
  authorization: enabled
  javascriptEnabled: false

#sharding:
#    clusterRole: shardsvr
  1. 重启数据库

redis

  1. 找到config文件 注释掉 bind 127.0.0.1 接收来自所有ip的请求
  2. 找到配置文件中 requirepass 配置成自己想要的密码
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 2343d(^$@!)

问题

  • 可能需要开放服务器的相应端口 (云服务器安全组、或者宝塔安全配置)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值