apache/beam · critical
error pinging MongoDB
Error message
error pinging MongoDB: %w
What it means
Returned by newClient in mongodbio when client.Ping against the primary fails. The client was constructed, but no MongoDB server could actually be reached or authenticated, so Setup aborts. It wraps the underlying driver/ping error.
Solutions
- Verify network reachability from the worker to host:port (test with `nc -vz host 27017` or ping).
- Check that the MongoDB server/replica set is up and has a PRIMARY elected.
- Validate credentials and authSource URI option; test the same URI with mongosh.
- If using TLS/mongodb+srv, verify DNS SRV resolution and CA certificate configuration.
- Retry on transient network failures; inspect the wrapped error (e.g. connection refused, auth failed) for the exact cause.
Example fix
// before uri := "mongodb://user:pass@internal-host:27017/db" // authSource defaults to admin // after: explicit authSource and correct host uri := "mongodb://user:pass@db.internal:27017/db?authSource=admin&connectTimeoutMS=5000"
Defensive patterns
Strategy: retry
Validate before calling
// reachability precheck before Setup:
conn, err := net.DialTimeout("tcp", hostPort, 5*time.Second)
if err != nil { return fmt.Errorf("mongo unreachable at %s: %w", hostPort, err) }
conn.Close() Try / catch
client, err := newClient(ctx, uri)
if err != nil {
if isNetworkError(err) { /* backoff + retry Setup */ }
if strings.Contains(err.Error(), "auth") { return ErrMongoAuth }
return err
} Prevention
- Open firewall/security-group egress from Beam workers to MongoDB on 27017 (or 443/srv).
- Ensure the replica set has a PRIMARY elected before starting pipelines.
- Set connectTimeoutMS/serverSelectionTimeoutMS in the URI for fail-fast behavior.
- Verify credentials and authSource against mongosh before deployment.
- Add readiness checks that ping MongoDB before launching the job.
When it happens
Trigger: Calling Setup where the MongoDB host is unreachable (wrong host/port, DNS failure for mongodb+srv), TLS handshake fails, or credentials are wrong (auth failure on ping).
Common situations: Firewall/security-group blocking port 27017; MongoDB not running or replica set not elected a primary; wrong authSource or credentials; VPC/network misconfig in containerized or Beam worker environments; TLS CA mismatch.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
Related errors
- error connecting to MongoDB
- error connecting to job server at
- error connecting to NATS
- error disconnecting from MongoDB
- failed to connect
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/dd31fc7a160737a8.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/go/pkg/beam/io/mongodbio/common.go:65
fn.client = client
}
fn.collection = fn.client.Database(fn.Database).Collection(fn.Collection)
return nil
}
func newClient(ctx context.Context, uri string) (*mongo.Client, error) {
opts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, fmt.Errorf("error connecting to MongoDB: %w", err)
}
if err := client.Ping(ctx, readpref.Primary()); err != nil {
return nil, fmt.Errorf("error pinging MongoDB: %w", err)
}
return client, nil
}
func (fn *mongoDBFn) Teardown(ctx context.Context) error {
if err := fn.client.Disconnect(ctx); err != nil {
return fmt.Errorf("error disconnecting from MongoDB: %w", err)
}
return nil
}
type documentID struct {
ID any `bson:"_id"`
}
func findID(View on GitHub (pinned to 12126d8942)