SQLSTATE [HY000] [2002] Resource temporarily unavailable - mysql - innodb and pdo

Getting numerous results in error logs like the ones below. All tables in the database are innodb and, as far as any interaction with these tables, all pdo with prepared statements.

As I said, all errors are almost identical to those listed below, but occur on several different pages. Regardless of the page, the ALWAYS error line points to the point at which I run the new statement ... for example, $stmt = $db->prepare("........ The statements themselves work fine, without errors, so I'm a little puzzled by what causes this.

A few errors like this for different pages:

[25-Sep-2014 10:19:09 America / Chicago] Failed to connect to the database: SQLSTATE [HY000] [2002] Resource temporarily unavailable [25-Sep-2014 10:19:09 America / Chicago] PHP Fatal error : call a member function Prepare () for a non-object in / home / test / public _html / add_log.php on line 28

Example stmt error points to - in this case, the string $stmt = $db->prepare(" . It always points to a line starting with a new prepared statement.

 $stmt = $db->prepare(" SELECT accounts.account_id, computers.computer_id, computers.status, users.user_id FROM accounts LEFT JOIN computers ON computers.account_id = accounts.account_id AND computers.computer_uid = :computer_uid LEFT JOIN users ON users.computer_id = computers.computer_id AND users.username = :username WHERE accounts.account_key = :account_key "); //bindings $binding = array( 'account_key' => $_POST['account_key'], 'computer_uid' => $_POST['computer_uid'], 'username' => $_POST['username'] ); $stmt->execute($binding); //result (can only be one or none) $result = $stmt->fetch(PDO::FETCH_ASSOC); 

connect script:

 <?php if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly'); // db config $db_host = 'localhost'; $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::ATTR_PERSISTENT => true)); } catch(PDOException $e) { error_log("Failed to connect to database: ".$e->getMessage()); } ?> 

What crossed my mind:

  • This particular script works a lot ... sometimes it can be called several times in one second.

  • On this site I use the same format for all my prepared statements ... $ stmt = ... $ result or $ results = ... I do not close the cursor, however, it is not required from my research, since the driver is MySQL, and this is done automatically.

  • I set PDO :: ATTR_PERSISTENT to true in my connection settings - will this have anything to do with this error?

What happens to all these errors? Is anyone

EDIT - Additional Information:

  • I changed localhost to 127.0.0.1 explicitly
  • I have disabled persistent connections / false

Since the above, the error has changed to SQLSTATE[HY000] [2002] Connection timed out . Is this really a problem with the person / computer connecting to "me" or is it really a problem with my / db server?

EDIT 2:

Is it possible that my use of $_SERVER['DOCUMENT_ROOT'] might cause the problem? Since this file "hit" so often, it also requires a script connection as often on the line below. I need a script connection, as follows on each page:

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

+8
sql php mysql pdo
source share
5 answers

I am going to point out a @MrGomez topic on this topic, which is basically a short, but still descriptive, error. This is not a PDO related issue, but specific to MySQL.

Reasons for getting "Resource temporarily unavailable

  • Running Available MySQL Memory
  • Executing MySQL file descriptors for the current user
  • Running live or dead end.
  • Starting resources on a proxy server
  • Start from PID, available for plug

However, your magazines should have more things to let you know what is going on. How to determine if there is any lock for any file.

+7
source share

In fact, this does not point to this line.

Error in your log

[25-Sep-2014 10:19:09 America / Chicago] Failed to connect to database: SQLSTATE [HY000] [2002] Resource is temporarily unavailable

it looks like it is being written in the output:

 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::ATTR_PERSISTENT => true)); } catch(PDOException $e) { error_log("Failed to connect to database: ".$e->getMessage()); // ERROR HERE } 

Note: $db not specified in the catch statement.

Then your script continues execution until it hits the line $db->prepare . Since $db is null, it throws another error, this time with the file line.

Other than that - see revo's tip for why you might get a "Resource is temporarily unavailable" error.

+3
source share

Increase your max_connections in my.cnf :

 max_connections = 1000 

Check the file descriptors available for mysql:

 ulimit -n 

If the default value is 1024, change the value to 65535 or more

 ulimit -n 65535 

restart mysqld

+2
source share

Just think - can you keep the database connection in memory so you don't have to recreate new ones? Also, from the point of view of your included DIR, the magic constant is great (I fully realized it today). Hope this helps.

0
source share

This usually means that you need to specify TCP / IP:

 "mysql:host=127.0.0.1" or "mysql:host=localhost;port=3306" 

In your case:

 $db_host = 'localhost'; $db_port = '3306'; 

maybe something like this:

 try { $db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_database;charset=utf8", $db_user, $db_pass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true)); } catch(PDOException $e) { error_log("Failed to connect to database: ".$e->getMessage()); // ERROR HERE } 
0
source share

All Articles