How to reduce cost of azure database for mysql , created for wordpress

Muhammad Qasim 41 Reputation points
2022-01-22T18:38:04.78+00:00

I have created a resource for hosting my wordpress website on azure. From marketplace i searched for wordpress and on first option wordpress was present in that search and i selected that option and created resource for wordpress. Now i am creating my wordpress website on azure and haven't published it yet bc it is underdevelopment. Now when I want to edit and work on my wordpress website then i login on azure and start the resource for that wordpress website and start editing my wordpress website online. and when i decide to stop editing my website then i also stop that resource to minime my cost. I also selected free tier :F1 app service plan.
Now when i logged in to azure then from 150 usd credits were decreased to 108 after two days but i had stop the resource but still charged. When i checked the details of costs then founded 35$ used by "azure database for mysql" . Now i changed the pricing tier of database for mysql but now i want to ask that is there any way to stop this service when i am not using it.
Can i stop this service Azure Database for MySQL server when i am not using it?
actually this service is costing about 35$ just for few days and i want to stop it when i am not using it.

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
709 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hendrik Kruger 5 Reputation points
    2023-03-25T08:23:42.53+00:00

    You can automate the MySQL server stop / start by using below script and setting up schedules.

    Set up Automation Account (https://learn.microsoft.com/en-us/azure/automation/quickstarts/create-azure-automation-account-portal)

    Configure managed Identity (https://learn.microsoft.com/en-us/azure/automation/enable-managed-identity-for-automation)

    Add MySQL_Stop_Start Runbook (https://learn.microsoft.com/en-us/azure/automation/manage-runbooks)

    Set up Runbook schedules (https://learn.microsoft.com/en-us/azure/automation/manage-runbooks)

    • You'll have to specify the parameters for START and STOP schedules

    START:
    User's image

    STOP:
    User's image

    Paste below script in your powershelgl runbook

    Add your TenantID, SubscriptionID and ClientID details

    #Envrionment parameters
    param( 
    
    [parameter(Mandatory=$true)] 
    [string] $resourceGroupName, 
     
    [parameter(Mandatory=$true)] 
    [string] $MySqlInstanceName, 
    
    [parameter(Mandatory=$true)] 
    [string] $action
    ) 
     
    filter timestamp {"[$(Get-Date -Format G)]: $_"} 
     
    Write-Output "Azure Automation Script started." | timestamp 
     
    #$VerbosePreference = "Continue" ##enable this for verbose logging
    $ErrorActionPreference = "Stop" 
     
    # Ensures you do not inherit an AzContext in your runbook
    Disable-AzContextAutosave -Scope Process
    $tenantId = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
    $subscriptionID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    $clientId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    Write-Output "Detecting environment"
    if ($env:AUTOMATION_ASSET_ACCOUNTID) 
    {
        try 
        {
            Write-Output "Azure detected. Authenticating via Managed Identity"
            # Attempt Azure System Managed Identity Authentication
            $AzureContext = (Connect-AzAccount -Identity -Subscription $subscriptionID -ErrorAction Stop).context
            # Set and store context
            $AzureContext = Set-AzContext -Subscription $subscriptionID
            Write-Output "Successfully connected using Azure Managed Identity. Running in Azure Automation"
        }
        catch
        {
            Write-Host "Something went wrong. Azure environment detected but authenticating via Managed Identity failed.`
            Alerting owners and existing."
            #TODO: Send Azure Alert for failed authentication
        }
    }
    $startTime = Get-Date 
    Write-Output "Azure Automation local time: $startTime." | timestamp 
    
    # Get the authentication token 
    $azContext = Get-AzContext
    $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
    $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
    $token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
    $authHeader = @{
        'Content-Type'='application/json'
        'Authorization'='Bearer ' + $token.AccessToken
    }
    Write-Output "Authentication Token acquired." | timestamp 
    
    ##Invoke REST API Call based on specified action
    
    if($action -eq 'stop')
    {
    
            # Invoke the REST API
            $restUri='https://management.azure.com/subscriptions/'+$azContext.Subscription.Id+'/resourceGroups/'+$resourceGroupName+'/providers/Microsoft.DBForMySQL/servers/'+$MySqlInstanceName+'/'+$action+'?api-version=2020-01-01'
            $response = Invoke-RestMethod -Uri $restUri -Method POST -Headers $authHeader
            Write-Output "MySQL Single Database Instance $MySqlInstanceName in Resource Group $resourceGroupName is getting stopped." | timestamp 
    }
    else
    {
            # Invoke the REST API
            $restUri='https://management.azure.com/subscriptions/'+$azContext.Subscription.Id+'/resourceGroups/'+$resourceGroupName+'/providers/Microsoft.DBForMySQL/servers/'+$MySqlInstanceName+'/'+$action+'?api-version=2020-01-01'
            $response = Invoke-RestMethod -Uri $restUri -Method POST -Headers $authHeader
            Write-Output "MySQL Single Database Instance $MySqlInstanceName in Resource Group $resourceGroupName is getting started." | timestamp 
     }
    
    Write-Output "MySQL Single Database Instance $MySqlInstanceName in Resource Group $resourceGroupName $action operation completed successfully." | timestamp 
    Write-Output "Azure Automation Script finished." | timestamp
    
    1 person found this answer helpful.
    0 comments No comments

  2. Anurag Sharma 17,566 Reputation points
    2022-01-24T10:22:22.22+00:00

    Hi @Anonymous , welcome to Microsoft Q&A forum.

    To reduce the cost of Azure Database for MySQL Server, we can stop the server and then start it when we want to use it again. When the server is in the Stopped state, the server's compute is not billed. However, storage continues to to be billed as the server's storage remains to ensure that data files are available when the server is started again.

    So stopping the server would definitely reduce some cost based on stoppage duration. You can refer to below link for more details:

    Stop/Start an Azure Database for MySQL

    Below article will show how can we stop it using azure portal through a button click or through Azure CLI:

    How to stop/start the Azure Database for MySQL

    Azure Portal:
    167913-image.png

    Azure CLI commands:

    -- To stop  
    az mysql server stop --name <server-name> -g <resource-group-name>  
      
    ---- To start  
    az mysql server start --name <server-name> -g <resource-group-name>  
    

    I would also suggest you to once go through below link that talks about Flexible Server for Azure MySQL and ways to save more costs:

    Save cost with RIs and by stopping the Azure DB for MySQL Flexible Server instance for up to 30 days

    Please let us know if this helps or else we can discuss further. If answer helps, you can mark it 'Accept Answer'.

    0 comments No comments