I just started an Azure hosted WordPress App Service and chose to do the MySQL in-app(preview) option for the database. For those that aren't aware, this allows me to run the MySQL server side-by-side with my Web application within the same environment.
However, I'm running into a problem with the way I am choosing to make MySQL queries.
I want to reuse code from a different PHP project where the MySQL calls are in the form of PDO statements, like so:
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 can't make these calls unless I have a username and password to do so.
The PHPMyAdmin side-panel looks like this in the MySQL in-app(preview):
And if I pull up the user accounts, this is what I see:
I'm lost when it comes to what user and pass I should use, and if I should even be using localdb
as my db (that's where all the wordpress tables are listed).
All in all, I'm just trying to pull information from the database using PDO statements and need to know how to go about it.
The connection string can be seen in
D:\home\data\mysql\MYSQLCONNSTR_localdb.txt
. You can locate this file through Kudu Debug Console which could be accessed viahttps://<yourwebsitename>.scm.azurewebsites.net/DebugConsole
.The file content looks something like:
Following is a sample code snippet using PDO to connect MySQL in-app.
Important update:
From https://social.msdn.microsoft.com/Forums/azure/en-US/4c582216-bc1b-48b0-b80b-87ae540c3d05/php-azure-mysql-inapp-changed-ports-randomly
So we should get the connection string from env variable in PHP like below:
go to Configuration then add connection
Name = defaultConnection Value = Database=your_database;Data Source=your_website.mysql.database.azure.com;User Id=your_username;Password=your_password
then for me suresh42326 answered worked
https://github.com/projectkudu/kudu/issues/2238#issuecomment-427358981