修改凭证后无法连接Laravel项目的Docker MariaDB数据库镜像
Hey there, let's walk through this issue step by step—sounds like you're hitting a common snag when updating database credentials in a Dockerized Laravel setup. Here's what to check and fix:
First up, double-check your .env.mysql and .env.docker files:
- Make sure your updated credentials (like
MYSQL_ROOT_PASSWORD,MYSQL_DATABASE,MYSQL_USER, andMYSQL_PASSWORD—I noticed your snippet cut off atMYSQ..., so confirm you didn't miss the full variable name) are correctly entered, no typos, extra spaces, or misplaced quotes. - The
$${VAR_NAME}syntax in your docker-compose.yml is meant to avoid shell variable conflicts, but ensure the variables actually exist in your.env.mysqlfile. If you're using a different env file path, double-check that too.
Docker often hangs onto old container state even after you update env vars. Let's wipe that clean:
- Stop and remove containers + associated volumes (skip
-vif you need to keep existing database data):docker-compose down -v - Rebuild and restart the entire stack to ensure new env vars are picked up:
docker-compose up --build
Make sure your Laravel .env.docker has matching database settings:
DB_CONNECTION=mysql DB_HOST=db # This matches the service name in docker-compose.yml—critical for container networking DB_PORT=3306 DB_DATABASE=your_updated_db_name # Match MYSQL_DATABASE from .env.mysql DB_USERNAME=your_updated_db_user # Match MYSQL_USER from .env.mysql DB_PASSWORD=your_updated_db_password # Match MYSQL_PASSWORD from .env.mysql
Also, Laravel might have cached old config values. Flush those:
docker-compose exec web php artisan config:clear docker-compose exec web php artisan cache:clear
Let's rule out issues with the MariaDB container itself:
- Check the database logs for startup errors (like password complexity issues or invalid database names):
docker-compose logs db - Test manual login to the database with your new credentials to confirm they work:
If you can log in successfully, the problem is on the Laravel/web container side, not the DB.docker-compose exec db mysql -u your_db_user -p
Make sure the web container can reach the DB container:
docker-compose exec web ping db
If this fails, there's a networking issue in your Docker setup—double-check that both services are in the same default network (Docker Compose creates one automatically unless you specify otherwise).
If you're still stuck, share the full, redacted versions of your .env files and container logs, and we can dig deeper!
内容的提问来源于stack exchange,提问作者Jaeger

