General error 2006 mysql server has gone away laravel

mysql server has gone away error during installing migration (laravel)

So I am using my cmd on my laravel folder and I tried to do (php artisan migrate:install). 2 errors came up.

  1. [PDOException] SQLSTATE[HY000] [2006] MySQL server has gone away

  2. [ErrorException] PDO::__construct(): MySQL server has gone away

Can anyone please explain what I did wrong?

Answer

This is not a Laravel issue, but a general MySQL Issue. Maybe the server is not running. Are you sure you’re running MySQL in the background?

Check this link: MySQL Gone Away

Do the following checks in your system:

  1. The Database Engine is running
  2. You have created your database
  3. You have created an user and granted permissions to the database
  4. You have setup the user and the database in your Laravel’s .env file.

After this, try to run the migrations command again, which is:

As explained Here

Let us know if that helps :).

General error 2006 mysql server has gone away laravel

Tutorials

Having problem importing your SQL dump, be it via command line or via container management console, here is the solution.

$ ./mysql -u root -p my_db < file.sql
Enter password: 
ERROR 2006 (HY000) at line 1: MySQL server has gone away

Error Message: ERROR 2006 (HY000): MySQL server has gone away, see above.

Description: This is caused by SQL import file being too big or field carrying too much information, causing it to exceed the max_allowed_packet setting.

System: Linux

Solution: increase the maximum allowed packet by changing the parameter in my.cnf file will resolve this problem.

Steps:

1. SSH or SFTP to the Server

2. Look for my.cnf with whereis command or

3. my.cnf can be found on linux: /etc/mysql

4. Add the following line to my.cnf

5. Save and close the file

6. Restart MySQL server using or Restart the Server

sudo service mysql restart

7. Try importing the SSL file again.

  • Home
  • Help
  • Installation & Maintenance
  • Matomo On-Premise (self-hosted)
  • Troubleshoot Database

The MySQL server has gone away (error 2006) has two main causes and solutions:

  • Server timed out and closed the connection. To fix, check that wait_timeout mysql variable in your my.cnf configuration file is large enough, eg wait_timeout = 28800
  • You may also need to increase the innodb_log_file_size mysql variable in your my.cnf configuration to for example innodb_log_file_size = 128MB or higher.
  • Server dropped an incorrect or too large packet. If mysqld gets a packet that is too large or incorrect, it assumes that something has gone wrong with the client and closes the connection. 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
  • Database server ran out of space. This can occur when performing an update to the Matomo database that requires DB Schema changes (Especially for large databases).

Once you’ve made these changes, and restarted your MySQL or MariaDB server, the issue should be fixed and no more error triggered. If not, try increase the 128M values to 256M for example. In websites with a lot of traffic and data, you might even need to increase the value to 1024M.

If the above changes do not resolve the issue and if you are using MYSQLI adapter in Matomo, try switching to adapter = PDO\MYSQL in your config/config.ini.php file.

You may be interested to learn more about Matomo Analytics to measure your websites and apps and keep control of your analytics data.

    • Octane Version: 1.0.1
    • Laravel Version: 8.42.0
    • PHP Version: 8.0.6
    • Server & Version: Swoole 4.6.6
    • Database Driver & Version: MySQL 5.6.10

    Description:

    We are using AWS Aurora as our database and by default they have a wait_timeout and interactive_timeout of 300 seconds (5 minutes) so if the system does not receive any request for more than 5 minutes it starts throwing

    SQLSTATE[HY000]: General error: 2006 MySQL server has gone away (SQL: ...)
    

    The exception is being thrown while trying to authorize a user through the auth middleware.

    Steps To Reproduce:

    1. Start an octane server.
    2. Set the wait_timeout and interactive_timeout to a minimum for your MySQL server.
    3. Make a few HTTP requests that uses MySQL connection. Enough requests to warm up all the workers.
    4. Wait a couple of minutes more than the set timeouts.
    5. Make another HTTP request.
    6. An exception should be thrown.

    My input:

    From what I've seen the framework will try to reconnect if a connection is dropped. Here's the exception trace:

    {
    	"trace": [
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
    			"line": 752,
    			"function": "runQueryCallback",
    			"class": "Illuminate\\Database\\Connection",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
    			"line": 732,
    			"function": "tryAgainIfCausedByLostConnection",
    			"class": "Illuminate\\Database\\Connection",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
    			"line": 641,
    			"function": "handleQueryException",
    			"class": "Illuminate\\Database\\Connection",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
    			"line": 346,
    			"function": "run",
    			"class": "Illuminate\\Database\\Connection",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
    			"line": 2315,
    			"function": "select",
    			"class": "Illuminate\\Database\\Connection",
    			"type": "->"
    		},
    		...
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Auth/TokenGuard.php",
    			"line": 85,
    			"function": "retrieveByCredentials",
    			"class": "App\\Auth\\RepositoryUserProvider",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php",
    			"line": 204,
    			"function": "user",
    			"class": "Illuminate\\Auth\\TokenGuard",
    			"type": "->"
    		},
    		...
    		{
    			"file": "/app/vendor/laravel/octane/src/Worker.php",
    			"line": 92,
    			"function": "handle",
    			"class": "Laravel\\Octane\\ApplicationGateway",
    			"type": "->"
    		},
    		{
    			"file": "/app/vendor/laravel/octane/bin/swoole-server",
    			"line": 118,
    			"function": "handle",
    			"class": "Laravel\\Octane\\Worker",
    			"type": "->"
    		},
    		{
    			"function": "{closure}"
    		},
    		{
    			"file": "/app/vendor/laravel/octane/bin/swoole-server",
    			"line": 164,
    			"function": "start",
    			"class": "Swoole\\Server",
    			"type": "->"
    		}
    	]
    }

    And it looks like the tryAgainIfCausedByLostConnection() is being called but that is not working.

    After skimming through the code for Laravel's queue worker I see that if an exception is thrown due to lost connection, the worker process is exited and new worker is created (https://github.com/laravel/framework/blob/39459134a81ef3cc4b078b2f9de1495459bba50a/src/Illuminate/Queue/Worker.php#L377). Maybe implement something like that for the HTTP workers as well?

    How do I fix error code 2006 MySQL server has gone away?

    The MySQL server has gone away (error 2006) has two main causes and solutions: Server timed out and closed the connection. To fix, check that wait_timeout mysql variable in your my. cnf configuration file is large enough, eg wait_timeout = 28800.

    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.

    How do I fix MySQL connection error?

    Some permanent solutions are:.
    Determine what is wrong with your DNS server and fix it..
    Specify IP addresses rather than host names in the MySQL grant tables..
    Put an entry for the client machine name in /etc/hosts on Unix or \windows\hosts on Windows..
    Start mysqld with the skip_name_resolve system variable enabled..

    Can't connect to MySQL server timed out?

    A Connection Timed Out error occurs when the database's firewall won't allow you to connect to the database from your local machine or resource. If you are getting this error, check that you have added the machine or resource you are connecting from to the database's list of trusted sources.