简体   繁体   中英

MySQL server has gone away error on server

I have the following code about storing info about arXiv.org pages in mysql

function paper_info($paper_id){
    $url = 'http://arxiv.org/abs/'.$paper_id;
    $options = array('http'=>array('method'=>"GET", 'header'=>"User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko\r\n"));
    $context = stream_context_create($options);
    $sites_html = file_get_contents($url, false, $context);
    $html = new DOMDocument();
    @$html->loadHTML($sites_html);
    $title = null;
    foreach($html->getElementsByTagName('meta') as $meta) {
        if($meta->getAttribute("name")=="citation_title"){ 
            $title = $meta->getAttribute('content');
        }
    }
    if(preg_match('~<blockquote class="abstract mathjax">(.*?)</blockquote>~si', $sites_html, $match)){
        $abstract = trim(preg_replace('#<span class="descriptor">(.*?)</span>#', '', $match[1]));
    }   

    return array($title, $abstract);
}
            $paper_id = "1509.05363v1";
            if(!isset($paper_info)) $paper_info = paper_info($paper_id);        
            mysql_query("INSERT INTO `data` (`ID`, `Action`, `Value`) VALUES (NULL, 'Submit', '0');");  
            mysql_query("INSERT INTO `data` (`ID`, `Action`, `Value`) VALUES (NULL, 'Paper ID', '".$paper_id."');");
            mysql_query("INSERT INTO `data` (`ID`, `Action`, `Value`) VALUES (NULL, 'Title', '".mysql_real_escape_string($paper_info[0])."');");
            mysql_query("INSERT INTO `data` (`ID`, `Action`, `Value`) VALUES (NULL, 'Abstract', '".mysql_real_escape_string($paper_info[1])."');"); 

The code works fine on localhost however, when I uploaded my site on online server this error happens

MySQL server has gone away

I tried increasing the max_allowed_packet as it was suggested in MySQL error 2006: mysql server has gone away by adding the line

mysql_query("SET GLOBAL max_allowed_packet = 1073741824;");

but the error still happens. Does anyone know why this happens?

After trying this

mysql_query("SET SESSION wait_timeout = 300;");
mysql_query("SET GLOBAL interactive_timeout=300;");
mysql_query("SET GLOBAL wait_timeout=300;");

the error doesn't happen now but the function paper_info return empty value on server but works fine on localhost. Why is this happening?

Have you tried setting wait_timeout AND interactive_timeout value?

Example:

SET GLOBAL wait_timeout=300; 
SET GLOBAL interactive_timeout=300;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM