Jump to content

Recommended Posts

Posted

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

  }

?>
Posted

O que tens como servidor?

Se estás a usar localhost, experimenta  trocar por 127.0.0.1.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

  • 6 months later...
Posted

Para quem tiver problemas semelhantes, uma das coisas mais corretas a fazer é verificar se realmente o problema é na conexão, testando as portas abertas do servidor no site Meu IP e verificar se realmente as conexões externas estão sendo rejeitadas. No caso não era, mas fica aqui a dica 😉.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.