Compass 无法与 VPS MongoDB 实例建立连接。以下是日志:
{"t":{"$date":"2024-12-05T20:38:26.097+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"185.121.228.66:51324","uuid":{"uuid":{"$uuid":"5f86b525-d80e-45ea-b05d-ca1e33028b58"}},"connectionId":52,"connectionCount":1}}
{"t":{"$date":"2024-12-05T20:38:26.111+00:00"},"s":"I", "c":"NETWORK", "id":6723804, "ctx":"conn52","msg":"Ingress TLS handshake complete","attr":{"durationMillis":13}}
{"t":{"$date":"2024-12-05T20:38:26.112+00:00"},"s":"E", "c":"NETWORK", "id":23256, "ctx":"conn52","msg":"SSL peer certificate validation failed","attr":{"error":"SSL peer certificate validation failed: unable to get issuer certificate"}}
{"t":{"$date":"2024-12-05T20:38:26.112+00:00"},"s":"I", "c":"EXECUTOR", "id":22988, "ctx":"conn52","msg":"Error receiving request from client. Ending connection from remote","attr":{"error":{"code":141,"codeName":"SSLHandshakeFailed","errmsg":"SSL peer certificate validation failed: unable to get issuer certificate"},"remote":"185.121.228.66:51324","connectionId":52}}
{"t":{"$date":"2024-12-05T20:38:26.112+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn52","msg":"Connection ended","attr":{"remote":"185.121.228.66:51324","uuid":{"uuid":{"$uuid":"5f86b525-d80e-45ea-b05d-ca1e33028b58"}},"connectionId":52,"connectionCount":0}}
不知道是什么导致了这个问题。文件两端的内容相同且可读。
mongo.conf:
net:
port: 27017
# bindIp: 127.0.0.1
bindIpAll: true
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/certAndKey.pem
CAFile: /etc/ssl/chain.pem
之前我在 VPS 端尝试过 NGINX,Compass 可以正常建立连接。我觉得问题出在 MongoDB 端。
certAndKey 内容:
-----BEGIN CERTIFICATE-----
MIIDkTCCAxigAwIBAgISBFenOkN8BKV0K4KSsaH2eRvgMAoGCCqGSM49BAMDMDIx
CzAJBgNVBAYTAlVTMRYwFAYDVQQK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9yZy8wEwYDVR0g
BAwwCjAIBgZngQwBAgEwJwYDVR0fBCAwHjAcoBqgGIYWaHR0cDovL3gxLmMubGVu
Y3Iub3JnLzANBgkqhkiG9w0BAQsFAAOCAgEAfYt7SiA1sgWGCIpunk46r4AExIRc
MxkKgUhNlrrv1B21hOaXN/5miE+LOTbrcmU/M9yvC6MVY730GNFoL8IhJ8j8vrOL
pMY22OP6baS1k9YMrtDTlwJHoGby04ThTUeBDksS9RiuHvicZqBedQdIF65pZuhp
eDcGBcLiYasQr/EO5gxxtLyTmgsHSOVSBcFOn9lgv7LECPq9i7mfH3mpxgrRKSxH
pOoZ0KXMcB+hHuvlklHntvcI0mMMQ0mhYj6qtMFStkF1RpCG3IPdIwpVCQqu8GV7
s8ubknRzs+3C/Bm19RFOoiPpDkwvyNfvmQ14XkyqqKK5oZ8zhD32kFRQkxa8uZSu
h4aTImFxknu39waBxIRXE4jKxlAmQc4QjFZoq1KmQqQg0J/1JF8RlFvJas1VcjLv
YlvUB2t6npO6oQjB3l+PNf0DpQH7iUx3Wz5AjQCi6L25FjyE06q6BZ/QlmtYdl/8
ZYao4SRqPEs/6cAiF+Qf5zg2UkaWtDphl1LKMuTNLotvsX99HP69V2faNyegodQ0
LyTApr/vT01YPE46vNsDLgK+4cL6TrzC/a4WcmF5SRJ938zrv/duJHLXQIku5v0+
EwOy59Hdm0PT/Er/84dDV0CSjdR/2XuZM3kpysSKLgD1cKiDA+IRguODCxfO9cyY
Ig46v9mFmBvyH04=
-----END CERTIFICATE-----
还有 fullchain.pem 文件,但没有使用它。所有证书都是通过 Certbot 从 LetsEncrypt 获得的。
有什么想法为什么 VPS 上的 MongoDB 不喜欢 Compass 尝试传达的内容?
目前 certAndKey.pem 包含
并且 chain.pem 包含
作为 CAFile 传递的 .pem 文件应该只包含根证书,即 ISRG Root X1。您需要获取该根证书并将其传递到 CAFile 中。
将中间证书、服务器证书、服务器私钥放在同一个文件中,作为certificateKeyFile传递。
将根证书添加到您的 CA。
chain.pem
文件应如下所示:通常,Let's Encrypt 证书存储在您的本地证书存储中,因此您也可以使用tlsUseSystemCA。我认为在 Compass 和 mongosh 中它甚至是默认的。
总结一下 Wernfried Domscheit 提供的解决方案,用通俗易懂的英语来表达,因为这个证书让我们非常困惑。因为 LetsEncrypt 的 Certbot 会为您生成以下文件:
您需要执行以下操作:
Certbot 不提供 RootCA!您需要直接从 LetEncrypt 网站下载。
https://letsencrypt.org/certificates/][1]
它是 ISRG Root X1,自签名的。这是直接链接
[https://letsencrypt.org/certs/isrgrootx1.pem][1]
至于你的 mongod.cong,tls 部分应该是这样的:
至于 Compass 连接,显然,只有“客户端证书和密钥(.pem)”文件才是您的 certKey 文件,就足以进行连接。