I'm setting up WordPress, MariaDB, and Nginx in Docker, with WordPress connecting to MariaDB and Nginx serving WordPress. MariaDB keeps returning `Access denied for user 'user'@'localhost' (using password: YES)`. I've tried several fixes and have lost track of what I changed. What is likely causing this, and what should I check to get the containers communicating correctly?
4 Answers
Remember that MariaDB initialization variables and setup scripts normally run only when the data volume is created for the first time. Changing the password or username in the Compose file later will not update an existing database. You can either alter the account manually in MariaDB or remove and recreate the volume if the data can be discarded.
Check that both containers are attached to the same Docker network and that WordPress uses the MariaDB service name rather than a changing container IP. Also compare `WORDPRESS_DB_HOST`, `WORDPRESS_DB_USER`, `WORDPRESS_DB_PASSWORD`, and `WORDPRESS_DB_NAME` with the values MariaDB was initialized with.
A clean layout is to keep WordPress and MariaDB on one internal application network, then connect Nginx through a separate proxy network. Nginx can reach WordPress by its service name, while MariaDB does not need to be exposed to the outside.
Inside a container, `localhost` means that same container, not the Docker host or another container. Since WordPress is connecting across the Docker network, configure its database host as the MariaDB service or container name, such as `mariadb`, rather than `localhost`. MariaDB also needs a grant for connections from the Docker network, such as `user`@`%`, instead of only `user`@`localhost`.

The wildcard host is generally fine when the database port is not exposed publicly. If the account was originally created only as `user`@`localhost`, update its grants from inside MariaDB or recreate the database setup as appropriate.