0

I try to connect to a distant host like this:

    class Singleton {
  public static $connection;

  public static function getConnection(){
      if(self::$connection==null){
         $dsn = 'mysql:dbname=gtrscrpt_ubot;';
         $user = 'MyUser';
         $password = 'MyPassword';
        try {
           self::$connection = new PDO('mysql:host=gator188.hostgator.com;port=2983;dbname=gtrscpt_ubot;',$user,$password );
           self::$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
      }
      return self::$connection;
  }

}


Singleton::getConnection();

I get this:

Connection failed: SQLSTATE[HY000] [2006] MySQL server has gone away

The problem is that I dont know if my code is wrong or cant I really connect to the server (I know however that I should connect)

UPDATE: Connection failed: SQLSTATE[HY000] [2000] mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file**

BlackFire27
  • 1,470
  • 5
  • 21
  • 32

1 Answers1

3

Looks like a problem with your host. Verify with them that your hostname, port and credentials are correct, and that your server can access external outgoing networking.

From the MySQL docs:

C.5.2.9. MySQL server has gone away

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.

Edit:

Your host is still using the old MySQL password scheme. If you have access to MySQL you can update your password as the error suggested:

SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges

Replace User and Host with your details. More info in this question.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112