简体   繁体   中英

Why am I getting SQLSTATE[HY000] [2002] Connection refused from Laravel on Docker?

This is my docker-compose:

version: '3'
services:
  web:
    build: .
    image: citadel/php7.3
    ports:
     - "80"
    volumes:
      - ./src:/home/app/src:cached
    container_name: 'studentlaptops'
    restart: unless-stopped
    tty: true
    environment:
      - XDEBUG_CONFIG='remote_host=<my-host-name>'
      - VIRTUAL_HOST=studentlaptops.docker
        #MySQL Service
  db:
    image: mysql:8.0.1
    container_name: sl-db
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: studentlaptops
      MYSQL_ROOT_PASSWORD: <password>
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf

#Volumes
volumes:
  dbdata:
    driver: local

When I run docker-compose, it comes up fine. When I connect via a gui client, I can access the sl-db database in the container. When I run php artisan migrate, it migrates successfully.

However when I hit the application in the web browser SQLSTATE[2002]Connection refused...

My env db section is:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=studentlaptops
DB_USERNAME=root
DB_PASSWORD=<password>

In your case I would change

DB_HOST=127.0.0.1

to

DB_HOST=db

which is the name of your mysql in docker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM