콘텐츠로 이동

MySQL Hosting integration reference

이 콘텐츠는 아직 번역되지 않았습니다.

MySQL logo

To get started with the Aspire MySQL integrations, follow the Get started with MySQL integrations guide. If you prefer to use Entity Framework Core (EF Core) to interact with your MySQL database, see the Get started with MySQL and EF Core guide.

This article includes full details about the Aspire MySQL Hosting integration, which models the server as the MySqlServerResource type and the database as the MySqlDatabaseResource type. To access these types and APIs, you need to install the MySQL Hosting integration in your AppHost project.

To get started with the Aspire MySQL hosting integration, install the 📦 Aspire.Hosting.MySql NuGet package in your AppHost project:

Aspire CLI — Aspire.Hosting.MySql 패키지 추가
aspire add mysql

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> mysql (Aspire.Hosting.MySql)
> Other results listed as selectable options...

Add MySQL server resource and database resource

Section titled “Add MySQL server resource and database resource”

In your AppHost project, call AddMySql to add and return a MySQL resource builder. Chain a call to the returned resource builder to AddDatabase, to add a MySQL database resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithLifetime(ContainerLifetime.Persistent);
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);

When Aspire adds a container image to the AppHost, it creates a new MySQL instance on your local machine. The MySQL resource includes default credentials with a username of root and a random password generated using the default password parameter.

When the AppHost runs, the password is stored in the AppHost’s secret store in the Parameters section:

{
"Parameters:mysql-password": "<THE_GENERATED_PASSWORD>"
}

The WithReference method configures a connection in the ExampleProject named mysqldb.

Add MySQL server resource with database scripts

Section titled “Add MySQL server resource with database scripts”

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);

To add a data volume to the MySQL resource, call the WithDataVolume method on the MySQL resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithDataVolume();
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);

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”

To add a data bind mount to the MySQL resource, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mysql = builder.AddMySql("mysql")
.WithDataBindMount(source: @"C:\MySql\Data");
var mysqldb = mysql.AddDatabase("mysqldb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(mysqldb)
.WaitFor(mysqldb);

Data bind mounts rely on the host machine’s filesystem to persist the MySQL data across container restarts. For more information on data bind mounts, see Docker docs: Bind mounts.

When you want to provide a root MySQL password explicitly, you can pass it as a parameter:

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);

For more information, see External parameters.

phpMyAdmin is a popular web-based administration tool for MySQL. To use phpMyAdmin within your Aspire solution, call the WithPhpMyAdmin method. This method adds a new container resource that hosts phpMyAdmin and connects it 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);

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

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.