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

Windows环境下Vagrant搭建的裸金属K8s集群中MongoDB Compass连接问题及账号密码咨询

Connecting MongoDB Compass to Your K8s GuestBook MongoDB Instance

First, let's tackle the credentials question, then walk through two reliable connection methods for your Vagrant-based bare-metal cluster.

Step 1: Locate MongoDB Credentials (Username/Password)

Most Kubernetes deployments store database credentials in Secrets—here's how to retrieve them:

  • List all secrets in your namespace (add -n <namespace> if you're not using the default):
    kubectl get secrets
    
    Look for a secret tied to MongoDB, like mongodb, guestbook-mongodb, or mongo-db-credentials.
  • Decode the base64-encoded username:
    kubectl get secret <your-mongo-secret-name> -o jsonpath='{.data.username}' | base64 -d
    
  • Decode the password the same way:
    kubectl get secret <your-mongo-secret-name> -o jsonpath='{.data.password}' | base64 -d
    

Note for Official GuestBook Example

If you used the standard Kubernetes GuestBook demo, the MongoDB deployment doesn't enable authentication by default—so you can skip the credentials step entirely. Verify this by checking the config inside the container:

# Inside the MongoDB container
cat /etc/mongod.conf | grep authorization

If there's no authorization: enabled line, auth is turned off.

Step 2: Connect with MongoDB Compass

You have two solid options here—port forwarding (recommended for temporary testing) or exposing the service via NodePort (for longer-term access).

Option 1: Kubectl Port Forwarding (Quick & Secure)

This maps your local machine's port directly to the cluster's MongoDB service, no public exposure needed:

  1. Find your MongoDB service name:
    kubectl get svc | grep mongo
    
  2. Run the port forward command (keep your terminal open while it runs):
    kubectl port-forward svc/<your-mongo-service-name> 27017:27017
    
  3. Open MongoDB Compass and use one of these connection strings:
    • With authentication:
      mongodb://<username>:<password>@localhost:27017/guestbook?authSource=admin
      
    • Without authentication (default GuestBook setup):
      mongodb://localhost:27017/guestbook
      

Option 2: Expose MongoDB via NodePort (Permanent Access)

If you want persistent access without keeping a port forward running:

  1. Edit your MongoDB service to change its type to NodePort:
    kubectl edit svc/<your-mongo-service-name>
    
  2. Replace type: ClusterIP with type: NodePort, then save and exit the editor.
  3. Get the assigned NodePort and your Vagrant node's IP:
    • NodePort:
      kubectl get svc <your-mongo-service-name>
      
      Look for the port mapping like 27017:30XXX/TCP—the second number is your NodePort.
    • Vagrant node IP:
      kubectl get nodes -o wide
      
      Use the INTERNAL-IP of the node where your MongoDB pod is running (you found this earlier with kubectl get pods -o wide).
  4. In MongoDB Compass, use this connection string:
    • With authentication:
      mongodb://<username>:<password>@<vagrant-node-ip>:<node-port>/guestbook?authSource=admin
      
    • Without authentication:
      mongodb://<vagrant-node-ip>:<node-port>/guestbook
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 15:47:47