-
Notifications
You must be signed in to change notification settings - Fork 21
Update and restructure MySQL integration docs #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,199 @@ | ||||||
| --- | ||||||
| title: MySQL Client integration reference | ||||||
| description: Learn how to use the Aspire MySQL Client integration to query MySQL databases from your Aspire projects. | ||||||
| --- | ||||||
|
|
||||||
| import { Image } from 'astro:assets'; | ||||||
| import InstallPackage from '@components/InstallPackage.astro'; | ||||||
| import InstallDotNetPackage from '@components/InstallDotNetPackage.astro'; | ||||||
| import { Aside, Code, Steps, Tabs, TabItem } from '@astrojs/starlight/components'; | ||||||
| import mysqlIcon from '@assets/icons/mysqlconnector-icon.png'; | ||||||
|
|
||||||
| <Image | ||||||
| src={mysqlIcon} | ||||||
| alt="MySQL logo" | ||||||
| width={100} | ||||||
| height={100} | ||||||
| class:list={'float-inline-left icon'} | ||||||
| data-zoom-off | ||||||
| /> | ||||||
|
|
||||||
| To get started with the Aspire MySQL integrations, follow the [Get started with MySQL integrations](../mysql-get-started/) guide. | ||||||
|
|
||||||
| This article includes full details about the Aspire MySQL Client integration, which allows you to connect to and interact with MySQL databases from your Aspire consuming projects. | ||||||
|
|
||||||
| ## Installation | ||||||
|
|
||||||
| To get started with the Aspire MySQL client integration, install the [📦 Aspire.MySqlConnector](https://www.nuget.org/packages/Aspire.MySqlConnector) NuGet package in the client-consuming project, that is, the project for the application that uses the MySQL client. The MySQL client integration registers a `MySqlConnector.MySqlDataSource` instance that you can use to interact with MySQL. | ||||||
|
|
||||||
| <InstallDotNetPackage packageName="Aspire.MySqlConnector" /> | ||||||
|
|
||||||
| ## Add a MySQL data source | ||||||
|
|
||||||
| In the `Program.cs` file of your client-consuming project, call the `AddMySqlDataSource` extension method to register a `MySqlDataSource` for use via the dependency injection container: | ||||||
|
|
||||||
| ```csharp title="C# — Program.cs" | ||||||
| builder.AddMySqlDataSource(connectionName: "mysqldb"); | ||||||
| ``` | ||||||
|
|
||||||
| <Aside type="tip"> | ||||||
| The `connectionName` parameter must match the name used when adding the MySQL database resource in the AppHost project. | ||||||
| </Aside> | ||||||
|
|
||||||
| You can then retrieve the `MySqlConnector.MySqlDataSource` instance using dependency injection: | ||||||
|
|
||||||
| ```csharp title="C# — ExampleService.cs" | ||||||
| public class ExampleService(MySqlDataSource dataSource) | ||||||
| { | ||||||
| // Use dataSource... | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Add keyed MySQL data source | ||||||
|
|
||||||
| There might be situations where you want to register multiple `MySqlDataSource` instances with different connection names. To register keyed MySQL data sources, call the `AddKeyedMySqlDataSource` method: | ||||||
|
|
||||||
| ```csharp title="C# — Program.cs" | ||||||
| builder.AddKeyedMySqlDataSource(name: "mainDb"); | ||||||
| builder.AddKeyedMySqlDataSource(name: "loggingDb"); | ||||||
| ``` | ||||||
|
|
||||||
| <Aside type="danger"> | ||||||
| When using keyed services, it's expected that your MySQL resource configured two named databases, one for the `mainDb` and one for the `loggingDb`. | ||||||
| </Aside> | ||||||
|
|
||||||
| Then you can retrieve the `MySqlDataSource` instances using dependency injection: | ||||||
|
|
||||||
| ```csharp title="C# — ExampleService.cs" | ||||||
| public class ExampleService( | ||||||
| [FromKeyedServices("mainDb")] MySqlDataSource mainDataSource, | ||||||
| [FromKeyedServices("loggingDb")] MySqlDataSource loggingDataSource) | ||||||
| { | ||||||
| // Use data sources... | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| For more information on keyed services, see [.NET dependency injection: Keyed services](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection#keyed-services). | ||||||
|
|
||||||
| ## Properties of the MySQL resources | ||||||
|
|
||||||
| When you use the `WithReference` method to pass a MySQL server or database resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project. | ||||||
|
|
||||||
| Aspire exposes each property as an environment variable named `[RESOURCE]_[PROPERTY]`. For instance, the `Uri` property of a resource called `mysqldb` becomes `MYSQLDB_URI`. | ||||||
|
|
||||||
| ### MySQL server resource | ||||||
|
|
||||||
| The MySQL server resource exposes the following connection properties: | ||||||
|
|
||||||
| | Property Name | Description | | ||||||
| |---------------|-------------| | ||||||
| | `Host` | The hostname or IP address of the MySQL server | | ||||||
| | `Port` | The port number the MySQL server is listening on | | ||||||
| | `Username` | The username for authentication | | ||||||
| | `Password` | The password for authentication | | ||||||
| | `Uri` | The connection URI in mysql:// format, with the format `mysql://{Username}:{Password}@{Host}:{Port}` | | ||||||
| | `JdbcConnectionString` | JDBC-format connection string, with the format `jdbc:mysql://{Host}:{Port}`. User and password credentials are provided as separate `Username` and `Password` properties. | | ||||||
|
|
||||||
| ### MySQL database resource | ||||||
|
|
||||||
| The MySQL database resource inherits all properties from its parent `MySqlServerResource` and adds: | ||||||
|
|
||||||
| | Property Name | Description | | ||||||
| |---------------|-------------| | ||||||
| | `Uri` | The connection URI with the database name, with the format `mysql://{Username}:{Password}@{Host}:{Port}/{DatabaseName}` | | ||||||
|
||||||
| | `Uri` | The connection URI with the database name, with the format `mysql://{Username}:{Password}@{Host}:{Port}/{DatabaseName}` | | |
| | `Uri` | The connection URI, with the format `mysql://{Username}:{Password}@{Host}:{Port}`. The database name is provided separately as the `DatabaseName` property. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The URI format description has inconsistent formatting in the backticks. The format string
mysql://{Username}:{Password}@{Host}:{Port}should match the style of other format strings. However, more importantly, this format appears to be incomplete or incorrect - MySQL URIs typically don't use themysql://scheme. The standard connection URI format for MySQL would be more likeServer={Host};Port={Port};User ID={Username};Password={Password}or similar. Please verify the correct URI format for MySQL connections.