Docker部署Laravel+Nginx+MySQL时migrate报mysql: not found错误
Hey there, let's break down why you're hitting this error and how to fix it quickly.
Root Cause
Your Laravel app's PHP container (built from app.dockerfile) doesn't have MySQL client tools installed. When Laravel tries to load the stored database schema during migrations, it uses Symfony's Process component to run the system-level mysql command—and since that command isn't present in the container, it throws the "not found" error.
Step-by-Step Fix
1. Update Your app.dockerfile
Add a step to install the MySQL client package. Insert this after your PHP extension installation steps (or anywhere before the final command in the Dockerfile):
# Install MySQL client tools for Laravel schema operations RUN apt-get update && \ apt-get install -y --no-install-recommends mysql-client && \ rm -rf /var/lib/apt/lists/*
The --no-install-recommends flag keeps your image smaller, and cleaning up the apt list reduces unnecessary bloat.
2. Rebuild and Restart the App Container
Re-build the app image to include the new package, then restart the container to apply changes:
docker-compose build app docker-compose up -d app
3. Re-Run Migrations
Now execute the migrate command again—it should run without the error:
docker-compose exec app php artisan migrate
Verify the Fix
To confirm the MySQL client is installed correctly, check its version with this command:
docker-compose exec app mysql --version
You should see output like mysql Ver 8.0.36-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))—that means the client is ready for Laravel to use.
Extra Notes
- This issue specifically pops up when Laravel needs to interact with the database via system-level commands (like schema dumping/loading). Regular CRUD operations or basic migrations might work fine without the client, but schema-related tasks rely on it.
- Since your app is already accessible at
127.0.0.1:8990, this fix won't disrupt your existing setup—it just adds the missing tool Laravel needs for migrations.
内容的提问来源于stack exchange,提问作者thelara

