You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改凭证后无法连接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:

1. Verify Environment Variable Setup First

First up, double-check your .env.mysql and .env.docker files:

  • Make sure your updated credentials (like MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD—I noticed your snippet cut off at MYSQ..., 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.mysql file. If you're using a different env file path, double-check that too.
2. Clear Stale Docker State & Rebuild

Docker often hangs onto old container state even after you update env vars. Let's wipe that clean:

  1. Stop and remove containers + associated volumes (skip -v if you need to keep existing database data):
    docker-compose down -v
    
  2. Rebuild and restart the entire stack to ensure new env vars are picked up:
    docker-compose up --build
    
3. Sync Laravel's DB Config with New Credentials

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
4. Debug the Database Container Directly

Let's rule out issues with the MariaDB container itself:

  1. Check the database logs for startup errors (like password complexity issues or invalid database names):
    docker-compose logs db
    
  2. Test manual login to the database with your new credentials to confirm they work:
    docker-compose exec db mysql -u your_db_user -p
    
    If you can log in successfully, the problem is on the Laravel/web container side, not the DB.
5. Confirm Container Networking

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:29:23