简体   繁体   English

PHP / MySQL-未捕获的mysqli_sql_exception:MySQL服务器已消失

[英]PHP / MySQL - Uncaught mysqli_sql_exception: MySQL server has gone away

I have the following code, it worked fine on XAMPP Server. 我有以下代码,在XAMPP Server上工作正常。 Now i copied the PHP-Code and the MySQL-Database to a new Server with manually installed Apache, PHP and MySQL. 现在,我将PHP代码和MySQL数据库复制到具有手动安装的Apache,PHP和MySQL的新服务器上。

I get the following error: 我收到以下错误:

Warning: Uncaught mysqli_sql_exception: MySQL server has gone away in C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php:87 警告:未捕获的mysqli_sql_exception:MySQL服务器已在C:\\ Intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php:87中消失

Stack trace: 堆栈跟踪:

#0 C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php(87): mysqli_stmt->execute() #0 C:\\ Intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php(87):mysqli_stmt-> execute()

#1 [internal function]: session->read('3r7jkgjk93pu9fc...') #1 [内部功能]:session-> read('3r7jkgjk93pu9fc ...')

#2 C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php(43): session_regenerate_id(true) #2 C:\\ Intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php(43):session_regenerate_id(true)

#3 C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php(19): session->start_session('_sichereSitzung...', false) #3 C:\\ intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php(19):session-> start_session('_ sichereSitzung ...',false)

#4 C:\\intranet\\apache24\\htdocs\\content\\php\\session.php(16): session->__construct() #4 C:\\ Intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.php(16):会话-> __ construct()

#5 C:\\intranet\\apache24\\htdocs\\index.php(21): require_once('C:\\intranet\\apa...') #5 C:\\ intranet \\ apache24 \\ htdocs \\ index.php(21):require_once('C:\\ intranet \\ apa ...')

#6 {main} thrown in C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php on line 87 Catchable fatal error: session_regenerate_id(): Failed to create session ID: user (path: ) in C:\\intranet\\apache24\\htdocs\\content\\php\\session.class.php 在第87行上的C:\\ Intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php中抛出#6 {main}可捕获的致命错误:session_regenerate_id():无法在C中创建会话ID:用户(路径:): \\ intranet \\ apache24 \\ htdocs \\ content \\ php \\ session.class.php

on line 43r code here 在这里行43r代码

This is my Code: 这是我的代码:

class session {

    function __construct() {
        // set custom session functions
        session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc'));

       // this line prevents unexpected effects when using objects as save handler
       register_shutdown_function('session_write_close');

       // start the session
       $this->start_session('_sichereSitzung-A3i6Zz', FALSE);
    }

    function start_session($session_name, $secure) {
        // make sure the session cookie is not accessible via javascript
        $httponly = true;

        // get session cookie parameters 
        $cookieParams = session_get_cookie_params(); 

        // set the parameters
        session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);

        // prevent javascript from getting session id
        ini_set('session.use_only_cookies', 1);

        // change the session name 
        session_name($session_name);

        // now we can start the session
        session_start();

        // this line regenerates the session and delete the old one
        // it also generates a new encryption key in the database
        session_regenerate_id(true); 
    }

    function open() {
        // configuration file
        require_once('config.php');     

        // the sql data
        require_once(PHP_PATH . 'sqluserdata.php');

        //open mysqli-connection
        $mysqli = new mysqli(SESS_HOST, SESS_USER, SESS_PW, SESS_DB, SESS_PORT);

        //bind the mysqli-connection to variable
        $this->db = $mysqli;

        return true;
    }

    function close() {
        // close mysqli-connection
        $this->db->close();

        return true;
    }

    function read($id) {
        $min=1;
        $max=200;
        $rand = mt_rand($min,$max);

        // only run garbage collection random on page load
        if ($rand == 1) {
            // garbage collection
            $this->gc(259200);
        }

        // if statement doesnt exists create statement
        if (!isset($this->read_stmt)) {
            $this->read_stmt = $this->db->prepare("SELECT data FROM session WHERE id = ? LIMIT 1");
        }

        // bind session parameter, run statement, bind result to variable
        $this->read_stmt->bind_param('s', $id);
        $this->read_stmt->execute();
        $this->read_stmt->store_result();
        $this->read_stmt->bind_result($data);
        $this->read_stmt->fetch();

        return $data;
    }

    function write($id, $data) {
        // bind time to variable
        $time = time();

        // if statement doesnt exists, prepare statement
        if(!isset($this->w_stmt)) {
            $this->w_stmt = $this->db->prepare("REPLACE INTO session (id, access, data) VALUES (?, ?, ?)");
        }

        // bind variables, execute statement
        $this->w_stmt->bind_param('sis', $id, $time, $data);
        $this->w_stmt->execute();

        return true;
    }

    function destroy($id) {
        // if statement doesnt exists, prepare statement
        if(!isset($this->delete_stmt)) {
            $this->delete_stmt = $this->db->prepare("DELETE FROM session WHERE id = ?");
        }

        // bind parameters, execute statement
        $this->delete_stmt->bind_param('s', $id);
        $this->delete_stmt->execute();

        return true;
    }

    function gc($max) {
        // if statement doesnt exists, prepare statement
        if(!isset($this->gc_stmt)) {
            $this->gc_stmt = $this->db->prepare("DELETE FROM session WHERE access < ?");
        }

        // calculate time to delete
        $old = time() - $max;

        // bind parameters, execute query
        $this->gc_stmt->bind_param('s', $old);
        $this->gc_stmt->execute();

        return true;
    }

}

Anyone know this error? 有人知道这个错误吗? Thanks in advance. 提前致谢。

This error has nothing to do with your PHP code - it's an error that MySQL sends. 该错误与您的PHP代码无关-这是MySQL发送的错误。

Basically, the MySQL server has crashed : 基本上, MySQL服务器已崩溃

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection. MySQL服务器消失的最常见原因是服务器超时并关闭了连接。 In this case, you normally get one of the following error codes (which one you get is operating system-dependent). 在这种情况下,您通常会获得以下错误代码之一(所获得的错误代码取决于操作系统)。

Check out the possible problems in the page I've linked to. 在我链接到的页面中检查可能的问题。 The common problems are: 常见的问题是:

  • you're doing a very complex query that the server cannot handle; 您正在执行服务器无法处理的非常复杂的查询;
  • you have misconfigured your MySQL server. 您配置错误的MySQL服务器。

Does the error happen all the time when you run the queries? 运行查询时是否始终发生错误? Or is it some specific query what it crashes on? 还是某些特定的查询导致崩溃? You should check that first. 您应该先检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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