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

Spring Data MongoDB在K8s副本集部署中遇数据库名称格式错误求助

Fixing "Database name must not contain slashes..." Error with K8s Secrets & Spring Data MongoDB

Hey there! Let's figure out why this error is popping up when you switch to K8s Secrets for your MongoDB config—you already got it working locally, so we're just a few tweaks away from fixing this.

First, let's break down the error message: "Database name must not contain slashes..." means the MongoDB driver is parsing something weird as your database name, almost certainly because the final spring.data.mongodb.uri value is malformed once it's pulled from K8s Secrets.

Step 1: Double-Check Your application.properties Syntax

Looking at your config line:

spring.data.mongodb.uri=${MYAPP_MONGODB}:mongodb://localhost:27017/myapp

Wait a second—this would create a URI like your-mongo-host:mongodb://localhost:27017/myapp if MYAPP_MONGODB is set to a host address. That's definitely invalid! No wonder the driver is confused.

You probably want one of two formats:

  • Option 1: Store just the MongoDB host in Secrets
    Update your properties to correctly build the URI:

    spring.data.mongodb.uri=mongodb://${MYAPP_MONGODB}:27017/myapp
    

    Here, MYAPP_MONGODB should just be the host (e.g., localhost, my-mongo-service.default.svc.cluster.local)—no mongodb:// prefix needed.

  • Option 2: Store the full URI in Secrets (simpler, less error-prone)
    If you want to avoid拼接 altogether, store the complete valid URI in Secrets:

    spring.data.mongodb.uri=${MYAPP_MONGODB_URI}
    

    Then your Secrets value would be something like mongodb://my-mongo-host:27017/myapp (add auth params if needed).

Step 2: Fix K8s Secrets Configuration

The most common culprit here is extra characters (like newlines) sneaking into your Secrets value because of incorrect base64 encoding.

  • Use stringData instead of data (easiest approach)
    stringData lets you write plain text values, and K8s handles the base64 encoding automatically—no more manual encoding mistakes. Here's an example Secret YAML:

    apiVersion: v1
    kind: Secret
    metadata:
      name: myapp-mongo-secrets
    stringData:
      # For Option 1 (host only):
      MYAPP_MONGODB: "my-mongo-service.default.svc.cluster.local"
      # OR for Option 2 (full URI):
      # MYAPP_MONGODB_URI: "mongodb://my-mongo-service.default.svc.cluster.local:27017/myapp"
    
  • If you must use data, encode correctly
    If you prefer using the data field, make sure you encode without adding a newline:

    # Encode your host/URI without extra newlines
    echo -n "my-mongo-service.default.svc.cluster.local" | base64
    

    Then paste that encoded string into your Secret's data field.

Step 3: Verify Environment Variable Injection

Once your Secret is deployed, check that the environment variable is being injected correctly into your Pod:

kubectl exec -it <your-pod-name> -- printenv MYAPP_MONGODB
# OR for full URI:
# kubectl exec -it <your-pod-name> -- printenv MYAPP_MONGODB_URI

Make sure the output is exactly the value you stored—no extra newlines, quotes, or weird characters. If there's a newline, that's probably the issue (fix it by re-encoding with echo -n or using stringData).

Step 4: Test the Deployment

After fixing the config and Secrets, redeploy your app. The driver should now parse the URI correctly, and that database name error should disappear.

The key here is ensuring the final URI passed to Spring Data MongoDB is 100% valid—K8s Secrets can introduce hidden formatting issues if not set up right, but these steps should cover the most common fixes.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:59:42