简体   繁体   中英

How to fix SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `categories` where `enable` = 1) on Laradock?

I'm trying to build laravel on docker, But when I tried to run php artisan migrate I got an error like this:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from categories where enable = 1)

Error in Output

Error in Output

This is my Dockerfile

FROM php:7.2-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql

This is my docker-compose.yml

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:5.7.22
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: cilsy
      MYSQL_USER: cilsy
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www
    ports:
      - "9000:9000"
    networks:
      - laravel

this is my.env file

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cilsy
DB_USERNAME=cilsy
DB_PASSWORD=secret

I have install composer php-common php-mbstring php-xml php-zip php-json libmcrypt-dev php-curl .

I don't know how to fix this problem, any ideas?

Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1" .

Can you run php artisan config:clear and restart the server

try to update XAMPP or whatever server using and with the latest PHP version.

I got the same error when I shifting my project to mac I tried everything at last I uninstall the XAMPP and install the XAMPP with the latest PHP version updated version.

I hope that helps.

Open the .env file and change it DB_HOST=localhost to DB_HOST=127.0.0.1

  • DB_USERNAME should be set to root if you do not have a default username in the installation time.
  • DB_PASSWORD If no password is set on the database, just clear it

After .env edit, must be clear cache: php artisan config:cache

In my case, changing the DB_HOST to localhost didn't fix the issue. I had to change it to mysql which works fine

在此处输入图像描述

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