Getting Error SQLSTATE[HY000] [2002] Connection refused on NAS Synology

ぐ巨炮叔叔 提交于 2019-12-02 01:19:52

问题


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.

I have trouble with the database and PHP code that add the email to the database.

If the server name is localhost, I get the following error:

SQLSTATE[HY000] [2002] No such file or directory

When it is 127.0.0.1 or 127.0.0.1:3306, I get the error below:

SQLSTATE[HY000] [2002] Connection refused

I didn't find the solution yet on Stackoverflow.

Here the PHP code:

<?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;

Why am I getting this error?


回答1:


I had the same problem:

SQLSTATE[HY000] [2002] Connection refused

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 );

What does work (for me):

$conn = new PDO("mysql:host=$servername:3307;dbname=$database", $login, $password);

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.




回答2:


I have the answer, the connection was refused because i installed mariaDB on my sinology witch change the default port for MySQL from 3306 to 3307.



来源:https://stackoverflow.com/questions/45254222/getting-error-sqlstatehy000-2002-connection-refused-on-nas-synology

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!