Fix MySQL connection refused - add proper database readiness check

The previous 5-second fixed wait was insufficient for MySQL 8.0 to fully
initialize, causing migrations and seeders to fail with 'Connection refused'.

Changes:
- Replace 'sleep 5' with active connection polling
- Try to connect every second for up to 30 seconds
- Use PHP PDO to test actual database connectivity
- Only proceed when database accepts connections
- Show clear status when database is ready

This ensures migrations and seeders run only after database is fully ready,
preventing 'Connection refused' errors on fresh setup.

Fixes the issue where Laravel loads but shows MySQL connection error.
This commit is contained in:
2026-03-10 20:31:51 +02:00
parent ac11580eb3
commit b1453ff249

View File

@@ -147,15 +147,41 @@ echo.
REM Step 6: Wait for database
if "%DB%"=="mysql" (
echo Waiting for database...
timeout /t 5 /nobreak >nul
echo Database ready
echo Waiting for MySQL to be ready...
set DB_READY=0
for /L %%i in (1,1,30) do (
docker-compose exec -T app php -r "new PDO('mysql:host=mysql;dbname=laravel', 'laravel', 'secret');" >nul 2>&1
if !errorlevel! equ 0 (
set DB_READY=1
goto :mysql_ready
)
timeout /t 1 /nobreak >nul
)
:mysql_ready
if !DB_READY! equ 1 (
echo MySQL ready
) else (
echo WARNING: MySQL may not be fully ready yet
)
echo.
)
if "%DB%"=="pgsql" (
echo Waiting for database...
timeout /t 5 /nobreak >nul
echo Database ready
echo Waiting for PostgreSQL to be ready...
set DB_READY=0
for /L %%i in (1,1,30) do (
docker-compose exec -T app php -r "new PDO('pgsql:host=pgsql;dbname=laravel', 'laravel', 'secret');" >nul 2>&1
if !errorlevel! equ 0 (
set DB_READY=1
goto :pgsql_ready
)
timeout /t 1 /nobreak >nul
)
:pgsql_ready
if !DB_READY! equ 1 (
echo PostgreSQL ready
) else (
echo WARNING: PostgreSQL may not be fully ready yet
)
echo.
)