"إذا أراد العامل أن يؤدي عمله بشكل جيد، فعليه أولاً أن يشحذ أدواته." - كونفوشيوس، "مختارات كونفوشيوس. لو لينجونج"
الصفحة الأمامية > برمجة > How to Fix \"General error: 2006 MySQL server has gone away\" When Inserting Data?

How to Fix \"General error: 2006 MySQL server has gone away\" When Inserting Data?

تم النشر بتاريخ 2024-12-18
تصفح:794

How to Fix \

How to Resolve "General error: 2006 MySQL server has gone away" While Inserting Records

Introduction:
Inserting data into a MySQL database can occasionally result in the error "General error: 2006 MySQL server has gone away." This error occurs when the connection to the server is lost, typically due to one of two variables in the MySQL configuration.

Solution:
The key to resolving this error is to adjust the wait_timeout and interactive_timeout variables. These variables control the maximum time that MySQL will wait before terminating an inactive connection.

Adjusting Timeout Variables:

  1. Connect to the database using your preferred method (e.g., PHP, SQL console).
  2. Execute the following query: SET session wait_timeout=28800
  3. Additionally, if you encounter the error "General error: 2013 Lost connection to MySQL server during query," you may also need to execute: SET session interactive_timeout=28800
  4. Execute the following query to verify the updated values: SHOW VARIABLES LIKE '%timeout%'

Example in PHP:

<?php

$db = new db();

$results = $db->query("SHOW VARIABLES LIKE '%timeout%'", TRUE);
echo "<pre>";
var_dump($results);
echo "</pre>";

$results = $db->query("SET session wait_timeout=28800", FALSE);
// UPDATE - this is also needed
$results = $db->query("SET session interactive_timeout=28800", FALSE);

$results = $db->query("SHOW VARIABLES LIKE '%timeout%'", TRUE);
echo "<pre>";
var_dump($results);
echo "</pre>";


class db {

    public $mysqli;

    public function __construct() {
        $this->mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
        if (mysqli_connect_errno()) {
            exit();
        }
    }

    public function __destruct() {
        $this->disconnect();
        unset($this->mysqli);
    }

    public function disconnect() {
        $this->mysqli->close();
    }

    function query($q, $resultset) {

        /* create a prepared statement */
        if (!($stmt = $this->mysqli->prepare($q))) {
            echo("Sql Error: " . $q . ' Sql error #: ' . $this->mysqli->errno . ' - ' . $this->mysqli->error);
            return false;
        }

        /* execute query */
        $stmt->execute();

        if ($stmt->errno) {
            echo("Sql Error: " . $q . ' Sql error #: ' . $stmt->errno . ' - ' . $stmt->error);
            return false;
        }
        if ($resultset) {
            $result = $stmt->get_result();
            for ($set = array(); $row = $result->fetch_assoc();) {
            $set[] = $row;
            }
            $stmt->close();
            return $set;
        }
    }
}

Additional Considerations:

  • The default value for wait_timeout is 28800 seconds (8 hours), which is excessive. Setting it to a lower value, such as 300 seconds, is recommended.
  • Increasing the timeout values should be done with caution as it can lead to performance issues if connections are not properly closed.
أحدث البرنامج التعليمي أكثر>

تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.

Copyright© 2022 湘ICP备2022001581号-3