Create a MySQL Database on Azure With PowerShell

In the post, we will create a MySQL database on Microsoft Azure using the Azure Az PowerShell module.

Az Module

This post will use the Azure Az PowerShell module, a cross-platform module available on Linux, macOS, and Windows. The Az module runs on PowerShell 7.1.3.


The create the MySQL database, I will use the following script. I assume that you already have a Resource Group ready. The top part of the script holds all the variables, and the second part is the cmdlet.

Note: Make sure you set the password in the $password variable.

You can also change the username which is set to sqladmin in the code. the database will run on a general-purpose G5 with one CPU and 10GB.

$rgname = "deploycontainers"
$mysqlservername = "deploycontainersmysql1"
$mysqluser = "sqladmin"
$password = ConvertTo-SecureString -String "ENTER-PASSWORD" -AsPlainText -Force
$location = "westus2"

New-AzMySqlServer -Name $mysqlservername -ResourceGroupName $rgname -Location $location -AdministratorUser $mysqluser -AdministratorLoginPassword $password -Sku gp_Gen5_1 -StorageInMb 10240

To run the code, first login to Azure using the following command.

connect-azaccount -Device

Save the code as .PS1 and run it.