无法生锈连接到MongoDB Atlas数据库

时间:2019-11-03 17:44:09

标签: mongodb rust mongodb-atlas

我是rust的n00b用户,并且我尝试使用the mongodb rust crate v0.4.0顺利连接到MongoDB Atlas MongoDB 4.0版集群上的数据库。

在Atlas中,我已授予防火墙访问权限,并且可以从我的家庭IP连接,因此这不是权限/访问问题。我搜索了错误,但在该主题上找不到太多帮助。看来mongodb锈驱动程序还很年轻,还没有完全烘焙。

我得到的错误是:

thread 'main' panicked at 'called Result::unwrap() on an Err value: OperationError("No servers available for the provided ReadPreference.")'

下面的回溯详细信息:

Running `target/debug/gigs-core`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: OperationError("No servers available for the provided ReadPreference.")', src/libcore/result.rs:1165:5
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1028
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1412
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:188
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:205
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:464
  11: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:373
  12: rust_begin_unwind
             at src/libstd/panicking.rs:302
  13: core::panicking::panic_fmt
             at src/libcore/panicking.rs:139
  14: core::result::unwrap_failed
             at src/libcore/result.rs:1165
  15: core::result::Result<T,E>::unwrap
             at /rustc/91fd6283e658e2c7aab2d3f5206fc1891f486af2/src/libcore/result.rs:933
  16: gigs_core::main
             at src/main.rs:12
  17: std::rt::lang_start::{{closure}}
             at /rustc/91fd6283e658e2c7aab2d3f5206fc1891f486af2/src/libstd/rt.rs:61
  18: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:48
  19: std::panicking::try::do_call
             at src/libstd/panicking.rs:287
  20: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:83
  21: std::panicking::try
             at src/libstd/panicking.rs:265
  22: std::panic::catch_unwind
             at src/libstd/panic.rs:396
  23: std::rt::lang_start_internal
             at src/libstd/rt.rs:47
  24: std::rt::lang_start
             at /rustc/91fd6283e658e2c7aab2d3f5206fc1891f486af2/src/libstd/rt.rs:61
  25: gigs_core::main

这是我正在运行的代码,显然已提取:

use mongodb::{Bson, bson, doc};
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;

fn main() {
    let mongodb_uri = String::from("mongodb://cluster0-kbbbhky.mongodb.net:2017, \
        cluster0-shard-00-00-kbbbhky.mongodb.net:27017, \
        cluster0-shard-00-00-kbbbhky.mongodb.net:27017/"
    );
    let client = Client::with_uri(&mongodb_uri)    
        .expect("Failed to initialize client.");
    client.db("admin").auth("username","password").unwrap();
    let coll = client.db("gigs_core").collection("events");
    let cursor = coll.find(None, None).unwrap();
    for result in cursor{
        if let Ok(item) = result {
            if let Some(&Bson::String(ref name)) = item.get("name"){
                println!("name : {}",name);
            }
        }
    }
}

外面有没有人建议使用锈连接到MongoDB Atlas数据库?

1 个答案:

答案 0 :(得分:1)

我之前也遇到过类似的问题,并使用with_unauthenticated_ssl选项进行了修复。 (看来MongoDB Atlas需要SSL连接。)

首先,编辑Cargo.toml以启用"ssl"的{​​{1}}功能

mongodb

,然后使用mongodb = { version = "0.4.0", features = ["ssl"] } 选项创建客户端。

with_unauthenticated_ssl

此外,您的use mongodb::{Bson, bson, doc}; use mongodb::{Client, ClientOptions, ThreadedClient}; use mongodb::db::ThreadedDatabase; fn main() { let mongodb_uri = String::from("mongodb://cluster0-kbbbhky.mongodb.net:2017, \ cluster0-shard-00-00-kbbbhky.mongodb.net:27017, \ cluster0-shard-00-00-kbbbhky.mongodb.net:27017/" ); // Define options let opts = ClientOptions::with_unauthenticated_ssl(None, false); // Initialize with options let client = Client::with_uri_and_options(&mongodb_uri, opts) .expect("Failed to initialize client."); client.db("admin").auth("username","password").unwrap(); let coll = client.db("gigs_core").collection("events"); let cursor = coll.find(None, None).unwrap(); for result in cursor{ if let Ok(item) = result { if let Some(&Bson::String(ref name)) = item.get("name"){ println!("name : {}",name); } } } } 看起来不正确。您也应该检查一下。

参考:https://medium.com/@shnarazk/connecting-mongodb-atlas-from-rust-programs-65e4cbcbd964

(参考文献说您需要启用mongodb_uri功能,但这是一个错字。"ssh"板条箱没有此功能。)