How to connect mongodb in K8s via a go app

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

How to connect mongodb in K8s via a go app

问题

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

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

端口转发:

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

Go应用程序:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. // "log"
  6. "time"
  7. "go.mongodb.org/mongo-driver/mongo"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "go.mongodb.org/mongo-driver/mongo/readpref"
  10. )
  11. func main() {
  12. username := "username"
  13. address := "localhost"
  14. password := "password"
  15. // 用你的MongoDB部署的连接字符串替换uri字符串。
  16. uri := "mongodb+srv://" + username + ":" + password + "@" + address + "/admin?w=majority"
  17. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  18. defer cancel()
  19. client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer func() {
  24. if err = client.Disconnect(ctx); err != nil {
  25. panic(err)
  26. }
  27. }()
  28. // Ping the primary
  29. if err := client.Ping(ctx, readpref.Primary()); err != nil {
  30. panic(err)
  31. }
  32. fmt.Println("Successfully connected and pinged.")
  33. }
英文:

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.

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

Port-forward:

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

Go app:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. //"log"
  6. "time"
  7. "go.mongodb.org/mongo-driver/mongo"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "go.mongodb.org/mongo-driver/mongo/readpref"
  10. )
  11. func main() {
  12. username := "username"
  13. address := "localhost"
  14. password := "password"
  15. // Replace the uri string with your MongoDB deployment's connection string.
  16. uri := "mongodb+srv://" + username + ":" + password + "@" + address + "/admin?w=majority"
  17. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  18. defer cancel()
  19. client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer func() {
  24. if err = client.Disconnect(ctx); err != nil {
  25. panic(err)
  26. }
  27. }()
  28. // Ping the primary
  29. if err := client.Ping(ctx, readpref.Primary()); err != nil {
  30. panic(err)
  31. }
  32. fmt.Println("Successfully connected and pinged.")
  33. }

答案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:

确定