Error: "Can’t connect to local Mysql server through socket"

Asked

Viewed 1,567 times

1

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

When accessing the site, it loads part of the page, but I have the following error:

SQLSTATE[HY000] [2002] Can’t 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();
        }

    }

}

Guys, Ocaweb support is terrible and I’m not finding any tutorial to help me make a remote connection to the server so I can apply some commands that exist on other topics. Has anyone experienced similar problems?

Thank you.

  • Probably a lot of people have been through similar problems. It was just me leaving for Amazon that the problems are over (it’s not spam, I’m not affiliated with Amazon in any way, just a customer). The funniest thing: THREE MONTHS after I shut down the LW server, having already deleted everything I could before the removal, I received an email warning that the server was out of air kkkk. And about 8 months later, I received an invitation by email with a link to an event of theirs, and... the link didn’t work :) ! probably because my account was no longer found there. A beauty the integration of things.

2 answers

1

Depending on the PHP version and the settings of php.ini using php inside keys, it won’t work, try concatenating to see if this is the error

of:

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

0

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

  1. Change the password of the bank within Locaweb (and change your code also to make the necessary link)

  2. Connection code:

    $connection=mysql_connect ("usodecontas.mysql.dbaas.com.br", "root", "abc123");
    if (!$connection) {
      die('Sem Conexão : ' . mysql_error());
    }
    

Important: even if you work locally (inside the server), the host cannot be localhost, it has to be your server name, for example: usodecontas.mysql.dbaas.com.br, take a look at what yours is like inside the Web, in a database.

Follow the file I used for reference:

https://wiki.locaweb.com.br/pt-br/Como_conectar_a_um_banco_MySQL_atrav%C3%A9s_de_script_PHP

Browser other questions tagged

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