'Failed to connect database using docker in laravel

i have a problem when connect database using laravel and docker ,i got error like this :

 Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = sisuket and table_name = migrations and table_type = 'BASE TABLE')

and this is my docker-compose in section mariadb

  mariadb:
    image: docker.io/bitnami/mariadb:10.7
    ports:
      - 3306:3306
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - MARIADB_DATABASE=sisuket
      - MARIADB_ROOT_USER=sisuket
      - MARIADB_ROOT_PASSWORD=sisuket
    restart: always
    networks:
      - app-tier

and this is my .env and database.php , i have try to change DB_HOST to be localhost and 127.0.0.1 . but,that's useless

APP_NAME=app
APP_ENV=local
APP_KEY=base64:aQ+ckCjANh0NPWNp1zOWJYHS6k3eiaoBmMVdhX93oqQ=
APP_DEBUG=true
APP_URL=http://localhost:80

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST= mariadb
DB_PORT=3306
DB_DATABASE=sisuket
DB_USERNAME=sisuket
DB_PASSWORD=sisuket

[//database.php
 'mysql' => \[
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'mariadb'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'sisuket'),
            'username' => env('DB_USERNAME', 'sisuket'),
            'password' => env('DB_PASSWORD', 'sisuket'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter(\[
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            \]) : \[\],
        \],][1]


Solution 1:[1]

Thre thing what you can try:

  1. php artisan config:cache. If that not work =>
  2. Remove the whitespace in the line DB_HOST= mariadb => DB_HOST=mariadb and php artisan config:cache. If that not work =>
  3. If that not work change DB_HOST to DB Host DB_HOST=127.0.0.1 and php artisan config:cache.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maik Lowrey