Azure MySQL in App for App services

When you need a database for your website, why purchase a separate database service?

Image for post
Image for post
MySQL in app in Azure app service

MySQL in app was announced in March, 2017. Azure provides MySQL within your web app (App service), every web app in Azure has MySQL in app as an option. For Windows hosting MySQL in app service is available.

Note that, this can be used for small projects or dev/test projects, when moving to production you might want to consider Azure database for MySQL, which is Enterprise-ready, fully managed community MySQL database.

Getting Started :

This option comes disabled by default in every app service you create. In order to have MySQL in app database, you need to enable it from your web app settings, as shown below :

Enabling MySQL in app for Azure app services

After enabling it, wait for couple of minutes to enable certain services for your web app, which includes some cool stuff such as phpmyadmin.

After enabling it, you’ll see the screen as below :

MySQL in App Settings

To secure the connection between your app and your MySQL Database, you have an environment variable named “MYSQLCONNSTR_localdb” which contains value as connection string for your in app MySQL database.

Now, access your phpmyadmin go to : https://<yoursite>.scm.azurewebsites.net/phpmyadmin. You have phpmyadmin now, which means you can start using MySQL database.

You can connect to in app MySQL as below :

<?php 
//below will give the whole connectionstring in a single string
$conn = getenv("MYSQLCONNSTR_localdb");

//Let's split it and decorate it in an array
$conarr2 = explode(";",$conn);
$conarr = array();
foreach($conarr2 as $key=>$value){
$k = substr($value,0,strpos($value,'='));
$conarr[$k] = substr($value,strpos($value,'=')+1);
}
// $conarr is an array of values of connection string
print_r($conarr);
?>

There you go, you have now connection details to MySQL in app available within your app service, now you can use mysqli_* functions or PDO to perform CRUD operations and much more.

Hope this article has walked you clear with the concepts of MySQL in app for Azure app services. In case you want to connect with me you can DM me in twitter at https://twitter.com/niravmadariya

Please don’t forget to clap for the article if you liked reading it. See you soon with more concepts in Azure :)

Next :

Microsoft Certified: Azure Solutions Architect Expert, Microsoft Student Partner since 2015, Cloud Developer, PHP and Flutter Lover, https://niravmadariya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store