在Debian上配置MongoDB以使用SSL加密连接,可以按照以下步骤进行:
首先,确保你已经安装了MongoDB。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install -y mongodb
你需要为MongoDB生成SSL证书和密钥。可以使用OpenSSL来生成这些文件。
mkdir -p /etc/ssl/mongodb
cd /etc/ssl/mongodb
# 生成CA私钥
openssl genrsa -out ca.key 2048
# 生成CA证书
openssl req -new -x509 -days 365 -key ca.key -out ca.crt -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=YourCAName"
# 生成服务器私钥
openssl genrsa -out mongodb.key 2048
# 生成服务器证书签名请求 (CSR)
openssl req -new -key mongodb.key -out mongodb.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=yourhostname"
# 使用CA证书签名CSR,生成服务器证书
openssl x509 -req -in mongodb.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out mongodb.crt -days 365
如果你需要客户端证书进行双向认证,可以按照以下步骤生成:
# 生成客户端私钥
openssl genrsa -out client.key 2048
# 生成客户端证书签名请求 (CSR)
openssl req -new -key client.key -out client.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=clientname"
# 使用CA证书签名CSR,生成客户端证书
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365
编辑MongoDB配置文件 /etc/mongod.conf,添加或修改以下内容:
net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb/mongodb.crt
CAFile: /etc/ssl/mongodb/ca.crt
allowConnectionsWithoutCertificates: false # 如果需要双向认证,设置为true
如果你需要配置客户端证书验证,还需要添加以下内容:
security:
authorization: enabled
ssl:
clientCertificateKeyFile: /etc/ssl/mongodb/client.key
clientCertificateCAFile: /etc/ssl/mongodb/ca.crt
保存配置文件后,重启MongoDB服务以应用更改:
sudo systemctl restart mongod
你可以使用 mongo shell 或其他MongoDB客户端工具来验证SSL连接是否成功。例如:
mongo --ssl --sslCAFile /etc/ssl/mongodb/ca.crt --sslPEMKeyFile /etc/ssl/mongodb/mongodb.crt
如果一切配置正确,你应该能够成功连接到MongoDB并执行操作。
600:sudo chmod 600 /etc/ssl/mongodb/*.key
sudo chmod 644 /etc/ssl/mongodb/*.crt
通过以上步骤,你应该能够在Debian上成功配置MongoDB以使用SSL加密连接。