Cannot connect to the database - An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
cshelswell
Posts: 2
Joined: 11 May 2023, 03:33

Cannot connect to the database - An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

Post by cshelswell » 11 May 2023, 03:42

Hi - I'm really hoping someone might be able to help me with this as I've been struggling for days.

For some reason no matter what I do when I perform an install and either use Docker and "sail up" or if I use "php artisan serve" I get the error:

Code: Select all

An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused: SET SESSION sort_buffer_size=2097144; SET NAMES 'utf8mb4'; SET SESSION sql_mode='ANSI'
I can login to the DB no problem using Sequel Pro, the migrations all work through the terminal.

After install I perform "sail up" then I can provide the correct details for the DB connection as per my docker-composer.yml file and my .env file but I just can't get the browser past that error message.

My docker-compose.yml file is:

Code: Select all

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mariadb
            - redis
            - meilisearch
            - minio
            - selenium
    mariadb:
        image: 'mariadb:10.4'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mariadb:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
    memcached:
        image: 'memcached:alpine'
        ports:
            - '11211:11211'
        networks:
            - sail
    meilisearch:
        image: 'getmeili/meilisearch:latest'
        ports:
            - '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
        volumes:
            - 'sail-meilisearch:/meili_data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "wget", "--no-verbose", "--spider",  "http://localhost:7700/health"]
            retries: 3
            timeout: 5s
    minio:
        image: 'minio/minio:latest'
        ports:
            - '${FORWARD_MINIO_PORT:-9000}:9000'
            - '${FORWARD_MINIO_CONSOLE_PORT:-8900}:8900'
        environment:
            MINIO_ROOT_USER: 'sail'
            MINIO_ROOT_PASSWORD: 'password'
        volumes:
            - 'sail-minio:/data/minio'
        networks:
            - sail
        command: minio server /data/minio --console-address ":8900"
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
            retries: 3
            timeout: 5s
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
            - '/dev/shm:/dev/shm'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mariadb:
        driver: local
    sail-redis:
        driver: local
    sail-meilisearch:
        driver: local
    sail-minio:
        driver: local
I've tried using the standard Mysql driver as well but same problem. My .env file is:

Code: Select all

APP_NAME=Aimeos
APP_ENV=local
APP_KEY=base64:OVsDA6nbFW6JevYFnrQLWdsfTzP0bTp6XUMt0Y7qJTc=
APP_DEBUG=1
APP_VERSION=1
APP_URL=http://localhost:8000
#ASSET_URL=http://localhost:8000
SHOP_MULTILOCALE=
SHOP_MULTISHOP=
SHOP_REGISTRATION=
SHOP_PERMISSION=admin
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=amazon_shops
DB_USERNAME=forge
DB_PASSWORD=forge
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY=
MIX_PUSHER_APP_CLUSTER=
Any help would be very gratefully received - Thanks so much.

User avatar
aimeos
Administrator
Posts: 7889
Joined: 01 Jan 1970, 00:00

Re: Cannot connect to the database - An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

Post by aimeos » 11 May 2023, 06:57

The "connection refused" message from MySQL is not about wrong credentials but it seems that the host name/IP and/or port is not right one.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

cshelswell
Posts: 2
Joined: 11 May 2023, 03:33

Re: Cannot connect to the database - An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

Post by cshelswell » 11 May 2023, 09:15

Thank you so much for your quick response.

I've tried changed ports / URLs etc but still end up with the same message. On a couple of occasions I've managed to get it to load the /admin page but even after creating the user it won't log in.

It's getting a bit late here in New Zealand so I think I'll probably attack it again tomorrow and let you know my findings.

I'm sure it'll be something really simple that I've just overlooked, that always tends to be the case in situations like this :)

Thanks again

Post Reply