skip to Main Content

I’m trying to run "php artisan migrate" in docker app container bash, after successfully installing mysql, nginx, php and etc. containers.

But this error comes up :

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = code_clan and table_name = migrations and table_type = ‘B
ASE TABLE’)

This is my folder/files structure :

> docker-my-app(folder)
  > docker(folder)
    > nginx(folder)
      > default.conf
    > php-fpm(folder)
      > Dockerfile
  > src(folder)
    > www(folder)
      > Laravel Project Files...
      > **.env**

  **docker-compose.yml**

This is my composer file (docker-compose.yml):

version: '3.8'

services:

  nginx:
    ports:
      - "80:80"
    image: nginx
    tty : true
    restart: unless-stopped
    volumes:
      - ./src/www:/usr/share/nginx/html:delegated
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - thenetwork
  
  app:
    build: ./docker/php-fpm
    container_name: myapp
    tty : true
    restart: unless-stopped
    environment:
      SERVICE_NAME: myapp
      SERVICE_TAGS: dev
    networks:
      - thenetwork
    working_dir: /usr/share/nginx/html
    volumes:
      - ./src/www:/usr/share/nginx/html
      - ./data/upload:/storage/uploads
  
  memcached:
    image: memcached

  mysql:
    ports:
      - "3306:3306"
    image: mysql:5.6
    restart: unless-stopped
    tty: true
    volumes:
      - mysqlvolume:/var/lib/mysql
      - ./data/mysql-import:/docker-entrypoint-initdb.d
    environment:
        - MYSQL_ROOT_PASSWORD=pwd
        - MYSQL_HOST=localhost
        - MYSQL_PORT=3306
    networks:
      - thenetwork

  redis:
    ports:
      - "6379:6379"
    image: redis



#Docker netwtorks
networks:
  thenetwork:
    driver: bridge
volumes:
  mysqlvolume:
    driver: local

This is my .env file (inside the root of laravel project):

APP_NAME=MyApp
APP_ENV=local
APP_KEY=base64:generatedKey
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=daily
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lara_app
DB_USERNAME=root
DB_PASSWORD=pwd
...

Inside docker-my-app cmd, I run :

 docker exec -it myapp bash

And inside there, I run :

/usr/share/nginx/html$ php artisan migrate

all the containers, including mysql are running successfuly.
And the error mentioned at the beginning happens.

Can you figure out what happens here?

2

Answers


  1. Chosen as BEST ANSWER

    After trying different ways:

    1. like setting up an .env file for docker-compose.yml
    2. and trying to clear the cache of laravel config and cache files inside docker myapp bash , like this:
    php artisan config:clear
    php artisan cache:clear
    
    1. at the end changing the DB_HOST constant inside laravel root .env file from 127.0.0.1 to mysql, solved my problem and migration and everything related to mysql is fine now. The reason was that I named the mysql service inside docker-compose.yml file to mysql.

    .env file :

    APP_NAME=MyApp
    APP_ENV=local
    APP_KEY=base64:generatedKey
    APP_DEBUG=true
    APP_URL=http://localhost
    
    LOG_CHANNEL=daily
    LOG_DEPRECATIONS_CHANNEL=null
    LOG_LEVEL=debug
    
    DB_CONNECTION=mysql
    **DB_HOST=mysql**
    DB_PORT=3306
    DB_DATABASE=lara_app
    DB_USERNAME=root
    DB_PASSWORD=pwd
    ...
    

  2. I am on MAMP. I just changed the port to 8889 and the password. It works
    php artisan config:cache
    php artisan cache:clear
    php artisan migrate

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search