
本文详解为何调用 `mlflow.set_experiment()` 报连接拒绝错误,核心原因在于未启动本地 mlflow 后端服务;只需一条命令启动服务器,即可正常使用实验管理、模型记录与指标追踪功能。
你遇到的错误:
MlflowException: API request to https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365/api/2.0/mlflow/experiments/get-by-name failed with exception ... Caused by NewConnectionError(... [WinError 10061] Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée)
本质上不是权限或认证问题,而是 MLflow 客户端试图连接一个根本不存在的服务端口 —— 你在代码中执行了:
mlflow.set_tracking_uri("https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365")
mlflow.set_experiment("MLflow Quickstart")但此时本地并未运行 MLflow Tracking Server,端口 8080 处没有任何服务监听,因此操作系统直接拒绝连接(Windows 错误码 10061 即明确表示“目标机器主动拒接”)。
✅ 正确做法:在运行 Python 脚本前,先启动 MLflow 后端服务。
? 启动本地 MLflow Tracking Server
打开终端(Windows PowerShell / macOS/Linux Terminal),执行以下命令:
mlflow server \ --host 127.0.0.1 \ --port 8080 \ --backend-store-uri sqlite:///mlflow.db \ --default-artifact-root ./mlruns
? 参数说明:
- --host 和 --port:指定服务监听地址(与代码中 set_tracking_uri 保持一致);
- --backend-store-uri:使用 SQLite 数据库存储实验、运行元数据(首次运行会自动创建 mlflow.db);
- --default-artifact-root:指定模型、日志等二进制文件的本地存储路径(默认为当前目录下的 ./mlruns)。
✅ 提示:若提示 mlflow: command not found,请先安装: pip install mlflow
✅ 验证服务已就绪
启动后,终端将输出类似日志:
Running the mlflow tracking server on https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365
此时打开浏览器访问 https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365,即可看到 MLflow UI 界面 —— 这是关键验证步骤。
? 完整可运行示例(含实验创建 + 训练记录)
启动服务后,再运行你的 Python 脚本(推荐添加异常处理和上下文管理):
import mlflow
from mlflow.models import infer_signature
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# ✅ 确保 URI 与启动 server 时一致
mlflow.set_tracking_uri("https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365")
mlflow.set_experiment("MLflow Quickstart") # 自动创建实验(若不存在)
# 开始一次训练运行
with mlflow.start_run():
# 加载数据
X, y = datasets.make_classification(n_samples=1000, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# 记录参数与指标
mlflow.log_param("model_type", "LogisticRegression")
mlflow.log_metric("accuracy", accuracy_score(y_test, y_pred))
mlflow.log_metric("precision", precision_score(y_test, y_pred))
mlflow.log_metric("recall", recall_score(y_test, y_pred))
mlflow.log_metric("f1", f1_score(y_test, y_pred))
# 记录模型(可选)
signature = infer_signature(X_train, model.predict(X_train))
mlflow.sklearn.log_model(model, "model", signature=signature)
print("✅ Run completed. Check https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365 for results.")运行后,刷新 MLflow UI 页面,即可看到新实验、运行记录、指标图表与模型卡片。
⚠️ 注意事项
- 服务需持续运行:MLflow Server 是长期进程,不要关闭终端;建议使用 nohup(Linux/macOS)或后台任务(Windows)守护。
- 端口冲突? 若 8080 已被占用,可换用 --port 5000,并同步更新 set_tracking_uri。
- 跨环境注意路径:--default-artifact-root 使用相对路径时,确保 Python 脚本与 Server 启动目录一致,或改用绝对路径(如 file:///full/path/to/mlruns)。
- 无需 Databricks:本地 SQLite + 文件系统完全满足学习与中小项目需求;Databricks 是云托管方案,非必需。
✅ 总结
| 问题现象 | 根本原因 | 解决动作 |
|---|---|---|
| Connection refused (WinError 10061) | 未启动 MLflow Tracking Server | 执行 mlflow server --host 127.0.0.1 --port 8080 ... |
| set_experiment() 失败 | 客户端无法连接后端 API | 先启服务 → 再跑代码 → 最后查 UI |
只要服务就绪,mlflow.set_experiment() 将自动创建实验(若不存在),后续所有 log_param/log_metric/log_model 均可正常持久化。现在,你已具备完整的本地 MLflow 追踪能力 —— 从零部署,开箱即用。










