问题描述
我刚刚启动了Azure托管的WordPress应用服务,并选择对数据库执行MySQL应用内(预览)选项.对于那些不知道的人,这使我可以在同一环境中与我的Web应用程序并排运行MySQL服务器.
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.
但是,我在选择进行MySQL查询的方式时遇到了问题.
However, I'm running into a problem with the way I am choosing to make MySQL queries.
我想重用来自另一个PHP项目的代码,其中MySQL调用采用PDO语句的形式,
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.
PHPMyAdmin侧面板在MySQL应用内(预览)中如下所示:
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:
当我应该使用什么用户和密码,甚至我什至要使用localdb
作为我的数据库时(这是列出所有wordpress表的地方),我迷失了.
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).
总而言之,我只是想使用PDO语句从数据库中提取信息,并且需要知道如何处理.
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.
推荐答案
可以在D:\home\data\mysql\MYSQLCONNSTR_localdb.txt
中看到连接字符串.您可以通过Kudu调试控制台找到此文件,可以通过https://<yourwebsitename>.scm.azurewebsites.net/DebugConsole
进行访问.
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 via https://<yourwebsitename>.scm.azurewebsites.net/DebugConsole
.
文件内容类似于:
Database=localdb;Data Source=127.0.0.1:54306;User Id=azure;Password=6#vWHD_$
以下是使用PDO连接MySQL应用内代码的示例代码段.
Following is a sample code snippet using PDO to connect MySQL in-app.
$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
为了编写稳定的客户端应用程序,请确保您从env变量中读取了连接信息.有关更多详细信息,请参见此
In order to write the stable client app, do make sure you read the connection info from env variable. See this for more details.
因此,我们应该从PHP中的env变量获取连接字符串,如下所示:
So we should get the connection string from env variable in PHP like 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);
}
这篇关于Azure:使用MySQL应用程序内(预览)的Wordpress数据库的默认用户/密码在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!