Get in touch with us!

Azure MySQL In App (preview) + WordPress

Microsoft recently announced a new feature in Azure Web App service called MySQL In App, as you can tell by the name this is an PaaS version of MySQL and it’s not ClearDB.
The service is now in public preview and I like to show you how easy it is to use.

First of we create a Web App in the Azure portal, give it a name and location an click create.
AzureMySQL (1)
When the web app is created we want to go into the settings blade “MySQL In App (preview)”, in here turn the feature On.
Finnish by clicking Save.
AzureMySQL (2)

The simplest way to show this in action is to deploy a WordPress site to the web app and we are going to do this by simply cloning an external git repository.
Go to deployment options and select External Repository.
AzureMySQL (3)

Enter the git repository for WordPress URL https://github.com/WordPress/WordPress and branch.
AzureMySQL (4)

While we wait for the repository to clone to our web app lets check if mysql has started.
AzureMySQL (5)

Azure Portal have an build-in process explorer for web applications and in here you will see the mysqld process as soon as the service is provisioned.
AzureMySQL (6)

If the process is running you will be able to go back to MySQL in App blade and click Manage.

AzureMySQL (7)

This will open phpmyadmin in a new tab. If you get prompted for password it is user:azure password:password
AzureMySQL (8)
I’ve seen people freak out about this. Why is the password password?! OMG OMG!!
Wait, check the URL, if you haven’t seen xxx.scm.azurewebsites.net before “bing it”.
The site is protected by Azure AD Pre-Authentication and the permissions are set from RBAC(IAM) in the Azure portal.

AzureMySQL (9)
If you take the same URL from another browser you will see what I mean, there is no need to have a more complex password in MySQL.

The WordPress repository is now successfully cloned.

AzureMySQL (10)

In order to use In app MySQL the right way we will never use any hostname, database, username or password manually to configure any application. Instead you want to get the connection string by code from the host. You want to do this because the hostname can change when scaling and password get renewed (in the future).
Information how can be found here https://blogs.msdn.microsoft.com/appserviceteam/2016/08/18/announcing-mysql-in-app-preview-for-web-apps/
But I will show you how to do it in WordPress. First we need an editor, so why not use the one in the portal. Open App Service Editor.
AzureMySQL (11)
Rename wp-config-sample.php to wp-config.php
AzureMySQL (12)

Add the code like it’s shown in the picture bellow.

/*Add at the begining of the file*/

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
 if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
 continue;
 }
 
 $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
 $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
 $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
 $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $connectstr_dbname);

/** MySQL database username */
define('DB_USER', $connectstr_dbusername);

/** MySQL database password */
define('DB_PASSWORD', $connectstr_dbpassword);

/** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
define('DB_HOST', $connectstr_dbhost);
AzureMySQL (13)

Now test it by going to the Web App URL
AzureMySQL (14)

How simple is that!? I love it!
AzureMySQL (15)

Since it’s in preview it also free so why not make the web free as well.
AzureMySQL (16)

It works 🙂
AzureMySQL (17)

 

If you want this feature please try it out and spread the word. It’s not for production, yet and it’s single instance but hopefully it will become as good as Azure SQL.

Jon Jander @MeapaX

Submit a Comment

Your email address will not be published. Required fields are marked *