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.
[PDOException] SQLSTATE[HY000] [2006] MySQL server has gone away
[ErrorException] PDO::__construct(): MySQL server has gone away
Can anyone please explain what I did wrong?
Advertisement
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:
- The Database Engine is running
- You have created your database
- You have created an user and granted permissions to the database
- 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 :).
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.
The MySQL server has gone away (error 2006) has two main causes and solutions: 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.Navigate to
- 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:
- Start an octane server.
- Set the wait_timeout and interactive_timeout to a minimum for your MySQL server.
- Make a few HTTP requests that uses MySQL connection. Enough requests to warm up all the workers.
- Wait a couple of minutes more than the set timeouts.
- Make another HTTP request.
- 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 (//github.com/laravel/framework/blob/39459134a81ef3cc4b078b2f9de1495459bba50a/src/Illuminate/Queue/Worker.php#L377). Maybe implement something like that for the HTTP workers as well?