Azure 虚拟机 - 无法访问 MySQL 数据库
Azure Virtual Machine - Can't access MySQL database
我确实在 Azure 门户中创建并设置了一个虚拟机作为我的 MySQL 服务器。此时我可以使用 PHPMyAdmin 正常远程访问我的数据库。但是,尝试使用 PHP 脚本或 Virtual Studio 代码连接它时,出现连接错误。
我的PHP脚本:
$mysqli = new mysqli('valicon.brazilsouth.cloudapp.azure.com:3306', 'myuser', 'mypassword', 'mydatabase');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo '<p>Connection OK '. $mysqli->host_info.'</p>';
echo '<p>Server '.$mysqli->server_info.'</p>';
$mysqli->close();
我遇到的错误:
Connect Error (2002) A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
我在 web.config 文件中的连接字符串 (Visual Studio):
<add name="MySQLConnection"
providerName="MySql.Data.MySqlClient"
connectionString="server=valicon.brazilsouth.cloudapp.azure.com; user id=myuser; Password=mypassword; database=mydatabase; SslMode=none" />
我遇到的错误:
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the
specified MySQL hosts.
我错过了什么?为什么我可以使用 PHPMyAdmin 远程访问和管理我的数据库,但无法将其连接到 PHP 或 ASP.NET 项目?
更新
我在我的虚拟机设置中添加了一个连接规则,该规则将端口 3306 打开到外部连接。现在我从 PHP 脚本中得到另一个错误:
No connection could be made because the target machine actively
refused it.
我确实找到了问题所在。所以,有兴趣的朋友,我就把答案留在这里吧。
我无法通过 PHP 或 ASP.NET 代码远程连接数据库,因为 MySQL 配置中的 'bind-address' 变量设置为 127.0.0.1 值.这意味着只有本地主机可以连接。
所以,评论这行
bind-address = 127.0.0.1
在/etc/mysql/mysql.conf.d/mysqld.cnf文件中(Linux Ubuntu 18.04),我可以打开到任何IP地址的连接。
由于 PHPMyAdmin 在本地运行,即使 'bind-address' 设置为 127.0.0.1,它也可以连接数据库。
我确实在 Azure 门户中创建并设置了一个虚拟机作为我的 MySQL 服务器。此时我可以使用 PHPMyAdmin 正常远程访问我的数据库。但是,尝试使用 PHP 脚本或 Virtual Studio 代码连接它时,出现连接错误。
我的PHP脚本:
$mysqli = new mysqli('valicon.brazilsouth.cloudapp.azure.com:3306', 'myuser', 'mypassword', 'mydatabase');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo '<p>Connection OK '. $mysqli->host_info.'</p>';
echo '<p>Server '.$mysqli->server_info.'</p>';
$mysqli->close();
我遇到的错误:
Connect Error (2002) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我在 web.config 文件中的连接字符串 (Visual Studio):
<add name="MySQLConnection"
providerName="MySql.Data.MySqlClient"
connectionString="server=valicon.brazilsouth.cloudapp.azure.com; user id=myuser; Password=mypassword; database=mydatabase; SslMode=none" />
我遇到的错误:
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts.
我错过了什么?为什么我可以使用 PHPMyAdmin 远程访问和管理我的数据库,但无法将其连接到 PHP 或 ASP.NET 项目?
更新
我在我的虚拟机设置中添加了一个连接规则,该规则将端口 3306 打开到外部连接。现在我从 PHP 脚本中得到另一个错误:
No connection could be made because the target machine actively refused it.
我确实找到了问题所在。所以,有兴趣的朋友,我就把答案留在这里吧。
我无法通过 PHP 或 ASP.NET 代码远程连接数据库,因为 MySQL 配置中的 'bind-address' 变量设置为 127.0.0.1 值.这意味着只有本地主机可以连接。
所以,评论这行
bind-address = 127.0.0.1
在/etc/mysql/mysql.conf.d/mysqld.cnf文件中(Linux Ubuntu 18.04),我可以打开到任何IP地址的连接。
由于 PHPMyAdmin 在本地运行,即使 'bind-address' 设置为 127.0.0.1,它也可以连接数据库。