{Azure SQL Database}SKU変更

 

https://learn.microsoft.com/ja-jp/azure/mysql/flexible-server/how-to-manage-server-cli

https://learn.microsoft.com/ja-jp/azure/mysql/flexible-server/concepts-service-tiers-storage

 

-- 1. Azureログイン

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

 

az group list
az upgrade

 

-- 2. VPC、サブネット作成

 

cat <<-'EOF' > main.tf


terraform{
    required_providers{
        azurerm={
            source  = "hashicorp/azurerm"
            version = "=3.6.0"
        }
    }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}


resource "azurerm_resource_group" "rg9999999" {
  name = "rg9999999"
  location = "Japan East"

}


resource "azurerm_virtual_network" "vnet01" {
  name                = "vnet01"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name
}

resource "azurerm_subnet" "subnet01" {
  name                 = "subnet01"
  resource_group_name  = azurerm_resource_group.rg9999999.name
  virtual_network_name = azurerm_virtual_network.vnet01.name
  address_prefixes     = ["10.0.0.0/24"]
}

 

resource "azurerm_public_ip" "pip01" {
  name                = "pip01"
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name
  allocation_method   = "Dynamic"
}

resource "azurerm_network_security_group" "nsg01" {
  name                = "nsg01"
  location            = azurerm_resource_group.rg9999999.location
  resource_group_name = azurerm_resource_group.rg9999999.name

  security_rule {
    name                       = "SSH"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
  security_rule {
    name                       = "OCI"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "*"
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "10.1.0.0/24"
    destination_address_prefix = "*"
  }
}

 


EOF

terraform init
terraform fmt
terraform -version

terraform plan

terraform apply -auto-approve


# terraform destroy -auto-approve

 

-- 3. Azure Database for MySQLサーバーの作成

※割り当てるsubnetが他リソースで使用されているとエラー

 

az mysql flexible-server list-skus \
--location japaneast \
--output table


az mysql flexible-server create \
--admin-password 'password' \
--admin-user azureuser \
--backup-retention 1 \
--name server99999999 \
--resource-group rg9999999 \
--sku-name Standard_B1ms \
--storage-auto-grow Disabled \
--storage-size 32 \
--version 8.0.21 \
--vnet vnet01 \
--subnet subnet01 \
--yes

 

 

az mysql flexible-server list \
--resource-group rg9999999


az mysql flexible-server show \
--resource-group rg9999999 \
--name server99999999


サーバーの削除

az mysql flexible-server delete \
--resource-group rg9999999 \
--name server99999999 \
--yes


-- 4. データベースの作成

az mysql flexible-server db create \
--resource-group rg9999999 \
--server-name server99999999 \
--database-name db01

az mysql flexible-server db list \
--resource-group rg9999999 \
--server-name server99999999

az mysql flexible-server db show \
--resource-group rg9999999 \
--server-name server99999999 \
--database-name db01


データベースの削除

az mysql flexible-server db delete \
--resource-group rg9999999 \
--server-name server99999999 \
--database-name db01 \
--yes


-- 5. SKU変更

 

az mysql flexible-server list-skus \
--location japaneast \
--output table


az mysql flexible-server update \
--resource-group rg9999999 \
--name server99999999 \
--sku-name Standard_B2s 

az mysql flexible-server update \
--resource-group rg9999999 \
--name server99999999 \
--sku-name Standard_B1ms 

 

az mysql flexible-server show \
--resource-group rg9999999 \
--name server99999999

 

 


-- 6. クリーンアップ

自動生成されたプライベート DNS ゾーンの削除

az network private-dns link vnet list \
--resource-group rg9999999 \
--zone-name "server99999999.private.mysql.database.azure.com" 


az network private-dns zone list \
--resource-group rg9999999

az network private-dns link vnet delete \
--resource-group rg9999999 \
--zone-name "server99999999.private.mysql.database.azure.com" \
--name vnet01-link \
--yes

 


az network private-dns zone delete \
--resource-group rg9999999 \
--name "server99999999.private.mysql.database.azure.com" \
--yes

 

az group list

 

az group delete \
--name NetworkWatcherRG \
--yes