title | description | author | ms.author | ms.reviewer | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | ms.devlang | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|
CLI script - Create an Azure Database for MySQL - Flexible Server database in a VNet |
This Azure CLI sample script shows how to create a Azure Database for MySQL - Flexible Server database in a VNet (private access connectivity method) and connect to the server from a VM within the VNet. |
shreyaaithal |
shaithal |
maghan |
06/18/2024 |
mysql |
flexible-server |
sample |
|
azurecli |
[!INCLUDEapplies-to-mysql-flexible-server]
This sample CLI script creates an Azure Database for MySQL - Flexible Server in a VNet (private access connectivity method) and connects to the server from a VM within the VNet.
Note
The connectivity method cannot be changed after creating the server. For example, if you create server using Private access (VNet Integration), you cannot change to Public access (allowed IP addresses) after creation. To learn more about connectivity methods, see Networking concepts.
[!INCLUDE quickstarts-free-trial-note]
[!INCLUDE azure-cli-prepare-your-environment.md]
[!INCLUDE cli-launch-cloud-shell-sign-in.md]
:::code language="azurecli" source="~/azure_cli_scripts/mysql/flexible-server/create-server-private-access/create-connect-server-in-vnet.sh" id="FullScript":::
Use the following steps to test connectivity to the MySQL server from the VM by connecting using SSH, downloading MySQL tools, and then using them to connect to the MySQL server.
-
To SSH into the VM, start by getting the public IP address and then use MySQL tools to connect
PUBLIC_IP=$(az vm list-ip-addresses --resource-group $RESOURCE_GROUP --name $VM --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv) ssh azureuser@$PUBLIC_IP
-
Download MySQL tools and connect to the server. Substitute <server_name> and <admin_user> with your values.
sudo apt-get update sudo apt-get install mysql-client wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem mysql -h <replace_with_server_name>.mysql.database.azure.com -u mysqladmin -p --ssl-mode=REQUIRED --ssl-ca=DigiCertGlobalRootCA.crt.pem
[!INCLUDE cli-clean-up-resources.md]
az group delete --name $RESOURCE_GROUP
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 network vnet subnet create | Creates a subnet within the VNet. |
az vm create | Creates an Azure Virtual Machine. |
az vm open-port | Opens a VM to inbound traffic on specified ports. |
az group delete | Deletes a resource group including all nested resources. |
- Try additional scripts: Azure CLI samples for Azure Database for MySQL - Flexible Server
- For more information on the Azure CLI, see Azure CLI documentation.