Restart/stop/start an Azure Database for MySQL - Flexible Server using Azure CLI
This sample CLI script performs restart, start and stop operations 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.
Important
When you Stop the server it remains in that state for the next 7 days in a stretch. If you do not manually Start it during this time, the server will automatically be started at the end of 7 days. You can chose to Stop it again if you are not using the server.
During the time server is stopped, no management operations can be performed on the server. In order to change any configuration settings on the server, you will need to start the server.
Also, see stop/start limitations before performing stop/start operations.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Azure Cloud Shell Quickstart - Bash.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you are running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
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
Edit the highlighted lines in the script with your values for variables.
#!/bin/bash
# Create a server, perform restart / start / stop operations
# 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 a 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. Stop the running server
az mysql flexible-server stop \
--resource-group $RESOURCE_GROUP \
--name $SERVER_NAME
# 4. Start the stopped server
az mysql flexible-server start \
--resource-group $RESOURCE_GROUP \
--name $SERVER_NAME
# 5. Restart the server
az mysql flexible-server restart \
--resource-group $RESOURCE_GROUP \
--name $SERVER_NAME
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 stop | Stops a Flexible Server. |
| az mysql flexible-server start | Starts a Flexible Server. |
| az mysql flexible-server restart | Restarts 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
- Try additional scripts: Azure CLI samples for Azure Database for MySQL - Flexible Server
- For more information on the Azure CLI, see Azure CLI documentation.
