Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

florenxe's avatar

Trouble connecting to Mysql in docker within a Laravel 9 project

I just recently migrated my Lumen API to Laravel 9. I also decided to use docker instead of homestead. I've not been able to successfully connect to the mysql instance running in the container. I keep getting access denied even when I explicitly turned off mysql password (Set it to false). Could this be a specific Laravel 9 issue because even when I ssh into the mysql container and try to access mysql I get the same access denied error

my env vars

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=pass

my docker-compose

version: "3.7"
services:
    app:
        build: .
        container_name: cookbookshq
        depends_on:
            - db
        volumes:
            - ./:/var/www
        networks:
            - cookbooks
    db:
        image: mysql:latest
        container_name: db
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: false
            MYSQL_DATABASE: test_db
            MYSQL_PASSWORD: pass
        volumes:
            - cookbooks-db:/var/lib/mysql
        networks:
            - cookbooks
    nginx:
        image: nginx:alpine
        container_name: web-server
        restart: unless-stopped
        ports:
            - "8080:80"
        volumes:
            - ./:/var/www
            - ./docker-compose/nginx:/etc/nginx/conf.d/
        networks:
            - cookbooks

volumes:
    cookbooks-db:

networks:
    cookbooks:
        driver: bridge

Really cant tell where I went wrong, your help is much appreciated!

0 likes
7 replies
Sinnbeck's avatar

Be aware that you cannot change anything to do with password after the volume is built. You will need to delete the volume completely and then rebuild the container

derba's avatar

@Sinnbeck He can bash into the container and change the password with root privilge

Sinnbeck's avatar

@derba that is true. But if it's a new project, it's much easier to just delete the volume

1 like
florenxe's avatar

@Sinnbeck Thanks for responding. Yes I know this, i've done this several times when I toggle the MYSQL_ROOT_PASSWORD I rebuild the container again. It didn't help either. I get the same access denied error. More so, when i ssh'd into the mysql container and run mysql -u root or mysql -u root -p I get the access denied error, even inside the container I am unable to access the mysql instance inside it which by the way was successfully built.

Attaching to cookbookshq, db, web-server
db           | 2022-08-14 05:31:27+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
db           | 2022-08-14 05:31:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db           | 2022-08-14 05:31:29+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
db           | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
db           | 2022-08-14T05:31:30.549689Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
db           | 2022-08-14T05:31:30.553917Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.30) starting as process 1
db           | 2022-08-14T05:31:30.605644Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
web-server   | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web-server   |

I've used the following db connection details and in every case, I get the same access denied error

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=mysql
DB_PASSWORD=pass

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root     #the default user mysql created
DB_PASSWORD=pass

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=
SQLSTATE[HY000] [1045] Access denied for user 'mysql'@'172.22.0.4'
SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.22.0.4'
Sinnbeck's avatar

@florenxe rebuilding the container does not remove the volume. You need to either use

docker volume rm volume_name

Or delete the folder on your computer (the volume!)

1 like
sheracore's avatar

@Sinnbeck It was my problem, I had this problem for two days and I built and built again, and permanently fixed settings were applied each time. Thanks, SInnbeck

Please or to participate in this conversation.