Skip to content

Can't connect to localhost, but 127.0.0.1 works #1653

Description

@tyilo

Versions/Environment

  1. What version of Rust are you using?
    1.94.1

  2. What operating system are you using?
    Linux

  3. What versions of the driver and its dependencies are you using? (Run cargo pkgid mongodb & cargo pkgid bson)

registry+https://github.com/rust-lang/crates.io-index#mongodb@3.5.2
registry+https://github.com/rust-lang/crates.io-index#bson@3.1.0
  1. What version of MongoDB are you using? (Check with the MongoDB shell using db.version())
    8.0.20

  2. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
    standalone

Describe the bug

I can connect to mongodb://127.0.0.1:27017/test but connecting to mongodb://localhost:27017/test doesn't work. Maybe this is a IPv6 vs IPv4 issue?

To Reproduce

Cargo.toml:

[package]
name = "test-connection"
version = "0.1.0"
edition = "2024"

[dependencies]
mongodb = { version = "3.5.2", default-features = false, features = ["dns-resolver", "rustls-tls", "compat-3-3-0", "bson-3"] }
tokio = { version = "1.50.0", features = ["full"] }

src/main.rs:

use mongodb::bson::{Document, doc};

async fn connect_to_database() -> mongodb::error::Result<mongodb::Database> {
    let client_options =
        mongodb::options::ClientOptions::parse(std::env::var("MONGODB_URL").unwrap()).await?;
    let default_database = client_options.default_database.as_ref().unwrap().clone();

    let client = mongodb::Client::with_options(client_options)?;
    Ok(client.database(&default_database))
}

#[tokio::main]
async fn main() {
    let db = connect_to_database().await.unwrap();
    let collection = db.collection::<Document>("test");
    let docs = collection.count_documents(doc! {}).await.unwrap();
    println!("{docs} documents in test collection");
}

Run a mongo server using podman:

$ podman run --rm -it -p 27017:27017 mongo:8.0.20

See that it is running:

$ podman ps
CONTAINER ID  IMAGE                        COMMAND     CREATED         STATUS         PORTS                     NAMES
9898fac8d819  docker.io/library/mongo:8.0  mongod      22 seconds ago  Up 22 seconds  0.0.0.0:27017->27017/tcp  agitated_lewin

See that connecting to 127.0.0.1 works:

$ MONGODB_URL=mongodb://127.0.0.1:27017/test cargo run
...
0 documents in test collection

See that connecting to localhost doesn't work (fails after 30 seconds):

$ MONGODB_URL=mongodb://localhost:27017/test cargo run
...
thread 'main' (141261) panicked at src/main.rs:16:58:
called `Result::unwrap()` on an `Err` value: Error { kind: ServerSelection { message: "Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: localhost:27017, Type: Unknown, Error: Kind: I/O error: Connection reset by peer (os error 104), labels: {}, source: None, server response: None } ] }" }, labels: {}, wire_version: None, source: None, server_response: None }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions