gpt4 book ai didi

mongodb - 无法连接到 rust 的MongoDB Atlas数据库

转载 作者:行者123 更新时间:2023-12-03 11:43:43 27 4
gpt4 key购买 nike

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

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

我得到的错误是:
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);
}
}
}
}

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

最佳答案

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

首先,编辑Cargo.toml以启用"ssl"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);
}
}
}
}

另外,您的 mongodb_uri看起来不正确。您也应该检查一下。

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

(引用文献说您需要启用 "ssh"功能,但这是一个错字。 mongodb crate 没有这种功能。)

关于mongodb - 无法连接到 rust 的MongoDB Atlas数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58682911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com