遇到错误SQLSTATE [HY000] [2002] NAS Synology上的连接被拒绝 [英] Getting Error SQLSTATE[HY000] [2002] Connection refused on NAS Synology
问题描述
我正在制作一个正在构建的页面,该页面托管在Synology NAS上.
I am making an under construction page which is hosted on my Synology NAS.
访问者可以订阅他们的电子邮件,并在该网站何时可用时得到通知.
Visitors can subscribe with their email and get informed when the website will be available.
我在将电子邮件添加到数据库的数据库和PHP代码方面遇到麻烦.
I have trouble with the database and PHP code that add the email to the database.
如果服务器名称为localhost
,则会出现以下错误:
If the server name is localhost
, I get the following error:
SQLSTATE [HY000] [2002]没有这样的文件或目录
SQLSTATE[HY000] [2002] No such file or directory
当它是127.0.0.1
或127.0.0.1:3306
时,出现以下错误:
When it is 127.0.0.1
or 127.0.0.1:3306
, I get the error below:
SQLSTATE [HY000] [2002]连接被拒绝
SQLSTATE[HY000] [2002] Connection refused
我在Stackoverflow上找不到解决方案.
I didn't find the solution yet on Stackoverflow.
以下是PHP代码:
<?php
$servername = "localhost";
$username = "id";
$password = "password";
$dbname = "dbname";
try {
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO email ( email ) VALUES ( '$email' )";
$conn->exec( $sql );
echo "New record created successfully";
}
catch( PDOException $e )
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
为什么会出现此错误?
推荐答案
我遇到了同样的问题:
SQLSTATE[HY000] [2002] Connection refused
尝试从php连接到在Synology NAS上运行的MariaDB数据库时.仅提供用户名和密码
when attempting to connect to the MariaDB database running on my Synology NAS from php. Providing just username and password
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
不起作用,虽然指定了端口号,但在其他地方也找不到解决方案:
didn't work, while specifying the port number, a solution found elsewhere, didn't work either:
$conn = new PDO( "mysql:host=$servername;port=3307;dbname=$dbname", $username, $password );
什么对我有用?
$conn = new PDO("mysql:host=$servername:3307;dbname=$database", $login, $password);
从Synology NAS上的主菜单打开MariaDB 10属性窗口时,将找到端口名称.
显然,port=3307
规范没有任何作用,但是也没有给出错误消息.
The port name is found when opening the MariaDB 10 properties window from the main menu on the Synology NAS.
Apparently, the port=3307
specification does not have any effect, but does not give an error message either.
这篇关于遇到错误SQLSTATE [HY000] [2002] NAS Synology上的连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!