MySQL In-App Configuration for PHP Content Management Systems
Read more about MySQL In-App here.
Content Management Systems such as WordPress, Drupal, Joomla contain the database credentials within a configuration file. If you decide to use MySQL In-App for development purposes, you will quickly realize that you need to modify these credentials to work with MySQL In-App.
Below is a table containing the default configuration, followed by In-App credentials and configuration. To update your configuration with MySQL In-App, copy-paste both Part 1 and Part 2 for your respective CMS.
WordPress 4+ | Joomla 3+ | Drupal 8+ | |
Configuration Location | wwwroot/wp-config.php | wwwroot/configuration.php | wwwroot/sites/default/settings.php |
Default Configuration | /** The name of the database for WordPress */
define('DB_NAME', 'databaseName');/** MySQL database username */
define('DB_USER', 'databaseUserName');/** MySQL database password */
define('DB_PASSWORD', 'databasePassword');/** MySQL hostname */
define('DB_HOST', 'databaseHostName'); |
public $dbtype = 'mysql';public $host = 'databaseHostName';public $user = 'databaseUserName';public $password = 'databasePassword';public $db = 'databaseName';public $dbprefix = 'jos_'; | $databases['default']['default'] = array ('database' => 'databasename','username' => 'sqlusername','password' => 'sqlpassword','host' => 'localhost','port' => '3306','driver' => 'mysql','prefix' => '','collation' => 'utf8mb4_general_ci',); |
MySQL In-AppPart 1 - Connection String | $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);
} |
||
WordPress 4+ | Joomla 3+ | Drupal 8+ | |
MySQL In-AppPart 2 - Configuration | // ** 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); |
public $dbtype = 'mysql';
public $host = DB_HOST;
public $user = DB_USER;
public $password = DB_PASSWORD;
public $db = DB_NAME;
public $dbprefix = 'jos_'; |
$databases['default']['default'] = array ('database' => $connectstr_dbname,'username' => $connectstr_dbusername,'password' => $connectstr_dbpassword,'prefix' => '','host' => $connectstr_dbhost,'port' => $_SERVER['WEBSITE_MYSQL_PORT'],'driver' => 'mysql','namespace' => 'Drupal\\Core\\Database\\Driver\\mysql','collation' => 'utf8mb4_general_ci',); |
Comments
- Anonymous
June 08, 2017
I'm using MySQL In App, but don't find the environment variables described, DB_HOST for example. Every couple months MySQL picks a new port and my app breaks, I need to update the configuration.php file in my Joomla site, and see no way to determine it programmatically.Today MySQL chose a new port. Only way I see to get it is use PHPMyAdmin from the Azure portal to observe it, then update the configuration.php file.//public $host = '127.0.0.1:49679'; public $host = '127.0.0.1:49714';I don't see this anywhere in the environment variables. What am I missing? - Anonymous
June 08, 2017
The comment has been removed- Anonymous
February 14, 2018
On this page, https://blogs.msdn.microsoft.com/appserviceteam/2016/08/18/announcing-mysql-in-app-preview-for-web-apps/#mysqlconnect search for "WEBSITE_MYSQL_PORT"
- Anonymous