Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou recebendo esse erro do servidor ao tentar conectar via PDO com o mysql no link abaixo
http://funerariasaopedro.net.br/crud/
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
in /var/www/html/funerariasaopedro.net.br/web/crud/_conexao/Conexao.php:33
Stack trace: #0 /var/www/html/funerariasaopedro.net.br/web/crud/_conexao/Conexao.php(33):
PDO->__construct('mysql:?????????...', 'carcleo', 'teste')
#1 /var/www/html/funerariasaopedro.net.br/web/crud/cadastraPlanos.php(9): CONEXAO\Conexao->__construct()
#2 /var/www/html/funerariasaopedro.net.br/web/crud/index.php(27): require_once('/var/www/html/f...')
#3 {main} thrown in /var/www/html/funerariasaopedro.net.br/web/crud/_conexao/Conexao.php on line 33
Classe de conexão:
<?php
namespace CONEXAO;
use PDO;
class Conexao {
private static $conexao;
private static $hostname = "server";
private static $port = 3306;
private static $dbname = "db";
private static $username = "user";
private static $password = "senha";
public function __construct() {
if (isset(self::$conexao)) {
self::$conexao = self::$conexao;
} else {
try {
self::$conexao = new PDO('mysql:
host='.self::$hostname.';
port= '.self::$port.';
dbname='.self::$dbname,
self::$username,
self::$password
);
} catch (Exception $e) {
self::$conexao = NULL;
echo $e->getMessage();
}
}
}
public function abreConexao() {
return self::$conexao;
}
public function fechaConexao () {
if (self::$conexao != null) {
self::$conexao = null;
}
}
}
O que pode ser isso?
Obs.: O mesmo código localmente funciona e os dados de conexão estão corretos no servidor.
As senhas do servidor já foram testadas ok.
A classe abaixo, com os mesmos dados, funciona:
<?php
class Conexao {
private $host = "server";
private $user = "user";
private $password = "senha";
private $db = "db";
private $conexao;
public function abreConexao() {
if (isset($this->conexao)) {
return $this->conexao;
} else {
$this->conexao = new mysqli($this->host, $this->user, $this->password, $this->db);
$this->conexao->set_charset("utf8");
return $this->conexao;
}
}
public function fechaConexao () {
if ($this->conexao != null) {
$this->conexao = null;
}
}
}
?>Carregando comentários...