Failed connection to database: SQLSTATE[HY000] [2002] No such file or directory

Asked

Viewed 571 times

0

I have this error in Mysql when I try to make the connection. Everything works perfectly when run by localhost, but when running by hosting, it happens:

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

I changed the address, password, name of the bank... all right. What could be causing this?

Connection class:

class Connection extends Config {
    private $dbhost = 'localhost';
    private $dbname = 'dev_pcel';
    private $dbuser = 'eudegani';
    private $dbpass = '8303';
    private $dbopts = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    private $dbconn = null;

    function __construct() {
        if (empty($this->dbconn)) {
            return $this->dbconn = $this->dbConnect();
        }
    }

    private function dbConnect() {
        if (!empty($this->dbhost) && !empty($this->dbname) && !empty($this->dbuser) && !empty($this->dbpass)) {
            try {
                $this->dbconn = new PDO("mysql:dbhost=" . $this->dbhost . ";dbname=" . $this->dbname . "", $this->dbuser, $this->dbpass, $this->dbopts);
                return $this->dbconn;
            } catch (PDOException $error) {
                exit('<h1>Falha na conexão com o banco de dados.</h1><h2>Detalhes da falha:</h2><pre>' . $error->getMessage() . '</pre>');
            }
        } else {
            exit('<h1>Falha na conexão com o banco de dados.</h1><h2>Dados de conexão não informados.</h2>');
        }
    }
}
  • Try changing the dbhost from 'localhost' for 127.0.0.1.

  • On the local server everything works, being the address 127.0.0.1 or localhost. The problem appears when it is in the hosting.

  • So, see if the 127.0.0.1 hosting works.

  • Didn’t work...

  • What a pity. I based myself on this question in the gringo OS: https://stackoverflow.com/q/20723803/1377664 See if any help there.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.