Skip to content
Docs Try Aspire
Docs Try

Set up MySQL in the AppHost

MySQL logo

This article is the reference for the Aspire MySQL Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model MySQL server and database resources in your AppHost project.

If you’re new to the MySQL integration, start with the Get started with MySQL integrations guide. For how consuming apps read the connection information this page exposes, see Connect to MySQL. For the MySQL Entity Framework Core (EF Core) client integration, see Get started with the MySQL Entity Framework Core integrations.

To start building an Aspire app that uses MySQL, install the 📦 Aspire.Hosting.MySql NuGet package:

Terminal
aspire add mysql

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.MySql@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.MySql" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add a MySQL server resource and then add a database resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithLifetime(ContainerLifetime.Persistent);
var mysqldb = mysql.AddDatabase("mysqldb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the docker.io/library/mysql image, it creates a new MySQL server instance on your local machine. A reference to the mysqldb database resource is used to add a dependency to the consuming project.

  2. The MySQL server resource includes default credentials with a username of root and a randomly generated password using the CreateDefaultPasswordParameter method.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced database resource, such as mysqldb in the preceding example.

You can use the WithCreationScript method to execute SQL scripts when the database is created. This is useful for initializing database schema or seeding data:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithLifetime(ContainerLifetime.Persistent);
var mysqldb = mysql.AddDatabase("mysqldb")
.WithCreationScript("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
""");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

phpMyAdmin is a popular web-based administration tool for MySQL. Call the WithPhpMyAdmin (or withPhpMyAdmin) method to add a phpMyAdmin container resource that connects to the MySQL container:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithPhpMyAdmin();
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

When you run the solution, the Aspire dashboard displays the phpMyAdmin resource with an endpoint. Select the endpoint link to open phpMyAdmin in a new browser tab.

To set a custom container name for the phpMyAdmin resource in TypeScript:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithPhpMyAdmin(phpMyAdmin => phpMyAdmin.WithHostPort(8080));
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

Add a data volume to the MySQL server resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithDataVolume(isReadOnly: false);
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

The data volume is used to persist the MySQL server data outside the lifecycle of its container. The data volume is mounted at the /var/lib/mysql path in the MySQL container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add a MySQL resource with a data bind mount

Section titled “Add a MySQL resource with a data bind mount”

Add a data bind mount to the MySQL server resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithDataBindMount(
source: "/MySQL/Data",
isReadOnly: false);
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist the MySQL server data across container restarts. The data bind mount is mounted at the C:\MySQL\Data on Windows (or /MySQL/Data on Unix) path on the host machine in the MySQL server container. For more information on data bind mounts, see Docker docs: Bind mounts.

Use initialization files to seed or configure the MySQL server at startup. The C# AppHost exposes WithInitBindMount(...), while the TypeScript AppHost exposes withInitFiles(...).

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithInitBindMount(@"C:\MySQL\Init");
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

The init files are executed when the MySQL server container starts. Place executable shell scripts or .sql files in the directory to initialize the database schema, seed data, or perform other setup tasks.

When you want to provide a root MySQL password and optional port explicitly, you can pass them as parameters:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("password", secret: true);
var mysql = builder.AddMySql("mysql", password)
.WithLifetime(ContainerLifetime.Persistent);
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);
// After adding all resources, run the app...

When no password parameter is provided, Aspire generates a strong password automatically using the CreateDefaultPasswordParameter method. For more information, see External parameters.

By default, Aspire injects the MySQL connection information using variable names derived from the resource name (for example, MYSQLDB_URI, MYSQLDB_HOST, MYSQLDB_PORT, MYSQLDB_PASSWORD). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql");
var mysqldb = mysql.AddDatabase("mysqldb");
var app = builder.AddExecutable("my-app", "node", "app.js", ".")
.WithReference(mysqldb)
.WithEnvironment(context =>
{
context.EnvironmentVariables["MYSQL_HOST"] = mysql.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
context.EnvironmentVariables["MYSQL_PORT"] = mysql.Resource.PrimaryEndpoint.Property(EndpointProperty.Port);
context.EnvironmentVariables["MYSQL_PASSWORD"] = mysql.Resource.PasswordParameter;
context.EnvironmentVariables["MYSQL_DATABASE"] = mysqldb.Resource.DatabaseName;
});
builder.Build().Run();

For the full reference of MySQL connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to MySQL.

The MySQL hosting integration automatically adds a health check for the MySQL resource. The health check verifies that the MySQL server is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.MySql NuGet package.