Problem Description
I’m having trouble with my Docker Compose setup where my WordPress application suddenly can’t connect to the MySQL database container. Everything was working fine for several months, then all my sites went down at once with database connection errors.
Current Situation
I managed to fix two of my three sites by updating the database, but one site still won’t connect. The main container keeps restarting because it can’t reach the database, so I can’t even get shell access to debug.
Docker Compose Configuration
Here’s my current setup:
wordpress:
image: wordpress:latest
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: database
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: mypassword
WORDPRESS_DB_NAME: wordpress_db
WORDPRESS_TABLE_PREFIX: wp_
volumes:
- ./wp-content:/var/www/html/wp-content
depends_on:
- database
database:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wpuser
MYSQL_PASSWORD: mypassword
volumes:
- ./mysql-data:/var/lib/mysql
What I’ve Tried
I’ve been stuck on this for about a week now. The weird thing is that stopping and restarting the containers eventually worked, but only after letting it sit for a while. Not sure why the timing mattered.
Has anyone seen this kind of intermittent database connection issue before? Any suggestions would be helpful.
Looking at your setup, the intermittent nature and the fact that waiting helped suggests MySQL might be running into memory or disk I/O issues during startup. I’ve dealt with similar problems on production environments where MySQL containers would fail to start properly under load. Check your Docker host’s available memory and disk space - MySQL 8.0 can be quite resource hungry during initialization. Another thing to investigate is whether your mysql-data volume has proper permissions. Sometimes after system updates or Docker version changes, volume permissions can get corrupted. Try running docker logs database to see if MySQL is throwing any specific errors during startup. If you’re still having issues, consider switching to a lighter database image like MariaDB temporarily to rule out MySQL 8.0 specific problems. The fact that two sites recovered but one didn’t suggests there might be corruption in that specific database’s data files.
This sounds like a race condition where WordPress starts before MySQL is fully ready to accept connections. I encountered something similar last year with a client’s multi-site setup. The depends_on directive only waits for the container to start, not for MySQL to be ready for connections. Consider adding a healthcheck to your MySQL container and using condition: service_healthy in your WordPress depends_on section. Also, MySQL 8.0 has authentication changes that can cause issues with older WordPress versions. You might want to add MYSQL_ROOT_HOST: '%' and command: --default-authentication-plugin=mysql_native_password to your database service. The intermittent nature suggests MySQL needs more time to initialize, especially if your data volume is large. Adding --innodb-buffer-pool-size=128M to the MySQL command can help with startup performance on resource-constrained systems.
i think u might be right. i had same issues b4. adding a healthcheck for mysql could help. also, increasing restart delay might solve it since it needs time to establish a connection. hope that helps!