MySQL connection timeout closed problem solution

PS: Original article, if you need to reprint, please indicate the source, thank you!     

The address of this article: http://flyer0126.iteye.com/blog/2346265

 

1. Applications (such as PHP) execute batches of MYSQL statements for a long time. The most common is acquisition or conversion of old and new data.

 

SHOW VARIABLES LIKE '%timeout%’

 

Solution: Add or modify the following two variables
in the my.ini file:
wait_timeout= 2880000
interactive_timeout =  2880000


For specific instructions on the two variables, you can google or see the official manual.
If you can't modify my.cnf, you can set CLIENT_INTERACTIVE when connecting to the database, for example:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)


2. Execute a SQL, but the SQL statement is too large or in the statement Contains BLOB or longblob fields.
For example, the processing of image data

 

SHOW VARIABLES LIKE '%max_allowed_packet%’

 
Solution:
Add or modify the following variables
in the my.cnf file:
max_allowed_packet =  10M   (you can also set the size you need)
The function of the max_allowed_packet parameter is to control the maximum length of its communication buffer.

 

3. Check the link status of MySQL and make it re-link

 

<?php
// database operation class
class DB{

    // save the database connection
    private static $_instance = null;

    // Connect to the database
    public static function get_conn($config){
        if(isset(self::$_instance) && !empty(self::$_instance)){
            return self::$_instance;
        }

        $dbhost = $config['host'];
        $dbname = $config['dbname'];
        $dbuser = $config['user'];
        $dbpasswd = $config['password'];
        $ pconnect = $ config ['pconnect'];
        $charset = $config['charset'];

        $dsn = "mysql:host=$dbhost;dbname=$dbname;";
        try {
            $h_param = array(
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            );
            if ($charset != '') {
                $h_param[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset; //Set the default encoding
            }
            if ($ pconnect) {
                $h_param[PDO::ATTR_PERSISTENT] = true;
            }
            $conn = new PDO($dsn, $dbuser, $dbpasswd, $h_param);

        } catch (PDOException $e) {
            throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);
        }

        self::$_instance = $conn;
        return $conn;
    }

    // execute the query
    public static function query($dbconn, $sqlstr, $condparam){
        $sth = $dbconn->prepare($sqlstr);
        try{
            $sth->execute($condparam);
        } catch (PDOException $e) {
            echo $e->getMessage().PHP_EOL;
            self::reset_connect($e->getMessage()); // call to reset connection on error
        }
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    // reset the connection
    public static function reset_connect($err_msg){
        if(strpos($err_msg, 'MySQL server has gone away')!==false){
            self::$_instance = null;
        }
    }

}
?>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326446032&siteId=291194637