9

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?

davejal
  • 6,009
  • 10
  • 39
  • 82
Mackinley
  • 107
  • 2
  • 3
  • 7

9 Answers9

9

You have Lost SQL connection to server during query. It is temporally issue. This is because of very low default setting of max_allowed_packet.

Raising max_allowed_packet in my.cnf (under [mysqld]) to 8 or 16M usually fixes it.

[mysqld]
max_allowed_packet=16M

NOTE: This can be set on your server as it's running. You need to restart the MySQL service once you are done.

Use: set global max_allowed_packet=104857600. My value sets it to 100MB.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
7

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:

php artisan migrate

As explained Here

Let us know if that helps :).

Andrés Smerkin
  • 177
  • 1
  • 5
2

For me the problem seemed to be that I assigned the wrong port to my Laravel project's .env file. Later, when I matched it with the my.cnf file, it worked.

I'm using Ubuntu 16.04 + nginx + MariaDB + Laravel project.

Mill3r
  • 544
  • 1
  • 10
  • 31
2

I encountered the same problem. The solution was to delete the mysql port number from 3306 or 80, and leave it empty In files

.env DB_PORT=3306 to DB_PORT= and on

database.php 'port' => env('DB_PORT', '3306'), to 'port' => env('DB_PORT', ''),

Wassim Al Ahmad
  • 1,102
  • 2
  • 7
  • 23
1

In my case the problem was I changed DB_HOST to localhost but it was fixed by keeping it default ie 127.0.0.1 and the port to default 3306. Here is the configuration for localhost in xampp:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hmb
DB_USERNAME=root
DB_PASSWORD=

Although I use localhost:8081/phpmyadmin to access my db

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
1

In addition to other answers:

  • Try changing localhost domain to 127.0.0.1 both in .env and config/database.php.
  • Also If you're using git then check git status and see if any unwanted files have been changed.

Because In my case options array for mysql connection configuration in file config/database.php was blank array somehow. I checked out git checkout config/database.php file and it starting to work fine.

Saleh Mahmood
  • 1,823
  • 1
  • 22
  • 30
0

If someone is looking for a solution, try to clean all cache you have, you can manually delete cache files in bootstrap/cache folder.

0

My solution is due to fix the DB_HOST parameter. Indeed I use PHPStorm and I set the public url in .env in order to use DB tools from the IDE and this broken Laravel. So I changed DB_HOST to localhost and Laravel now works fine.

papesky
  • 637
  • 1
  • 8
  • 20
0

I ran into this issue on my XAMPP dev server while running a lot of Laravel unit tests. Configuration for XAMPP MySQL is located at C:\XAMPP\mysql\bin\my.ini. Open that file, look for the max_allowed_packet option (under the [mysqld] header). By default this is set to 1 megabyte (probably 1 mebibyte to be more accurate).

Simply change it to something large enough to prevent you from running into this issue again:

[mysqld]
max_allowed_packet=32M

Then restart the MySQL service from the XAMPP Control Panel to force the changes to take effect.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68