Azure: Where is the default user / password for a Wordpress database using MySQL in-app (preview)?

I just launched the App Store app for WordPress and decided to use the MySQL in-app (preview) option for the database. For those who don't know this, this allows me to run the MySQL server side by side with my web application in the same environment.

However, I am having a problem with the way I select MySQL queries.

I want to reuse code from another PHP project where MySQL calls are in the form of PDO statements, for example:

try { $db = new PDO('mysql:host=localhost;dbname=localdb;charset=utf8', 'user', 'pass'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch(PDOException $ex) { echo "did not connect..."; } $sth = $db->prepare("SELECT *FROM MyTable;"); $sth->execute(); 

I cannot make these calls if I do not have a username and password for this.

PHPMyAdmin sidebar looks like this in MySQL in-app (preview):

PHPMyAdmin sidebar side for MySQL preview in application

And if I raise user accounts, this is what I see:

PHPMyAdmin User Accounts

I get lost when it comes to which user and pass I should use, and even if I should use localdb as my db (which lists all wordpress tables).

In general, I'm just trying to extract information from a database using PDO instructions and need to know how to do this.

+5
source share
2 answers

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#vWHD_$ 

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); } 
+13
source
 /*Add at the begining of the file if you want to connect to custom database then fill this variable $connectstr_dbname = 'MY_CUSTOM_DB'; and comment or remove $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value); from below code... */ $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); 
-1
source

Source: https://habr.com/ru/post/1262661/


All Articles