MySQL server has gone away CodeIgniter

Databases are key components of most modern websites so errors that affect yours tend to be particularly worrisome. The “MySQL server has gone away” error, for example, may make you believe that your database was lost. That means you might have to resort to your latest backup to get your site up and running.

Despite how intimidating it sounds, however, the”MySQL server has gone away” error is pretty easy to fix. In fact, with the right instructions, your website should be back up and running in a matter of minutes.

In this article, we’re going to show you what the “MySQL server has gone away” error looks like and break down what can cause it in WordPress. Then we’ll teach you how to fix it and prevent this error from showing up again in the future.

Let’s get to work!

An Introduction to the “MySQL Server Has Gone Away” Error

First, let’s take a quick look at what the “MySQL server has gone away” error looks like:

MySQL server has gone away CodeIgniter
Browser showing the “MySQL server has gone away” error

The error itself is pretty straightforward and it almost always appears in the same way. Depending on which browser you use and your server’s configuration, however, the specific wording might change a bit.

Kinsta spoiled me so bad that I demand that level of service from every provider now. We also try to be at that level with our SaaS tool support.

MySQL server has gone away CodeIgniter
Suganthan Mohanadasan from @Suganthanmn
View plans

As for the error itself, it has to do with your MySQL database, as you might imagine from the name. To be more specific, one of three things usually causes this error on most websites:

  1. There’s a broken table within your database. Your database became corrupted so you need to revert to a recent backup or repair it.
  2. Your PHP ‘timeout’ setting is too low. If a PHP script needs access to your database and it can’t fetch the information within the timeout window that’s been set, this can also trigger the aforementioned error.
  3. ‘Packets’ have either been dropped or are too large. The server deems this to be the case, it essentially closes the connection and throws up the error.

Fortunately, all of these issues can be easily addressed. Let’s talk about how you can start the troubleshooting process.

How to Fix the “MySQL Server Has Gone Away” Error in WordPress (3 Methods)

As we’ve seen, there are a few potential causes for this particular WordPress error.

For that reason, there are different possible solutions. In most cases, one of the fixes below should get rid of the error on your website. So if one doesn’t work, you can simply move on to the next.

1. Edit Your WordPress wp-db.php File

If your website’s PHP timeout setting is too low and your database is too large, fetching the data you need during that window can be a problem. As we mentioned before, this can trigger the “MySQL server has gone away” error.

To prevent that from happening, you’ll need to edit one of your WordPress core files, called wp-db.php. You can find this file within your WordPress root folder, by opening up the wp-includes directory:

MySQL server has gone away CodeIgniter
The wp-db.php file

For accessing these files, we recommend that you use an and connect via SFTP (understand the difference between FTP and SFTP). Once you’ve connected to your site, locate wp-db.php, and right-click on it to open the file using your default local text editor. Follow this quick guide to show all hidden files in Filezilla.

Then, search through the file for the following line:

$this->ready = true;

Add the following line right below that code:

$this->query("set session wait_timeout=300");

What this code does is set your PHP timeout value to 300 seconds, which should be a lot more than you need to prevent any errors from showing up.

Now save the changes to your wp-db.php file and make sure your website is loading as it should.

Deploy your application to Kinsta - Start with a $20 Credit now.

Run your Node.js, Python, Go, PHP, Ruby, Java, and Scala apps, (or almost anything else if you use your own custom Dockerfiles), in three, easy steps!

Deploy now and get $20 off

Please note that if you’re a Kinsta user, you shouldn’t need to alter your website’s PHP timeout settings. All of our plans have a base timeout value of 300 seconds out of the box and we can help you increase it depending on your needs.

2. Repair Your WordPress Database

Sometimes your WordPress database can become corrupted, which in turn can lead to errors when you’re trying to establish a connection with it. This isn’t all that common but it can happen in the course of a normal website’s growth as you add more tables to your database (as well as plugin and theme information).

To fix this issue, you can use a built-in WordPress function to . First, however, you’ll have to enable that feature. This involves navigating to your WordPress root directory and opening the wp-config.php file in order to edit it.

Once the file is open, scroll to the bottom and add the following line to it:

define('WP_ALLOW_REPAIR', true);

That simple line of code tells WordPress to enable the database repair function. Save the changes to wp-config.php, and close the file. To run the function, just visit the following URL:

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

WordPress will then ask if you want to simply repair your database or repair and optimize it. The first option is all you need to fix the “MySQL server has gone away” error:

MySQL server has gone away CodeIgniter
The repair database option in WordPress

The process shouldn’t take long, and when it’s done, the error in question should be gone. However, you still have some cleanup to do, as you’ll need to disable the database repair function on your website. If you don’t, anyone could trigger it by accessing the same URL.

Tired of subpar level 1 WordPress hosting support without the answers? Try our world-class support team! Check out our plans

Before you wrap up, therefore, return to your WordPress root directory and remove the line of code you added earlier. Then save your changes to the file and close it.

3. Restore Your Website Using a Backup Through Your Hosting Provider

If all else fails, you can always use a full backup of your website to restore it to a point when the database was working properly. Ideally, you’ll do this with a recent backup so that you lose as little data as possible.

The problem is that not all WordPress web hosts offer built-in backup functionality for their users. That means you’re often stuck using manual solutions such as plugins. These tools aren’t necessarily bad, but if you don’t have access to your WordPress admin area, restoring a backup becomes a tall order.

Info

Kinsta provides automatic daily and system generated backups for all plans. Optional hourly backups are also available.

Here at Kinsta, on the other hand, you get access to full backups of your website with every plan. To restore your site (including its database) to an earlier point, you just have to access your hosting dashboard and look for the Backups tab:

MySQL server has gone away CodeIgniter
Restore your backup in MyKinsta

Click on the backup you want to restore and you’ll see more details about when it was created. For each backup, there’s a Restore button you can use to return your website to its state at that time.

Do remember, though: using this functionality will overwrite the current version of your website. So you’ll only want to use it as a last-resort measure when you’re sure you won’t lose any critical information.

Info

All Kinsta plans features weekly automatic MySQL database optimization to ensure better database performance. Check out our plans for more info.

Experiencing the scary *MySQL Server Has Gone Away* error in WordPress? Don't sweat it, here are 3 ways to fix it immediately! 😰🔧 #mysql issuesClick to Tweet

Summary

The more your website grows, the more data it will need to store. All of that information goes into your WordPress database. In some cases, if it gets too big, you might run into errors such as “MySQL server has gone away”.

If you encounter this particular error, here are three ways you can get rid of it:

  1. Edit your WordPress wp-db.php file.
  2. Repair your WordPress database.
  3. Restore your website using a backup through your hosting provider.

In this other guide, we show you additional tips and steps on how to repair your WordPress database issues.

Now that you know how to fix, it’s time to actually get rid of this annoying error message. If you’re still experiencing issues with MySQL, you might want to check How to Fix the MySQL 1064 Error.


Get all your applications, databases and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 275+ PoPs worldwide

Test it yourself with $20 off your first month of Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.

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

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, then increase the timeouts for MySQL by increasing the wait_timeout and interactive_timeout parameters by using a custom parameter group.

What is the reason for 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.

How to restart MySQL server?

Restart the MySQL Server.
Open a terminal session on the STA server, and log in as the Oracle user..
Start the MySQL service: $ STA start mysql..
Verify the server is running: $ STA status mysql. You should see: mysql is running..

Where is database connection in CodeIgniter?

CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database. php. You can also set database connection values for specific environments by placing database.