Sub topic 3
Running a Tracking Server That Supports the Registry
This chapter assumes you can run MLflow Tracking from Python: you know experiments, runs, parameters, metrics, and artifacts. It also assumes you have a virtual environment, a shell, and a basic idea of what a SQL database is. By the end you will have a local server where model registration works, and you will know what changes when you move it to production.
Why the registry needs a server with a database
The Model Registry stores structured metadata: registered model names, version numbers, tags, descriptions, and aliases. Each version also points back to the run that produced it. MLflow keeps this metadata in a backend store that supports transactional updates and queries. In practice that means a SQL database.
The default local setup writes runs to a ./mlruns directory of plain files. That file store works for tracking experiments, but it does not support the registry. If you point registry calls at it, MLflow raises an exception saying the registry is unavailable for that URI and lists the supported schemes (sqlite, postgresql, mysql, mssql). That exception is the most common first error, and the fix is to run the server with a database URI.
A server also gives you one shared place. Every teammate, training job, and serving process talks to the same registry over HTTP, so an alias that one person moves is the alias everyone else sees.
Two stores, two jobs
An MLflow server manages two kinds of storage. Registry data lives in the first one, and model files live in the second.
| Store | Holds | Server flag | Typical production choice |
|---|---|---|---|
| Backend store | Experiments, runs, params, metrics, tags, registered models, model versions, aliases | `--backend-store-uri` | PostgreSQL or MySQL |
| Artifact store | Model files, plots, and other run artifacts | `--default-artifact-root` or `--artifacts-destination` | S3, Azure Blob, GCS, or a shared filesystem |
A registered model version does not copy the model files. It stores a reference to the model artifact that a run logged. If you delete the artifact store contents, the registry still lists the version, but loading it fails. Back up both stores together.
By default the registry uses the same database as the backend store. A separate --registry-store-uri flag exists for splitting them, but most teams never need it.
Start a local server
Install the packages used in this tutorial inside your virtual environment.
pip install mlflow scikit-learnThen start the server with a SQLite database and a local artifact directory.
mlflow server \
--backend-store-uri sqlite:///mlflow.db \
--default-artifact-root ./mlartifacts \
--host 127.0.0.1 \
--port 5000Each flag does one thing:
--backend-store-uri sqlite:///mlflow.dbcreates or opens the SQLite filemlflow.dbin the directory where you ran the command. MLflow creates the tables on first start. This is the setting that makes the registry available.--default-artifact-root ./mlartifactsis where new experiments store artifacts. The path is resolved relative to the server's working directory.--host 127.0.0.1accepts connections from this machine only.--port 5000is the HTTP port. Openhttp://127.0.0.1:5000in a browser to see the UI. Registered models appear under the Models tab once you create one.
Leave this terminal running and open a second one for client code.
Verify that the registry works
Before you log anything, check that the server is up and that the registry answers. First test the health endpoint from the shell.
curl http://127.0.0.1:5000/healthIt returns OK. Then ask the registry for its models from Python.
import mlflow
from mlflow import MlflowClient
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = MlflowClient()
models = client.search_registered_models()
print(len(models))On a fresh server this prints 0. An empty list is the success signal: the call reached the registry and found nothing yet. If you get an exception about an unsupported URI, the server is running on a file store, so restart it with the --backend-store-uri shown above.
Instead of calling set_tracking_uri in every script, you can set the environment variable once per shell.
export MLFLOW_TRACKING_URI=http://127.0.0.1:5000Both mlflow module functions and MlflowClient() pick it up. An explicit set_tracking_uri call in code overrides it, which is a common source of confusion when two scripts seem to write to different registries.
Where artifacts go, and why it matters
In the local setup the client and server share a machine and a filesystem, so ./mlartifacts works whichever process writes the files. This stops being true when clients run elsewhere. By default, the client writes artifacts directly to the artifact location recorded for the experiment. If that location is a path on the server's disk, a remote client tries to write to that path on its own disk, or fails.
You have two ways to handle this:
- Give every client direct credentials and network access to the artifact store, such as an S3 bucket.
- Let the server proxy artifact traffic. You start it with
--artifacts-destinationpointing at the real storage, and clients upload and download through the server over HTTP. Clients then need no storage credentials.
Whichever you choose, check the artifact location of a new experiment before you rely on it. Proxied artifact behavior and its defaults have changed between MLflow releases, so confirm the flags against the documentation for the version you run.
The artifact location is fixed when an experiment is created. Changing --default-artifact-root later does not move existing experiments, so models logged from them still resolve to the old location.
Moving to a production-style database
SQLite is a single file with limited concurrency. It suits a laptop or a demo, but not several users logging at the same time. For a shared server, use PostgreSQL or MySQL and keep the credentials out of your shell history.
pip install psycopg2-binary boto3
export MLFLOW_DB_URI="postgresql://mlflow:CHANGE_ME@db-host:5432/mlflow"
mlflow server \
--backend-store-uri "$MLFLOW_DB_URI" \
--artifacts-destination s3://my-bucket/mlflow \
--host 0.0.0.0 \
--port 5000Replace the host, bucket, and password with your own values. The database and bucket must exist already, and the server needs permission to use them.
Upgrading the database schema
MLflow versions change the database schema. When you upgrade the mlflow package on the server, run the migration before you start it.
mlflow db upgrade "$MLFLOW_DB_URI"Back up the database first. If a newer client talks to a server whose schema is older than its code expects, requests fail with database errors that do not mention the version mismatch. Keep server and client versions close, and upgrade the server before the clients that talk to it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Exception that registry functionality is unavailable for URI `./mlruns` | The server or client uses the file store | Start the server with a SQL `--backend-store-uri` and point clients at its `http://` URL |
| `Connection refused` on port 5000 | The server is not running, or it is bound to a different host or port | Check the terminal running `mlflow server` and the `--host` and `--port` values |
| Models appear in the wrong registry | An environment variable or `set_tracking_uri` points somewhere else | Print `mlflow.get_tracking_uri()` in the failing script |
| Version exists but loading fails | The artifact store was moved or deleted, or the client cannot reach it | Restore the artifacts, or fix the proxy or credentials |
| Database errors after upgrading MLflow | The schema was not migrated | Run `mlflow db upgrade` against the backend store |
Exercise
You start mlflow server with no --backend-store-uri and your training script calls mlflow.sklearn.log_model(..., registered_model_name="iris-classifier"). The call fails with an error that registry functionality is unavailable. What is the cause, and what two things do you change to fix it?
Show answer
The default backend store is the local ./mlruns file store, which does not support the Model Registry. Restart the server with a database URI such as --backend-store-uri sqlite:///mlflow.db. Then make sure the script points at the server, using mlflow.set_tracking_uri("http://127.0.0.1:5000") or the MLFLOW_TRACKING_URI environment variable.
Exercise
A teammate on another machine can log runs to your shared server, and the registry lists their model version. Loading that version on your machine fails because the model files cannot be found. The server uses --default-artifact-root /srv/mlartifacts. What most likely happened, and what are two ways to fix it?
Show answer
Their client wrote the artifacts to the path /srv/mlartifacts on their own machine, because by default clients write to the artifact location directly and that path is local to whoever writes. The registry stored only a reference to it. Either give clients access to shared artifact storage, such as an S3 bucket, or run the server with --artifacts-destination so it proxies artifact uploads and downloads.