如何在本地启动 MLflow 跟踪服务器并正确设置实验
技术百科
花韻仙語
发布时间:2026-01-22
浏览: 次 本文详解为何调用 mlflow.set_experiment() 会报连接拒绝错误,并手把手教你启动本地 mlflow 后端服务,完成实验创建、模型训练与指标记录的完整闭环。
MLflow 的跟踪(Tracking)功能依赖一个后端服务来持久化实验、运行、参数和指标。当你执行 mlflow.set_experiment(

✅ 正确做法:先启动 MLflow Tracking Server,再运行你的 Python 脚本。
1. 启动本地 MLflow 服务
在终端(Windows PowerShell / CMD / 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(pip install mlflow)。启动成功后,终端将显示类似 Running the MLflow tracking server at http://127.0.0.1:8080 的提示,并自动打开 Web UI(浏览器访问 http://127.0.0.1:8080 即可查看实验仪表盘)。
2. 修改并运行你的 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 和实验
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("MLflow Quickstart")
# 开始一次新运行
with mlflow.start_run():
# 加载数据
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42
)
# 训练模型
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# 记录参数与指标
mlflow.log_param("model_type", "LogisticRegression")
mlflow.log_param("max_iter", 200)
mlflow.log_metric("accuracy", accuracy_score(y_test, y_pred))
mlflow.log_metric("precision", precision_score(y_test, y_pred, average="weighted"))
mlflow.log_metric("recall", recall_score(y_test, y_pred, average="weighted"))
mlflow.log_metric("f1", f1_score(y_test, y_pred, average="weighted"))
# 记录模型(自动推断 signature)
signature = infer_signature(X_train, clf.predict(X_train))
mlflow.sklearn.log_model(clf, "model", signature=signature)
print("✅ Run logged successfully! Check http://127.0.0.1:8080")⚠️ 注意事项
- 服务必须前置运行:Python 脚本不能替代 mlflow server;它只是客户端。
- 端口冲突? 若 8080 已被占用,可改用 --port 5000 并同步更新 set_tracking_uri("http://127.0.0.1:5000")。
- 权限问题(Windows):某些环境需以管理员身份运行终端启动服务;若仍报错,尝试关闭防火墙或杀毒软件临时拦截。
- Databricks 替代方案:若注册 Databricks 账户持续失败,推荐使用 Databricks Community Edition(免费且无需人工审核),或坚持本地 SQLite + 文件存储方案,完全满足学习与中小项目需求。
至此,你已打通 MLflow 本地跟踪全流程:服务启动 → 实验定义 → 运行记录 → Web 可视化。后续可进一步探索模型注册(Model Registry)、项目打包(Projects)与部署(Models Serving)等高级能力。
# ai
# 后端
# 闭环
# 当你
# python
# windows
# 已被
# 推荐使用
# 首次
# 浏览器
# mac
# win
# ui
# linux
# 防火墙
# 端口
# default
# http
# macos
# cos
# 数据库
# 报错
# 杀毒软件
# 根本原因
# 会报
# 手把手教你
# sqlite
# pip
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何在 Pandas 中按元素交集合并两列字符串
- php删除数据怎么加限制_带where条件删除避免
- Python抽象类与接口设计_规范说明【指导】
- 新手学PHP架构总混淆概念咋办_重点梳理【教程】
- Win11怎么关闭搜索历史_Win11清除设备上的
- 如何优化Golang程序CPU性能_Golang
- 如何使用Golang安装API文档生成工具_快速生
- Win10怎样清理C盘Steam游戏缓存_Win1
- Win11如何设置环境变量 Win11添加和修改系
- 如何使用Golang实现错误包装与传递_Golan
- 如何优化Golang Web性能_Golang H
- Python文本编码与解码_跨平台解析说明【指导】
- c++ try_emplace用法_c++ map
- php8.4如何实现队列任务_php8.4redi
- win11如何清理传递优化文件 Win11为C盘瘦
- Win11怎么关闭小组件_Win11禁用任务栏天气
- Win11怎么快速锁屏_Win11一键锁屏快捷键W
- 如何使用Golang管理跨项目依赖_Golang多
- 如何在 Go 项目开发中正确处理本地包导入与远程模
- C++如何使用std::optional?(处理可
- Windows的便笺功能如何使用?(桌面备忘技巧)
- Python并发安全问题_资源竞争说明【指导】
- Win11开机自检怎么关闭_跳过Win11开机磁盘
- Go语言中slice追加操作的底层共享机制详解
- Win11怎么设置按流量计费_Win11限制后台流
- Python装饰器设计思路_功能增强机制说明【指导
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- C++ static_cast和dynamic_c
- PHP怎么接收URL中的锚点参数_获取#后面参数值
- c++如何获取map中所有的键_C++遍历键值对提
- Win11关机界面怎么改_Win11自定义关机画面
- Win10怎样设置多显示器_Win10多显示器扩展
- Mac如何设置动态壁纸?(让桌面动起来)
- 如何在 Python 测试中动态配置 @backo
- Python与OpenAI接口集成实战_生成式AI
- Win11怎么设置默认邮件应用_Windows11
- Windows资源管理器总是卡顿或重启怎么办?(修
- 如何从 Go 的 map[string]inter
- c++如何用AFL++进行模糊测试 c++ Fuz
- Windows10电脑怎么设置虚拟内存_Win10
- Win11输入法切换快捷键怎么改_Windows
- Win11怎么关闭开机声音_Win11系统启动提示
- Windows 10怎么隐藏特定更新补丁_Wind
- 如何在Golang中使用replace替换模块_指
- Python对象比较与排序_集合使用说明【指导】
- Windows10如何更改桌面背景_Win10个性
- 如何在 Laravel 中通过嵌套关联关系进行 o
- Win11怎样安装微信开发者工具_Win11安装开
- php485函数执行慢怎么优化_php485性能提
- Win11怎么查看激活状态_查询Windows 1

QQ客服