Error: "Can not connect to local MySQL server through socket"

0

I just created a domain on locaweb and created a website and database.

When I access the site, it loads part of the page, but I get the following error:

  

SQLSTATE [HY000] [2002] Can not connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)   Fatal error: Call to a member function query () on a non-object in /home/storage/9/04/78/gabrieldevito/public_html/gentelella-master/production/Classes/Persistencia/ContaCrud.php on line 44

The connection class with the bank is:

class Connection {

    public $host = 'localhost';
    public $dbname = 'usodecontas';
    public $username = 'root';
    public $password = 'abc123';

    public function conectar() {
        try {

            $conn = new PDO("mysql:host={$this->host};dbname={$this->dbname}", $this->username, $this->password);

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $conn;

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

    }

}

People, the support of locaweb is terrible and I can not find some tutorial that helps me to make a remote connection on the server so I can apply some commands that exist in other topics. Has anyone ever had similar problems?

Thank you.

    
asked by anonymous 09.08.2017 / 00:06

2 answers

0

I had the same problem as you to connect to my bank, after some time I realized that there were some adjustments I needed to make, follow what was done and I hope it helps:

  • Change the bank password within Locaweb (and change it in your code so that it can link as well)

  • Connection code:

    $connection=mysql_connect ("usodecontas.mysql.dbaas.com.br", "root", "abc123");
    if (!$connection) {
      die('Sem Conexão : ' . mysql_error());
    }
    
  • Important: Even if you are working locally (inside the server), the host can not be localhost, it must be the name of your server, for example: usodecontas.mysql.dbaas.com.br, take a look at is your within the locaweb, in database.

    Here is the file I used to reference:

    link

        
    01.09.2017 / 13:46
    0

    Depending on the PHP version and the php.ini settings use php inside keys, it will not work, try concatenating to see if that is the error

    From:

    $conn = new PDO("mysql:host={$this->host};dbname={$this->dbname}", $this->username, $this->password);
    

    To

    $conn = new PDO("mysql:host=".$this->host.";dbname=".$this->dbname, $this->username, $this->password);
    
        
    26.10.2018 / 14:13