Setup for Azure Database for MySQL Flexible Server

Setup Guide

Note: The Azure Portal when accessed via Firefox for Mac does not seem to work for all the steps in this guide. Safari was used successfully on a Mac (November 2022).

Setup Through the Azure Portal

Create an Azure Database for MySQL

Open the Azure portal, on the main screen, click Create a Resource.

AzureDbMySql-Create

Click on Databases in the left-side menu, then under Azure Database for MySQL, click on Create.

AzureDbMySqlCreateChoice

 

Select Flexible Server (Reccommended), then click on Create.

AzureDbMySql-DeployOption-Create

 

Configure the MySQL Database Server

On the next screen, fill in all the values as shown in the image below. For some of the fields you will need to make choices or entries that are specific to your database, such as:

Now click on the Networking tab, or Next: Networking

Note: Even though a cost is shown, your Azure for Students credit won't be charged.

AzureDbMySqlFlexibleServerConfig

On the networking screen:

You're done, click on Next: Security

AzureDbMySqlFlexibleServerConfig-Networking

 

That's it for the settings.
Click Review + Create, then if everything looks ok, click Create!

You will see the deployment screen. The deployment might take a few minutes.

AzureDbMySqlFlexibleServerDeployment

Once deployment is complete, go to the new resource and prepare to disable secure connections (SSL).

Note: We're disabling secure connection to the database just to simplify our setup. If you want to have a secure connection, you can read: Connect to Azure Database for MySQL - flexible server with Encrypted Connections.

On the resource page for your new Azure Database for MySql flexible server, in the left side menu, click on Server parameters, then put require_secure_transport in the search box, then once that parameter has been found, set it's value to OFF, to finish, click Save.

 

AzureDbMySqlFlexibleServer-ServerParams

Finally we need to get a connection string that we can use with Entity Framework. Click on Overview in the left hand menu, click on Connect, then expand the Connect from your app list, and copy the ADO.NET connection string..

 

Once the ADO.NET connection string has been copied, you will need to modify it for use when publishing your ASP.NET MVC web app to Azure. Whether you are putting it into an Appsettings.json file (to publish from VS for Mac), or into the Publish settings dialog (to publish from VS for Windows), you need to:

For example:

Now your database server is ready for use!

##Adding a Database

You can add a database to the server in a number of ways:

AzureMySqlAddDatabase

 

Connecting to the Database

From a Web App

Example connection string for the Entity Framwork Pomelo MySql database provider:

Note: When using the Publish feture in Visual Studio, this connection string is causing errors when I add it to Addional Settings, Database and try to use it to apply migrations when publishing.

 

Updating a Database with EF CLI Tools

You can use the dotnet ef update command to update a remote database like the one you created on Azure.

Specify the Environment in the Command

By specifying an environment, Development or Production, you can control which appsettings.json file and by that, which connection string is used for the update command. Here is an example:

Note that there are two sets of dashes, -- -- , this is not a typo!

Specify a Connection String in the Command

Use this command to update the database and apply a migration:

Note that when using zsh in the terminal on a Mac, you must use single quotes around the connection string. With Bash on Windows you can use either double or single quotes.

With Workbench for MySQL

Create a connection using these parameters:

MySqlWorkbenchAzureSettings

 

References

Creative Commons License These ASP.NET Core MVC Lecture Notes written by Brian Bird in 2022 and revised in , are licensed under a Creative Commons Attribution 4.0 International License.