How to connect mongodb in K8s via a go app

huangapple go评论71阅读模式
英文:

How to connect mongodb in K8s via a go app

问题

我有一个正在运行的MongoDB服务。我使用端口转发在本地访问它,同时,我尝试使用一个Go应用程序检查连接。但是我得到了下面的错误。

panic: error parsing uri: lookup _mongodb._tcp.localhost on 8.8.8.8:53: no such host

端口转发:

kubectl port-forward service/mongodb-svc 27017:27017

Go应用程序:

package main

import (
    "context"
    "fmt"
    // "log"
    "time"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)

func main() {
    username := "username"
    address := "localhost"
    password := "password"
    // 用你的MongoDB部署的连接字符串替换uri字符串。
    uri := "mongodb+srv://" + username + ":" + password + "@" + address + "/admin?w=majority"
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
    if err != nil {
        panic(err)
    }

    defer func() {
        if err = client.Disconnect(ctx); err != nil {
            panic(err)
        }
    }()

    // Ping the primary
    if err := client.Ping(ctx, readpref.Primary()); err != nil {
        panic(err)
    }

    fmt.Println("Successfully connected and pinged.")
}
英文:

I have a mongodb service up and running. I port-forward to access it locally and in the meantime, I try to check connection with a go app. But I get the error below.

panic: error parsing uri: lookup _mongodb._tcp.localhost on 8.8.8.8:53: no such host

Port-forward:

kubectl port-forward service/mongodb-svc 27017:27017

Go app:

package main

import (
    "context"
    "fmt"
    //"log"
    "time"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)

func main() {
    username := "username"
    address := "localhost"
    password := "password"
    // Replace the uri string with your MongoDB deployment's connection string.
    uri := "mongodb+srv://" + username + ":" + password + "@" + address + "/admin?w=majority"
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
    if err != nil {
        panic(err)
    }

    defer func() {
        if err = client.Disconnect(ctx); err != nil {
            panic(err)
        }
    }()

    // Ping the primary
    if err := client.Ping(ctx, readpref.Primary()); err != nil {
        panic(err)
    }

    fmt.Println("Successfully connected and pinged.")
}

答案1

得分: 3

您的客户正在尝试进行DNS服务查找,因为您在URI中指定了+srv连接类型。请停止这样做,并改用正确的连接字符串。我们支持在集群内部进行此操作,但不支持通过端口转发。我怀疑您正在尝试混合使用集群内和集群外的教程。您不能这样做。

英文:

Your client is trying to a DNS service lookup because you specified the +srv connection type in your URI. Stop doing that and use the correct connection string instead. We do support that in-cluster but not via port forward. I suspect you're trying to mix and match tutorials for both in-cluster and out of cluster. You can't do that.

huangapple
  • 本文由 发表于 2021年8月17日 05:42:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/68809529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定