Configure slow query logs on an Azure Database for MySQL - Flexible Server using Azure CLI

This sample CLI script configures slow query logs on an Azure Database for MySQL - Flexible Server.

If you don't have an Azure subscription, create an Azure free account before you begin. With an Azure free account, you can now try Azure Database for MySQL - Flexible Server for free for 12 months. For more details, see Try Flexible Server for free.

Prerequisites

  • This article requires version 2.0 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.

Sample script

Edit the highlighted lines in the script with your values for variables.

#!/bin/bash

# Configure slow query logs on Azure Database for MySQL Flexible Server

# Set up variables
RESOURCE_GROUP="myresourcegroup"
SERVER_NAME="mydemoserver" # Substitute with preferred name for MySQL Flexible Server.
LOCATION="westus" 
ADMIN_USER="mysqladmin" 
PASSWORD="" # Enter your server admin password
IP_ADDRESS= # Enter your IP Address for Public Access - https://whatismyipaddress.com

# 1. Create resource group
az group create \
--name $RESOURCE_GROUP \
--location $LOCATION

# 2. Create a MySQL Flexible server in the resource group
az mysql flexible-server create \
--name $SERVER_NAME \
--resource-group $RESOURCE_GROUP \
--location $LOCATION \
--admin-user $ADMIN_USER \
--admin-password $PASSWORD \
--public-access $IP_ADDRESS

# 3. Enable slow query logs

az mysql flexible-server parameter set \
--name slow_query_log \
--resource-group $RESOURCE_GROUP \
--server-name $SERVER_NAME \
--value ON

# 4. Set long_query_time time to 15 sec
# This setting will log all queries executing for more than 15 sec. Please adjust this threshold based on your definition for slow queries

az mysql flexible-server parameter set \
--name long_query_time \
--resource-group $RESOURCE_GROUP \
--server $SERVER_NAME \
--value 15

# 5. Allow slow administrative statements (ex. ALTER_TABLE, ANALYZE_TABLE) to be logged.

az mysql flexible-server parameter set \
--resource-group $RESOURCE_GROUP \
--server-name $SERVER_NAME \
--name log_slow_admin_statements \
--value ON

Clean up deployment

After the sample script has been run, the following code snippet can be used to clean up the resources.

#!/bin/bash

RESOURCE_GROUP="myresourcegroup" 
SERVER_NAME="mydemoserver" # Enter your server name

# Delete MySQL Flexible Server
az mysql flexible-server delete \
--resource-group $RESOURCE_GROUP 
--name $SERVER_NAME

# Optional : Delete resource group
az group delete --name $RESOURCE_GROUP

Script explanation

This script uses the following commands. Each command in the table links to command specific documentation.

Command Notes
az group create Creates a resource group in which all resources are stored
az mysql flexible-server create Creates a Flexible Server that hosts the databases.
az mysql flexible-server parameter set Updates the parameter of a flexible server.
az mysql flexible-server delete Deletes a Flexible Server.
az group delete Deletes a resource group including all nested resources.

Next steps