Mysql server gone away php

  • Hello,

    MySQLi error: [2006] MySQL server has gone away

    Can you please help.

    This problem started about 2 months ago, every automatic and manual scan fails with the above error. I hoped the problem would go away but it persists.

    Below are the troubleshooting steps I’ve made and some configuration settings.

    BIG thanks for any help you can provide!
    Julian

    Troubleshoot test 1
    Deactivated all plugins except Wordfence
    Run PHP 7.4

    Troubleshoot test 2
    Deactivated all plugins except Wordfence
    Run PHP 7.3

    Wordfence scan failed each troubleshooting test run with error:
    MySQLi error: [2006] MySQL server has gone away

    Wordfence
    Using low resource scanning
    Requests 300 MB memory when scanning

    Hosting
    SiteGround, GrowBig, shared hosting. Resources seem to be OK.
    Cloudflare CDN

    Configuration
    WordPress Login page is protected behind a CAPTCHA.

    max_allowed_packet variable cannot be increased further.

    wait_timeout is capped at 60 seconds

    Error Logs
    php_errorlog file in the public_html:

    [28-Nov-2019 07:37:36 CST6CDT] PHP Fatal error: Unknown: Failed opening required '/home/oohlal9/public_html/wordfence-waf.php' (include_path='.:/usr/local/php71/pear') in Unknown on line 0
    [28-Nov-2019 07:37:37 CST6CDT] PHP Fatal error: Unknown: Failed opening required '/home/oohlal9/public_html/wordfence-waf.php' (include_path='.:/usr/local/php71/pear') in Unknown on line 0
    [28-Nov-2019 07:37:50 CST6CDT] PHP Fatal error: Unknown: Failed opening required '/home/oohlal9/public_html/wordfence-waf.php' (include_path='.:/usr/local/php71/pear') in Unknown on line 0
    [28-Nov-2019 07:45:49 CST6CDT] PHP Fatal error: Unknown: Failed opening required '/home/oohlal9/public_html/wordfence-waf.php' (include_path='.:/usr/local/php73/pear') in Unknown on line 0

  • Last updated: 2019-12-20

    I tried to query an Amazon Relational Database Service (Amazon RDS) DB instance that is running MySQL, and I received one of the following error messages: "MySQL server has gone away" or "Lost connection to server during query."

    Short Description

    If the server timed out and closed the connection, you can receive one of the following errors:

    • CR_SERVER_GONE_ERROR - The client couldn't send a question to the server.
    • CR_SERVER_LOST - The client didn't get an error when writing to the server, but the client didn't get a full answer (or any answer) to the question.

    For more information, see the MySQL documentation for MySQL server has gone away.

    Resolution

    See the following causes for and related resolutions for these errors:

    • If a connection is idle for too long, the connection might be terminated incorrectly from the client. To resolve this issue, verify that application timeouts are shorter than the MySQL timeouts, and be sure that your applications close idle connections.
    • If a connection times out, increase the timeouts for MySQL by increasing the wait_timeout and interactive_timeout parameters by using a custom parameter group. For more information, see Working with DB Parameter Groups.
    • If the query that is generating the error is retrieving a large data set, increase the max_allowed_packet size parameter by using a custom parameter group. For more information, see Modifying Parameters in a DB Parameter Group.
    • If the error occurs only when returning large data sets, the client might be using a large MTU value of 9001. To resolve this issue, reduce the client TCP/IP MTU value. For information on changing your client MTU value, see Configuring the MTU of an Instance.
    • If any init_connect parameters are set to a value that doesn't allow the parameter to be processed, associated client connections can fail. Be sure that all init_connect parameters are processed correctly. Be sure that users have EXECUTE permissions for any procedures referenced as an init_connect parameter.
    • If all connections are dropping at the same time, confirm that other connections are still working when this issue occurs. To resolve this issue, verify the MySQL DB instance is not crashing or failing over by viewing Amazon RDS events and reviewing your MySQL error logs.


    Did this article help?


    Do you need billing or technical support?

    AWS support for Internet Explorer ends on 07/31/2022. Supported browsers are Chrome, Firefox, Edge, and Safari. Learn more »

    Mysql server gone away php

    One reason why WordPress works so effectively is that it is powered by a MySQL database technology in the background. MySQL database works as a storage engine for your site settings, posts, pages, comments, and other content. Should something go wrong with that database, your server will throw up an error message. Are you currently seeing the following error? “MySQL Server Has Gone Away”.

    If you are, it means something went wrong with your database.

    To a beginner, and even someone with advanced knowledge, this MySQL error log can be indeed scary, and that’s quite understandable. At first, it might seem like your MySQL server has really gone away – i.e. it just disappeared. Or perhaps something went awfully wrong and the server died.

    In reality, none of the aforementioned assumptions are correct. When you see the error MySQL server has gone away, it indicates that:

    • Database connections have been closed
    • Server exceeding timeout setting
    • A file you are trying to upload to your database is too much for the server to handle

    Other factors can also be responsible for this error, as you will get to find out later.

    If you are currently experiencing this error, don’t fret. You are about to find out how to fix it quite easily.

    Content:

    • MySQL has gone away error: what causes it?
    • How to fix MySQL server error
      • Fix any corrupted database
      • Increase timeout settings
      • Do a full backup restoration
    • Conclusion 

    MySQL Server Has Gone Away Error: What Causes It?

    Server has gone error is one of those errors developers often encounter when working with MySQL.

    This error, usually, stems from MySQL performance settings. For instance, MySQL server has a connection window of 8 hours. That is to say, if the server is left idle for 8 hours, remote MySQL connections will be lost. And this lost connection leads to an error.

    But there are quite a number of other factors that can contribute to this error. They are as follows:

    • Your MySQL server wasn’t set to reconnect automatically. So when a timeout occurs on the server, client connections will be lost.
    • Something corrupts your database, thus prompting the need to fix it or recover the corrupted database from a backup.
    • The server ran into problems while executing a query fed to it.
    • This error can also be a result of the query being too large for the server to process. When this happens, the MySQL server closes the connection with the client-side and then pops up an error.
    • It’s equally possible that the problem could be coming from your hosting provider. For instance, if your database server goes down in your host, you may see this error.

    With all that said, let’s now see some of the ways to fix this error.

    How To Fix MySQL Server Error

    Thankfully, this error, sometimes seen as“general error: 2006 MySQL server has gone away”, can be fixed with some work. In this section, we will be showing you some of the methods to restore your database. As a matter of fact, some of the methods are so simple that you can have your website up and running within minutes.

    Method 1: Fix Any Corrupted Database

    Like earlier mentioned, a corrupted database can cause your server to throw up errors. Usually, the more you add content and plugins to your WordPress site, the more tables will be added to your database.

    With time, your database starts to expand, thus increasing the chances of a table getting corrupted.

    Thankfully, a corrupted database can be fixed, and the process is fairly simple. First, you will have to access your site via FTP. When connected, open the folder containing your WordPress files and locate the wp-config.php file (your main configuration file).

    Once you have access to this SQL file, open it and paste the following line of code to it.

    define(‘WP_ALLOW_REPAIR’, true);

    Ensure you save and upload the file after pasting the above code.

    What this code does is to instruct WordPress to initiate the database repair process. To complete the process, copy the following lines of code and paste into your browser.

    https://mywebsite.com/wp-admin/maint/repair.php

    Of course, you will have to replace mywebsite.com with your actual URL.

    Once you do this, you will see the screen below. Click Repair Database.

    Mysql server gone away php

    Upon click, WordPress will automatically fix broken database tables, and after that restart MySQL.

    The MySQL server error code should be gone now. But if it hasn’t, move on to the next step.

    Note: It’s important you disable the database repair function. If you don’t someone might manipulate your database just by typing the repair function URL.

    To do this, navigate back to your wp-config.php file, and delete the code you had earlier pasted there.

    Method 2: Increase Timeout Setting

    Like we had earlier mentioned, a low timeout window can cause MySQL to run into problems. As such, one of the ways to fix MySQL database connections problem is by increasing your site’s timeout value.

    To do this, you will have to make edits to your site’s wp-db.php file. As the name suggests, this is where your site’s MySQL settings are saved.

    This file sits in the wp-includes folder, and to access it you will first have to establish a FTP connection.

    Again, open the folder containing your WP file, and locate the ‘wp-includes’ folder. Once inside this folder, look for wp-db.php file. When you find it, right-click on it and select View/Edit

    Mysql server gone away php

    With the file opened, copy and paste the following lines of code

    $this->query(“set session wait_timeout=400”);

    For best results, ensure that you paste the code just below the line that reads

    $this->ready = true;

    Basically, what you are telling WordPress is to increase the timeout value to 400 seconds.

    Save the file and exit. With the timeout value increased, your site should be restored now. If this method still doesn’t work, proceed to the next method.

    Method 3: Do a Full Backup Restoration

    If up to this point nothing has worked, the best thing to do is a full db (and possibly site) restoration from a backup. Of course, for this to work, you should have a full backup of your website.

    Depending on your hosting provider, your site might have been backed up automatically. So check with your provider to know if they have a backup copy of your website.

    MySQL server has gone” is an error nobody ever wishes to encounter. However, this error might just happen to you, especially if your site is large or rapidly growing.

    In this article, we solved MySQL server error and showed you the steps we took. If you followed through, your site should be up and running by now.

    If not, you may decide to request professional help and have it fixed quickly.

    You may also check out our WP College to learn how to get the best out of your site. Among other things, you will learn how to stop MySQL errors from happening in the first place.

    And if you found this article useful, kindly do share.

    More Resources:

    • WordPress Database Plugin
    • How To Install WordPress On Localhost – Complete Guide
    • How To Fix Sidebar Below Content Error In WordPress

    Read More Useful Articles:

    How do I fix MySQL server has gone in PHP?

    To fix, you can increase the maximal packet size limit max_allowed_packet in my. cnf file, eg. set max_allowed_packet = 128M , then restart your MySQL server: sudo /etc/init. d/mysql restart.

    Why MySQL server has gone away?

    The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection. In this case, you normally get one of the following error codes (which one you get is operating system-dependent). The client couldn't send a question to the server.

    Can not connect to MySQL in PHP?

    PHP may not be able to connect to the MySQL server if the server name is not recognized. Make sure that the server name is set to localhost. In case of other errors, make sure to consult the error_log file to help when trying to solve any issues. The file is located in the same folder where the script is running.

    How do I fix MySQL server has gone away error WordPress?

    How to Fix the “MySQL Server Has Gone Away” Error in WordPress (3 Methods).
    Edit Your WordPress wp-db. php File. ... .
    Repair Your WordPress Database. ... .
    Restore Your Website Using a Backup Through Your Hosting Provider..