遇到错误SQLSTATE [HY000] [2002] NAS Synology上的连接被拒绝 [英] Getting Error SQLSTATE[HY000] [2002] Connection refused on NAS Synology

查看:294
本文介绍了遇到错误SQLSTATE [HY000] [2002] 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.1127.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆