0

我用用户名和密码配置了 mongodb,并部署了 mongoDb 和 mongoDb express。问题是我在 mongo-express 日志中收到以下错误:

无法使用 connectionString 连接到数据库:mongodb://username:password@mongodb://lc-mongodb-service:27017:27017/"

我可以看到连接字符串包含两次 27017 端口,中间还有不应该存在的“mongodb://”。

这是我的 mongo-express 部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lc-mongo-express
  labels:
    app: lc-mongo-express
spec:
  replicas: 1
  selector:
    matchLabels:
      app: lc-mongo-express
  template:
    metadata:
      labels:
        app: lc-mongo-express
    spec:
      containers:
      - name: lc-mongo-express
        image: mongo-express
        ports:
        - containerPort: 8081
        env:
        - name: ME_CONFIG_MONGODB_SERVER
          valueFrom: 
            configMapKeyRef:
              name: lc-configmap
              key: DATABASE_URL
        - name: ME_CONFIG_MONGODB_ADMINUSERNAME
          valueFrom:
            secretKeyRef:
              name: lc-secret
              key: MONGO_ROOT_USERNAME
        - name: ME_CONFIG_MONGODB_ADMINPASSWORD
          valueFrom:
            secretKeyRef:
              name: lc-secret
              key: MONGO_ROOT_PASSWORD
---
apiVersion: v1
kind: Service
metadata:
  name: lc-mongo-express-service
spec:
  selector:
    app: lc-mongo-express
  type: LoadBalancer  
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081

还有我的 mongoDb 部署:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: lc-mongodb-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  volumeMode: Filesystem
  storageClassName: gp2
---
apiVersion: apps/v1
kind: StatefulSet
metadata: 
  name: lc-mongodb
  labels: 
    app: lc-mongodb
spec: 
  replicas: 1
  serviceName: lc-mongodb-service
  selector: 
    matchLabels: 
      app: lc-mongodb
  template: 
    metadata: 
      labels: 
        app: lc-mongodb
    spec: 
      volumes:
        - name: lc-mongodb-storage
          persistentVolumeClaim:
            claimName: lc-mongodb-pvc
      containers: 
        - name: lc-mongodb
          image: "mongo"
          ports: 
          - containerPort: 27017
          env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom:
              secretKeyRef:
                name: lc-secret
                key: MONGO_ROOT_USERNAME
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: lc-secret
                key: MONGO_ROOT_PASSWORD
          command:
          - mongod
          - --auth
          volumeMounts:
            - mountPath: '/data/db'
              name: lc-mongodb-storage
---
apiVersion: v1
kind: Service
metadata:
  name: lc-mongodb-service
  labels:
    name: lc-mongodb
spec:
  selector:
    app: lc-mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

我究竟做错了什么?

4

1 回答 1

0

您的连接字符串格式错误

你应该尝试类似的东西

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

现在假设您使用的是 Node js

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@<Mongo service Name>/<Database name>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    // creating collection
    const collection = client.db("test").collection("devices");
    // perform actions on the collection object
    client.close();
});

在使用 PVC 和配置路径时,您还缺少命令中的Db路径 args: ["--dbpath","/data/db"]

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mongo
  name: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        args: ["--dbpath","/data/db"]
        livenessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - "db.adminCommand('ping')"
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - "db.adminCommand('ping')"
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: password
        volumeMounts:
        - name: "mongo-data-dir"
          mountPath: "/data/db"
      volumes:
      - name: "mongo-data-dir"
        persistentVolumeClaim:
          claimName: "pvc"
于 2021-11-13T19:38:45.780 回答