Enable and download server slow query logs of an Azure Database for MariaDB server using Azure CLI

This sample CLI script enables and downloads the slow query logs of a single Azure Database for MariaDB server.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

  • Use the Bash environment in Azure Cloud Shell.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For additional sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

  • 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

In this sample script, edit the highlighted lines to update the admin username and password to your own. Replace the <log_file_name> in the az monitor commands with your own server log file name.

#!/bin/bash

# Create a resource group
az group create \
--name myresourcegroup  \
--location westus

# Create a MariaDB server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure
# Substitute the <server_admin_password> with your own value
az mariadb server create \
--name mydemoserver \
--resource-group myresourcegroup \
--location westus \
--admin-user myadmin \
--admin-password <server_admin_password> \
--sku-name GP_Gen5_2 \

# List the configuration options for review
az mariadb server configuration list \
--resource-group myresourcegroup  \
--server mydemoserver

# Turn on statement level log
az mariadb server configuration set \
--name log_statement \
--resource-group myresourcegroup \
--server mydemoserver \
--value all

# Set log_min_duration_statement time to 10 sec
az mariadb server configuration set \
--name log_min_duration_statement \
--resource-group myresourcegroup \
--server mydemoserver \
--value 10000

# List the available log files and direct to a text file
az mariadb server-logs list \
--resource-group myresourcegroup \
--server mydemoserver > log_files_list.txt

# Download log file from Azure 
# Review log_files_list.txt to find the server log file name for the desired timeframe
# Substitute the <log_file_name> with your server log file name
# Creates the postgresql-<date>_000000.log file in the current command line path
az mariadb server-logs download \
--name <log_file_name> \
--resource-group myresourcegroup \
--server mydemoserver

Clean up deployment

Use the following command to remove the resource group and all resources associated with it after the script has been run.

#!/bin/bash
az group delete --name myresourcegroup

Script explanation

This script uses the commands outlined in the following table:

Command Notes
az group create Creates a resource group in which all resources are stored.
az mariadb server create Creates a MariaDB server that hosts the databases.
az mariadb server configuration list List the configuration values for a server.
az mariadb server configuration set Update the configuration of a server.
az mariadb server-logs list List log files for a server.
az mariadb server-logs download Download log files.
az group delete Deletes a resource group including all nested resources.

Next steps