Home >Database >Mysql Tutorial >Why Does 'Access Denied' Occur for the MySQL Root User Even When MYSQL_ROOT_PASSWORD Is Set in Docker?

Why Does 'Access Denied' Occur for the MySQL Root User Even When MYSQL_ROOT_PASSWORD Is Set in Docker?

DDD
DDDOriginal
2024-12-20 08:20:11404browse

Why Does

MySQL_ROOT_PASSWORD Is Set, But Access Denied for User 'root'@'localhost' (using Password: YES)

In dockerized MySQL environments, setting the MYSQL_ROOT_PASSWORD environment variable is not sufficient to ensure access to the database. This is because the image will not initialize a new database if the db_data volume already contains an existing installation.

Troubleshooting:

  1. Check if a database is already initialized in the db_data volume: If you started the container with a pre-existing volume, the environment variables will have no effect and the database will not be initialized.
  2. Delete the db_data volume to force initialization:

    • Run docker-compose down -v to remove the existing volume.
    • Run docker-compose up -d to start the container with a fresh volume and initialize the database.
  3. Convert to a bind mount if desired:

    • After initializing the database, you can convert the volume to a bind mount by changing the volumes section in docker-compose.yml to:

      volumes:
         - /host/path/to/db_data:/var/lib/mysql
    • Delete the contents of the bind mount location before starting the container: rm -rf /host/path/to/db_data/*
  4. Check for other potential issues:

    • Ensure that the MYSQL_ROOT_PASSWORD variable is set properly.
    • Check the firewall settings to ensure that port 3306 is open.
    • Verify that the MySQL service is running within the container.

The above is the detailed content of Why Does 'Access Denied' Occur for the MySQL Root User Even When MYSQL_ROOT_PASSWORD Is Set in Docker?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn