The connection string can be seen in D:\home\data\mysql\MYSQLCONNSTR_localdb.txt . You can find this file through the Kudu Debug console, which can be accessed via https://<yourwebsitename>.scm.azurewebsites.net/DebugConsole .
The contents of the file look something like this:
Database=localdb;Data Source=127.0.0.1:54306;User Id=azure;Password=6
The following is an example code snippet using PDO to connect MySQL in an application.
$dsn = 'mysql:dbname=localdb;host=127.0.0.1:54306;charset=utf8'; $user = 'azure'; $password = '6#vWHD_$'; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } echo "Success: A proper connection to MySQL was made!";
Important update:
From https://social.msdn.microsoft.com/Forums/azure/en-US/4c582216-bc1b-48b0-b80b-87ae540c3d05/php-azure-mysql-inapp-changed-ports-randomly
A VM can host multiple WebApps; hence several processes in a MySQL application. When we start the MySql process, we try to use the same port as before. However, this may be done by another service or another in-app MySQL. As a result, the port may change. In addition, the web application can be moved from one virtual machine to another, and the set of available ports will vary.
To write a stable client application, make sure you read the connection information from the env variable. See more details.
So, we need to get the connection string from the env variable in PHP, as shown below:
$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); }