Use CLI to create a single Azure SQL database and configure a firewall rule
This Azure CLI script example creates an Azure SQL database and configure a server-level firewall rule. Once the script has been successfully run, the SQL Database can be accessed from all Azure services and the configured IP address.
If you don't have an Azure subscription, create a free account before you begin.
Open Azure Cloud Shell
Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:
|Select Try It in the upper-right corner of a code block.|
|Open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal.|
If you choose to install and use the CLI locally, this topic requires that you are running the Azure CLI version 2.0 or later. Run
az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.
#!/bin/bash # Set an admin login and password for your database export adminlogin=ServerAdmin export password=ChangeYourAdminPassword1 # The logical server name has to be unique in the system export servername=server-$RANDOM # The ip address range that you want to allow to access your DB export startip=0.0.0.0 export endip=0.0.0.0 # Create a resource group az group create \ --name myResourceGroup \ --location westeurope # Create a logical server in the resource group az sql server create \ --name $servername \ --resource-group myResourceGroup \ --location westeurope \ --admin-user $adminlogin \ --admin-password $password # Configure a firewall rule for the server az sql server firewall-rule create \ --resource-group myResourceGroup \ --server $servername \ -n AllowYourIp \ --start-ip-address $startip \ --end-ip-address $endip # Create a database in the server with zone redundancy as true az sql db create \ --resource-group myResourceGroup \ --server $servername \ --name mySampleDatabase \ --sample-name AdventureWorksLT \ --service-objective S0 \ --zone-redundant # Update database and set zone redundancy as false az sql db update \ --resource-group myResourceGroup \ --server $servername \ --name mySampleDatabase \ --zone-redundant false
Clean up deployment
After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.
az group delete --name myResourceGroup
This script uses the following commands. Each command in the table links to command specific documentation.
|az group create||Creates a resource group in which all resources are stored.|
|az sql server create||Creates a logical server that hosts the SQL Database.|
|az sql server firewall create||Creates a firewall rule to allow access to all SQL Databases on the server from the entered IP address range.|
|az sql db create||Creates the SQL Database in the logical server.|
|az group delete||Deletes a resource group including all nested resources.|
For more information on the Azure CLI, see Azure CLI documentation.
Additional SQL Database CLI script samples can be found in the Azure SQL Database documentation.