GitHub操作无法连接到MongoDB服务 [英] GitHub actions cannot connect to MongoDB service
本文介绍了GitHub操作无法连接到MongoDB服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用GitHub操作运行自动测试时遇到问题。我不明白为什么我不能连接运行我的集成测试的MongoDB服务。我尝试了不同的主机:本地主机、127.0.0.1、0.0.0.0,但它们都无法连接到数据库。
它在我的坞站设置中工作得非常好,但由于某种原因,它在GitHub操作中不起作用。
name: CI master
on: [push, pull_request]
env:
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always
APP_ENV: development
APP_MONGO_USER: test
APP_MONGO_PASS: password
APP_MONGO_DB: test
jobs:
# Run tests
test:
name: Test
runs-on: ubuntu-latest
services:
mongo:
image: mongo
env:
MONGO_INITDB_ROOT_USERNAME: ${APP_MONGO_USER}
MONGO_INITDB_ROOT_PASSWORD: ${APP_MONGO_PASS}
MONGO_INITDB_DATABASE: ${APP_MONGO_DB}
ports:
- 27017:27017
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
配置文件(Development ment.toml)。
[application]
host = "127.0.0.1"
port = 8080
正在连接到数据库。环境变量和配置文件被合并,我在这里通过CONFIG:&;设置访问它们。
pub async fn init(config: &Settings) -> Result<Database> {
let client_options = ClientOptions::parse(
format!(
"mongodb://{}:{}@{}:27017",
config.mongo.user, config.mongo.pass, config.application.host
)
.as_str(),
)
.await?;
let client = Client::with_options(client_options)?;
let database = client.database("test"); // TODO: replace with env var
database.run_command(doc! {"ping": 1}, None).await?;
println!("Connected successfully.");
Ok(database)
}
调用init函数。
// Mongo
let mongo = mongo::init(&config).await.expect("Failed to init mongo");
我收到的错误。
thread 'health_check' panicked at 'Failed to init mongo: Error { kind: ServerSelectionError { message: "Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: 127.0.0.1:27017, Type: Unknown, Error: Connection refused (os error 111) }, ] }" }, labels: [] }', tests/health_check.rs:31:44
推荐答案
我最终通过将运行状况检查添加到我的服务中解决了这个问题。我的问题似乎与我的数据库在运行测试之前尚未启动有关。
services:
mongodb:
image: mongo
env:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: test
options: >-
--health-cmd mongo
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 27017:27017
这篇关于GitHub操作无法连接到MongoDB服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文