unedotamps's avatar

I Got Connection Timeout When Connection To DB

when i tried simple code to check connection it takes so long like 2 minutes then i got this error message and also i tried php aritisan migrate and got this error after 120s running Illuminate\Database\QueryException

  SQLSTATE[HY000] [2006] MySQL server has gone away (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'meme_origin' and table_type = 'BASE TABLE' order by table_name)
  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:822
    818▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    819▕                 );
    820▕             }
    821▕ 
  ➜ 822▕             throw new QueryException(
    823▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    824▕             );
    825▕         }
    826▕     }
      +39 vendor frames 

  40  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

all the env is valid , i tried connected to db using native php and it work really fine

0 likes
4 replies
StephanGadzhev's avatar

The error message is fired when the MySQL server unexpectedly close I assume its because of the long run time. This is definitely Myslq problem and not Laravel. Check first if the Mysql server is up. If yes set the "max_allowed_packet=16M" in "my.cnf". This usally fix the problem but if not try to check your "MySQL Server Logs" or share it here so we can have more information about the error or whats the problem. This error message is general.

unedotamps's avatar

@StephaneGadzhev i add max_allowed_packet=16M to my.cnf and restart using sudo service mysql restart and get the same error

laravel.log

[2023-12-08 13:24:27] local.ERROR: SQLSTATE[HY000] [2006] MySQL server has gone away (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'meme_origin' and table_type = 'BASE TABLE' order by table_name) {"exception":"[object] (Illuminate\Database\QueryException(code: 2006): SQLSTATE[HY000] [2006] MySQL server has gone away (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'meme_origin' and table_type = 'BASE TABLE' order by table_name) at /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php:822)

laravel.log stack trace

#0 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(976): Illuminate\Database\Connection->runQueryCallback()
#1 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(955): Illuminate\Database\Connection->tryAgainIfCausedByLostConnection()
#2 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(778): Illuminate\Database\Connection->handleQueryException()
#3 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(407): Illuminate\Database\Connection->run()
#4 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(394): Illuminate\Database\Connection->select()
#5 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php(41): Illuminate\Database\Connection->selectFromWriteConnection()
#6 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php(162): Illuminate\Database\Schema\MySqlBuilder->getTables()
#7 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php(184): Illuminate\Database\Schema\Builder->hasTable()
#8 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(700): Illuminate\Database\Migrations\DatabaseMigrationRepository->repositoryExists()

...
[previous exception] [object] (PDOException(code: 0): PDO::__construct(): Error while reading greeting packet. PID=90088 at /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:65)
[stacktrace]
#0 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(65): PDO->__construct()
#1 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(83): Illuminate\Database\Connectors\Connector->createPdoConnection()
#2 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(48): Illuminate\Database\Connectors\Connector->tryAgainIfCausedByLostConnection()
#3 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection()
#4 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(184): Illuminate\Database\Connectors\MySqlConnector->connect()
#5 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#6 /home/unedotamps/Code/PHP/testmigration/vendor/laravel/framework/src/Illuminate/Database/Connection.php(1319): call_user_func()

errorlog mysql

2023-12-08T13:19:34.226503Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-12-08T13:19:34.226556Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-12-08T13:19:34.258433Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '127.0.0.1' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-12-08T13:19:34.258465Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.35-0ubuntu0.22.04.1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
Snapey's avatar
Snapey
Best Answer
Level 122

@unedotamps You need to check your database credentials.

I usually go for tinker first and run config('database') and check your settings.

If what is presented is not the same as your env file then you need to clear the config cache. You can re-cache if this is a production environment.

unedotamps's avatar

@Snapey yes this is the problem my env DB_PORT = 5432 overide the laravel env DB_PORT

Please or to participate in this conversation.