Sub topic 3
Running a Tracking Server That Supports the Registry
This chapter assumes you know MLflow experiments, runs, parameters, metrics, and artifacts. You should also know how to create a Python virtual environment and run commands from a terminal.
Why the server configuration matters
The MLflow Model Registry stores named models and their versions. Each version points to a model artifact logged by an MLflow run. The registry therefore needs access to two different kinds of storage:
- The backend store holds tracking and registry metadata, such as experiments, runs, registered model names, version numbers, tags, and aliases.
- The artifact store holds files, such as serialized models, dependencies, signatures, and plots.
For a local tutorial, SQLite is a convenient backend store and a directory on disk is a convenient artifact store.
A file-based backend such as ./mlruns may work for basic local tracking, but a database backend is the appropriate choice when you need the Model Registry. SQLite also gives you transactions and a structured store that can later be replaced by a production database.
Start a local registry-capable server
Install MLflow in the active virtual environment before starting the server:
pip install mlflow scikit-learnRun this command from a directory where the MLflow database and artifact directory may be created:
mlflow server \
--backend-store-uri sqlite:///mlflow.db \
--default-artifact-root ./mlartifacts \
--host 127.0.0.1 \
--port 5000The sqlite:///mlflow.db URI tells MLflow to use the mlflow.db file as its backend store. The three slashes mean that the path is relative to the current directory. Use an absolute path when the server must start from different directories.
The --default-artifact-root option sets the location for artifacts created by runs. Keep this directory available to the server process. A relative path is resolved using the server's working directory, so an absolute path is safer for scripts and services.
The server listens at http://127.0.0.1:5000. Leave this terminal running while you use MLflow from another terminal.
Point clients at the server
Every training or deployment process that should use this server must set its tracking URI. The URI controls where MLflow sends run and registry requests.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("server-check")
with mlflow.start_run() as run:
mlflow.log_param("server", "local")
mlflow.log_metric("accuracy", 0.91)
print(f"Run ID: {run.info.run_id}")After this script runs, the experiment and run metadata should be in mlflow.db. Any logged files should be under mlartifacts. You can inspect runs in the MLflow UI at http://127.0.0.1:5000.
Check the configuration
| Setting | Example | Purpose |
|---|---|---|
| Tracking URI | `http://127.0.0.1:5000` | Destination for client requests |
| Backend store | `sqlite:///mlflow.db` | Registry and run metadata |
| Artifact root | `./mlartifacts` | Logged model and file storage |
A common error is configuring the client with sqlite:///mlflow.db while expecting it to contact the server. That URI makes the client open the database directly. For a server-based setup, use the HTTP tracking URI in client code.
Another error is starting the server from one directory and looking for artifacts from another. Record the server's working directory, or configure an absolute artifact path.
Exercise
Which setting stores registered model metadata, and which setting stores the serialized model files?
Show answer
The backend store, such as SQLite, stores registry metadata. The artifact root stores serialized models and other logged files.