# MySqlBuilderExtensions Methods

- Package: [Aspire.Hosting.MySql](/reference/api/csharp/aspire.hosting.mysql.md)
- Type: [MySqlBuilderExtensions](/reference/api/csharp/aspire.hosting.mysql/mysqlbuilderextensions.md)
- Kind: `Methods`
- Members: `10`

Provides extension methods for adding MySQL resources to an `Hosting.IDistributedApplicationBuilder`.

## AddDatabase(IResourceBuilder<MySqlServerResource>, string, string?)

- Name: `AddDatabase(IResourceBuilder<MySqlServerResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L118-L145)

Adds a MySQL database to the application model.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlDatabaseResource> AddDatabase(
        this IResourceBuilder<MySqlServerResource> builder,
        string name,
        string? databaseName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The MySQL server resource builder.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `databaseName` (`string?`) `optional`
  The name of the database. If not provided, this defaults to the same value as `name`.

## Returns

`IResourceBuilder<MySqlDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

When adding a [MySqlDatabaseResource](/reference/api/csharp/aspire.hosting.mysql/mysqldatabaseresource.md) to your application model the resource can then be referenced by other resources using the resource name. When the dependent resource is using the extension method `ResourceBuilderExtensions.WaitFor` then the dependent resource will wait until the MySQL database is available.

Note that calling [MySqlBuilderExtensions.AddDatabase(IResourceBuilder<MySqlServerResource>, string, string?)](/reference/api/csharp/aspire.hosting.mysql/mysqlbuilderextensions/methods.md#adddatabase-iresourcebuilder-mysqlserverresource-string-string) will result in the database being created on the MySQL server when the server becomes ready. The database creation happens automatically as part of the resource lifecycle.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddMySql(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?)

- Name: `AddMySql(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L38-L90)

Adds a MySQL server resource to the application model. For local development a container is used.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> AddMySql(
        this IDistributedApplicationBuilder builder,
        string name,
        IResourceBuilder<ParameterResource>? password = null,
        int? port = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `password` (`IResourceBuilder<ParameterResource>`) `optional`
  The parameter used to provide the root password for the MySQL resource. If `null` a random password will be generated.
- `port` (`int?`) `optional`
  The host port for MySQL.

## Returns

`IResourceBuilder<MySqlServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithCreationScript(IResourceBuilder<MySqlDatabaseResource>, string)

- Name: `WithCreationScript(IResourceBuilder<MySqlDatabaseResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L193-L198)

Defines the SQL script used to create the database.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlDatabaseResource> WithCreationScript(
        this IResourceBuilder<MySqlDatabaseResource> builder,
        string script)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlDatabaseResource>`)
  The builder for the [MySqlDatabaseResource](/reference/api/csharp/aspire.hosting.mysql/mysqldatabaseresource.md).
- `script` (`string`)
  The SQL script used to create the database.

## Returns

`IResourceBuilder<MySqlDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

Default script is

```csharp
CREATE DATABASE IF NOT EXISTS `QUOTED_DATABASE_NAME`;
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<MySqlServerResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<MySqlServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L356-L359)

Adds a bind mount for the data folder to a MySql container resource.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> WithDataBindMount(
        this IResourceBuilder<MySqlServerResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<MySqlServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<MySqlServerResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<MySqlServerResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L340-L342)

Adds a named volume for the data folder to a MySql container resource.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> WithDataVolume(
        this IResourceBuilder<MySqlServerResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only volume.

## Returns

`IResourceBuilder<MySqlServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<PhpMyAdminContainerResource>, int?)

- Name: `WithHostPort(IResourceBuilder<PhpMyAdminContainerResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<PhpMyAdminContainerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L321-L326)

Configures the host port that the PGAdmin resource is exposed on instead of using randomly assigned port.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<PhpMyAdminContainerResource> WithHostPort(
        this IResourceBuilder<PhpMyAdminContainerResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<PhpMyAdminContainerResource>`)
  The resource builder for PGAdmin.
- `port` (`int?`)
  The port to bind on the host. If `null` is used, a random port will be assigned.

## Returns

`IResourceBuilder<PhpMyAdminContainerResource>` -- The resource builder for PGAdmin.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithInitBindMount(IResourceBuilder<MySqlServerResource>, string, bool)

> **Obsolete:** Use WithInitFiles instead.

- Name: `WithInitBindMount(IResourceBuilder<MySqlServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L372-L375)

Adds a bind mount for the init folder to a MySql container resource.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> WithInitBindMount(
        this IResourceBuilder<MySqlServerResource> builder,
        string source,
        bool isReadOnly = true)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<MySqlServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## WithInitFiles(IResourceBuilder<MySqlServerResource>, string)

- Name: `WithInitFiles(IResourceBuilder<MySqlServerResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L388-L395)

Copies init files into a MySql container resource.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> WithInitFiles(
        this IResourceBuilder<MySqlServerResource> builder,
        string source)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The resource builder.
- `source` (`string`)
  The source file or directory on the host to copy into the container.

## Returns

`IResourceBuilder<MySqlServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPassword(IResourceBuilder<MySqlServerResource>, IResourceBuilder<ParameterResource>)

- Name: `WithPassword(IResourceBuilder<MySqlServerResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MySqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L211-L215)

Configures the password that the MySQL resource uses.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<MySqlServerResource> WithPassword(
        this IResourceBuilder<MySqlServerResource> builder,
        IResourceBuilder<ParameterResource> password)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MySqlServerResource>`)
  The resource builder.
- `password` (`IResourceBuilder<ParameterResource>`)
  The parameter used to provide the password for the MySQL resource.

## Returns

`IResourceBuilder<MySqlServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPhpMyAdmin(IResourceBuilder<T>, Action<IResourceBuilder<PhpMyAdminContainerResource>>, string?)

- Name: `WithPhpMyAdmin(IResourceBuilder<T>, Action<IResourceBuilder<PhpMyAdminContainerResource>>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L232-L309)

Adds a phpMyAdmin administration and development platform for MySql to the application model.

```csharp
public static class MySqlBuilderExtensions
{
    public static IResourceBuilder<T> WithPhpMyAdmin<T>(
        this IResourceBuilder<T> builder,
        Action<IResourceBuilder<PhpMyAdminContainerResource>>? configureContainer = null,
        string? containerName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The MySql server resource builder.
- `configureContainer` (`Action<IResourceBuilder<PhpMyAdminContainerResource>>`) `optional`
  Callback to configure PhpMyAdmin container resource.
- `containerName` (`string?`) `optional`
  The name of the container (Optional).

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
