Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000] [2002] Connection timed out

Tags:

php

mysql

pdo

Getting this in my error logs every now and then and want to figure out the issue :

[04-Oct-2014 13:25:42 America/Chicago] Failed to connect to database: SQLSTATE[HY000] [2002] Connection timed out [04-Oct-2014 13:25:42 America/Chicago] PHP Fatal error: Call to a member function prepare() on a non-object in /home/spconlin/public_html/spc_app/app/add_log.php on line 28

Connect script :

<?php

if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');

// db config
$db_host        = '127.0.0.1';
$db_database    = '*****';
$db_user        = '*****';
$db_pass        = '*****'; 

//db connection
try {
    $db = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_user, $db_pass, 
        array(
            PDO::ATTR_EMULATE_PREPARES => false, 
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //PDO::ERRMODE_SILENT is default setting
            PDO::ATTR_PERSISTENT => false
            )
    );
}
catch(PDOException $e) {
    error_log("Failed to connect to database: ".$e->getMessage());
}

?>

What I have done :

  • switch from 'localhost' to 127.0.0.1 in the connection
  • switch persistent connections to false

Before doing the above changes to my connection I was getting SQLSTATE[HY000] [2002] Resource temporarily unavailable rather than connection timed out as shown above.

Some background information - this add_log.php script in particular is adding information to the database up to a few times a second when it is actually being used. While almost 99% of the time this particular script is referenced in the error sometimes other ones are. I realize that the script pointed to really does not mean anything because this is not where the error is occurring (it occurs in the connection not the add_log.php script), but it is odd it is almost always this particular file referenced which leads me to believe the fact it is called so often in succession is part of the problem.

DB is innodb.

Is this actually a problem with my server and db or the person/computer connecting to it? Is it possible 'they' have a bad connection to 'me' and this is what causes the error?

EDIT :

Could it be in the manner which I am requiring my connect script on pages it is needed? Since the file is 'hit' quite often it would also be requiring my connect script just as much by the line below. Note that I am using $_SERVER['DOCUMENT_ROOT'] (could that have an affect?).

require $_SERVER['DOCUMENT_ROOT'].'/custom/functions/connect.php';

like image 501
user756659 Avatar asked Oct 05 '14 01:10

user756659


1 Answers

Things you should improve in your code is to check for connection before creating a new one.

You should manage to reuse connection as much as possible instead of recreating unnecessary ones.

When too many connection are open you you will experience errors like this, and it will not be so obvious to reproduce.

Also when connection is not successfull you should exit before preparing.

This changes might require you to implement that logic inside of a class.

like image 103
meda Avatar answered Sep 22 '22 06:09

meda