我有一个 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去应用:package mainimport ( "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 回答
largeQ
TA贡献2039条经验 获得超7个赞
您的客户端正在尝试 DNS 服务查找,因为您在 URI 中指定了连接类型。请停止执行此操作,并改用正确的连接字符串。我们确实支持群集内,但不支持通过端口转发。我怀疑您正在尝试混合和匹配集群内和集群外的教程。你不能这么做。+srv
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报
0/150
提交
取消