You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K8s Pod中Mongo --ssl命令默认连接错误数据库问题求助

问题原因与解决方法

核心原因

--authenticationDatabase参数仅用于指定身份验证时使用的数据库,它只负责完成登录认证逻辑,并不会改变mongo shell连接成功后默认进入的数据库(mongo shell默认自动切换到test库)。所以你虽然用doc_document_db完成了认证,但后续执行db.getCollectionNames()时,操作的还是默认的test库。

两种解决办法

  • 方法一:在eval命令中先切换数据库
    修改--eval的内容,先切换到目标数据库再查询集合:

    mongo --ssl --host host.us-west-2.docdb.amazonaws.com --port 27017 --sslCAFile global-bundle.pem --username dbadmin --password ********  --authenticationDatabase doc_document_db --eval "use doc_document_db; db.getCollectionNames()"
    
  • 方法二:连接时直接指定目标数据库
    在命令末尾添加目标数据库名称,连接成功后会直接进入该库执行后续命令:

    mongo --ssl --host host.us-west-2.docdb.amazonaws.com --port 27017 --sslCAFile global-bundle.pem --username dbadmin --password ********  --authenticationDatabase doc_document_db doc_document_db --eval "db.getCollectionNames()"
    

内容的提问来源于stack exchange,提问作者RCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 00:42:02