Table of Contents
In the post, we will create a MySQL database on Microsoft Azure using the Azure Az PowerShell module.
Connect to Microsoft Azure PowerShell
First, you need connect to Microsoft Azure PowerShell, there’re two ways to run PowerShell commands in Microsoft Azure:
Method 1: From Azure PowerShell modules installed in a computer.
Method 2: From Azure Cloud Shell using browsers.
Create a new MySQL Database in Azure using PowerShell
1. If this is your first time using the Azure Database for MySQL service, you must register the Microsoft.DBforMySQL resource provider.
Register-AzResourceProvider -ProviderNamespace Microsoft.DBforMySQL
2. We assume that you already have a Resource Group ready. If you don’t have once, you can Create a new resource group using PowerShell.
3. Declare variables, 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 of storage.
$ResourceGroupNname = "WestUS-RG"
$MySQLServername = "westus-mysql1"
$MySQLuser = "sqladmin"
$Password = ConvertTo-SecureString -String "Pass@ord123" -AsPlainText -Force
$Location = "WestUS"
New-AzMySqlServer -Name $MySQLServername `
-ResourceGroupName $ResourceGroupNname `
-Location $Location `
-AdministratorUser $MySQLuser `
-AdministratorLoginPassword $Password `
-Sku gp_Gen5_2 `
-StorageInMb 10240 -Verbose
The MySQL databases was created, you can connect to the database using app or command.
VERBOSE: Performing the operation "New-AzMySqlServer_Create" on target "Call remote 'ServersCreate' operation".
Name Location AdministratorLogin Version SkuName SkuTier SslEnforcement
---- -------- ------------------ ------- ------- ------- --------------
westus-mysql1 westus sqladmin 5.7 GP_Gen5_2 GeneralPurpose Enabled
Or you can check the database in the Azure portal.
Connect to the server using the mysql command-line tool
1. Connect to the server using the mysql command-line tool.
mysql -h eastus-mysql1.mysql.database.azure.com -u sqladmin@eastus-mysql1 -p --ssl-ca=BaltimoreCyberTrustRoot.crt.pem
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 64372
Server version: 5.6.47.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2. View server status.
MySQL [(none)]> status
--------------
/usr/bin/mysql Ver 15.1 Distrib 10.3.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Connection id: 64372
Current database:
Current user: [email protected]
SSL: Cipher in use is ECDHE-RSA-AES256-GCM-SHA384
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MySQL
Server version: 5.6.47.0 MySQL Community Server (GPL)
Protocol version: 10
Connection: eastus-mysql1.mysql.database.azure.com via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8mb4
Conn. characterset: utf8mb4
TCP port: 3306
Uptime: 18 min 26 sec
Threads: 15 Questions: 670 Slow queries: 0 Opens: 59 Flush tables: 2 Open tables: 28 Queries per second avg: 0.605
--------------