<p>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.</p>
<pre><code>panic: error parsing uri: lookup _mongodb._tcp.localhost on 8.8.8.8:53: no such host
</code></pre>
<p>Port-forward:</p>
<pre><code>kubectl port-forward service/mongodb-svc 27017:27017
</code></pre>
<p>Go app:</p>
<pre><code>package main

import (
    &quot;context&quot;
    &quot;fmt&quot;
    //&quot;log&quot;
    &quot;time&quot;

    &quot;go.mongodb.org/mongo-driver/mongo&quot;
    &quot;go.mongodb.org/mongo-driver/mongo/options&quot;
    &quot;go.mongodb.org/mongo-driver/mongo/readpref&quot;
)

func main() {
    username := &quot;username&quot;
    address := &quot;localhost&quot;
    password := &quot;password&quot;
    // Replace the uri string with your MongoDB deployment's connection string.
    uri := &quot;mongodb+srv://&quot; + username + &quot;:&quot; + password + &quot;@&quot; + address + &quot;/admin?w=majority&quot;
    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(&quot;Successfully connected and pinged.&quot;)
}
</code></pre>

<p>Your client is trying to a DNS service lookup because you specified the <code>+srv</code> 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.</p>
