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

lemmon's avatar
Level 28

Cant get artisan to run db migrations using docker permissions issue

I am following The Docker Tutorial by Andrew Schmelyun on laracasts. All was going well I could run migrations using artisan but only if I am using sqlite db. when I change to mysql db I get this error. Thank you ahead of time for any help.

I know sail works well but can you deploy sail to production?

/Users/henrylemmon/Sites/laravel-docker.test > docker-compose run --rm artisan migrate:fresh
[+] Creating 1/0
 ✔ Container laravel-dockertest-mysql-1  Running                           0.0s 

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'172.18.0.4' (using password: YES) (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'laravel' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:813
    809▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    810▕                 );
    811▕             }
    812▕ 
  ➜ 813▕             throw new QueryException(
    814▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    815▕             );
    816▕         }
    817▕     }

      +33 vendor frames 

  34  artisan:13
      Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))

Here are the files for docker and I am using laravel 11.

.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

docker-compose.yml

services:
  nginx:
    build:
      context: .
      dockerfile: nginx.dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
      - php
  mysql:
    image: mysql:latest
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./mysql:/var/lib/mysql
  php:
    build:
      context: .
      dockerfile: php.dockerfile
    volumes:
      - ./src:/var/www/html
  composer:
    build:
      context: .
      dockerfile: composer.dockerfile
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
  npm:
    image: node:current-alpine
    volumes:
      - ./src:/var/www/html
    entrypoint: ["npm"]
    working_dir: /var/www/html
  artisan:
    build:
      context: .
      dockerfile: php.dockerfile
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - mysql
    entrypoint: ["php", "/var/www/html/artisan"]

composer.dockerfile

FROM composer:2

ENV COMPOSERUSER=laravel
ENV COMPOSERGROUP=laravel

RUN adduser -g ${COMPOSERGROUP} -s /bin/sh -D ${COMPOSERUSER}

nginx.dockerfile

FROM nginx:stable-alpine

ENV NGINXUSER=laravel
ENV NGINXGROUP=laravel

RUN mkdir -p /var/www/html/public

ADD nginx/default.conf /etc/nginx/conf.d/default.conf

RUN sed -i "s/user www-data/user ${NGINXUSER}/g" /etc/nginx/nginx.conf

RUN adduser -g ${NGINXGROUP} -s /bin/sh -D ${NGINXUSER}

php.dockerfile

FROM php:8-fpm-alpine

ENV PHPGROUP=laravel
ENV PHPUSER=laravel

RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}

RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf

RUN mkdir -p /var/www/html/public

RUN docker-php-ext-install pdo pdo_mysql

CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

here are the mysql logs

/Users/henrylemmon > cd Sites/laravel-docker.test
/Users/henrylemmon/Sites/laravel-docker.test > docker-compose logs mysql
mysql-1  | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.4.0-1.el9 started.
mysql-1  | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql-1  | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.4.0-1.el9 started.
mysql-1  | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysql-1  | 2024-06-30T05:31:51.870589Z 0 [System] [MY-015015] [Server] MySQL Server - start.
mysql-1  | 2024-06-30T05:31:51.999942Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.4.0) starting as process 1
mysql-1  | 2024-06-30T05:31:52.001025Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
mysql-1  | 2024-06-30T05:31:52.003463Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql-1  | 2024-06-30T05:31:52.062876Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql-1  | 2024-06-30T05:31:52.166437Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql-1  | 2024-06-30T05:31:52.166489Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql-1  | 2024-06-30T05:31:52.168812Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql-1  | 2024-06-30T05:31:52.178376Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.4.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
mysql-1  | 2024-06-30T05:31:52.435588Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
0 likes
6 replies
LaryAI's avatar
Level 58

The error you're encountering, SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'172.18.0.4' (using password: YES), indicates that the MySQL server is rejecting the connection attempt from the Laravel application. This is typically due to incorrect credentials or the MySQL server not being ready when the application tries to connect.

Here are a few steps to troubleshoot and resolve this issue:

  1. Verify MySQL Credentials: Ensure that the credentials in your .env file match those specified in the docker-compose.yml file.

    DB_CONNECTION=mysql
    DB_HOST=mysql
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=laravel
    DB_PASSWORD=secret
    
    services:
      mysql:
        image: mysql:latest
        ports:
          - 3306:3306
        environment:
          MYSQL_DATABASE: laravel
          MYSQL_USER: laravel
          MYSQL_PASSWORD: secret
          MYSQL_ROOT_PASSWORD: secret
    
  2. Check MySQL Container Logs: Check the logs of the MySQL container to ensure it started correctly and is not encountering any issues.

    docker-compose logs mysql
    
  3. Wait for MySQL to be Ready: Sometimes, the application tries to connect to the database before MySQL is fully ready. You can add a health check to the MySQL service to ensure it is ready before other services depend on it.

    Update your docker-compose.yml to include a health check for MySQL:

    services:
      mysql:
        image: mysql:latest
        ports:
          - 3306:3306
        environment:
          MYSQL_DATABASE: laravel
          MYSQL_USER: laravel
          MYSQL_PASSWORD: secret
          MYSQL_ROOT_PASSWORD: secret
        volumes:
          - ./mysql:/var/lib/mysql
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
          interval: 10s
          timeout: 5s
          retries: 3
    
  4. Rebuild and Restart Containers: After making changes to the docker-compose.yml file, rebuild and restart your containers to apply the changes.

    docker-compose down
    docker-compose up --build
    
  5. Run Migrations: Once the containers are up and running, try running the migrations again.

    docker-compose run --rm artisan migrate:fresh
    

By following these steps, you should be able to resolve the MySQL connection issue and successfully run your database migrations. If the problem persists, double-check the MySQL container logs for any additional clues and ensure that the MySQL service is running correctly.

Tray2's avatar

My guess is that you managed to get a trailing space somewhere in your files where you specify the username and password. I would suggest starting a shell on your mysql instance and try to log in from the prompt, if that works then you have a type-o somewhere in your settings files.

lemmon's avatar
Level 28

@tray2 Thank you for your reply. I ran

docker compose exec -it mysql /bin/sh

and I got the below error

ERROR 1045 (28000): Access denied for user 'laravel'@'localhost' (using password: YES)
Tray2's avatar
Tray2
Best Answer
Level 73

@lemmon Then you need to check your docker files.

lemmon's avatar
Level 28

@Tray2 There was a typo in one of my dockerfiles. Thnks

Please or to participate in this conversation.