Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SimonAngatia's avatar

SQLSTATE[HY000] [2002] Connection refused error when running sail artisan migrate command

In the process of running migrations in my application, some tables gets migrated to the database but when it gets to one table, it throws an error that connection refused:

Migrating: 2020_01_05_160933_create_sessions_table
Migrated:  2020_01_05_160933_create_sessions_table (801.99ms)
Migrating: 2020_01_06_102937_create_devices_table
Migrated:  2020_01_06_102937_create_devices_table (447.02ms)
Migrating: 2020_01_16_110833_create_settings_table
Migrated:  2020_01_16_110833_create_settings_table (711.88ms)
Migrating: 2020_01_18_171110_create_matches_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (SQL: create table `matches` (`id` bigint unsigned not null auto_increment primary key, `bet365_id` bigint not null, `bets_api_id` bigint null, `sport_id` bigint not null, `home_team_id` bigint not null, `away_team_id` bigint not null, `league_id` bigint not null, `cc` varchar(255) null, `starts_at` int not null, `time_status` tinyint not null, `last_bets_api_update` int not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

I may not know why the connection is timing out on from one migration. KIndly help. My docker-compose.yml:

version: '3'
services:
    laravel.test:
        build:
            context: ./docker/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            # - mysql
            # - memsql
            - pgsql
            - redis
            # - selenium
    # selenium:
    #     image: 'selenium/standalone-chrome'
    #     volumes:
    #         - '/dev/shm:/dev/shm'
    #     networks:
    #         - sail
    mysql:
        image: 'mysql:8.0'
        ports:
            - '3305:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
    pgsql:
        image: postgres:13
        ports:
            - '${FORWARD_DB_PORT:-5432}:5432'
        environment:
            PGPASSWORD: '${DB_PASSWORD:-secret}'
            POSTGRES_DB: '${DB_DATABASE}'
            POSTGRES_USER: '${DB_USERNAME}'
            POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - 'sailpostgresql:/var/lib/postgresql/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
#    memsql:
#        image: 'memsql/cluster-in-a-box'
#        ports:
#            - '3306:3306'
#            - '8080:8080'
#        environment:
#            LICENSE_KEY: '${MEMSQL_LICENCE}'
#            ROOT_PASSWORD: '${MEMSQL_ROOT_PWD}'
#        volumes:
#              - 'sailmemsql:/var/lib/memsql'
#        networks:
#              - sail
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
    # memcached:
    #     image: 'memcached:alpine'
    #     ports:
    #         - '11211:11211'
    #     networks:
    #         - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailmemsql:
        driver: local
    sailpostgresql:
        driver: local
    sailredis:
        driver: local

My .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=appdb
DB_USERNAME=username
DB_PASSWORD=password
0 likes
11 replies
jlrdw's avatar

Re-read about migrating in sail, I am pretty sure you precede with the word sail.

Edit:

Verify the host is correct for sail.

SimonAngatia's avatar

@jlrdw Yes, I do that. It migrates some tables but when it gets to others, it says that the connection refused. What I am doing to run the migrations: ./sail artisan migrate

jlrdw's avatar

@SimonAngatia Are the top migrations and the one failing all in the same docker container. That's strange some work and others fail, double check spelling and the migration file for errors, maybe you have something wrong.

SimonAngatia's avatar

@Sinnbeck It migrates sequentially until it gets to the matches migration and that is when it throws the error. This is that migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMatchesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection(config('database.feed_connection'))->create('matches', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('bet365_id');
            $table->bigInteger('bets_api_id')->nullable();
            $table->bigInteger('sport_id')->index();
            $table->bigInteger('home_team_id');
            $table->bigInteger('away_team_id');
            $table->bigInteger('league_id');
            $table->string('cc')->nullable();
            $table->integer('starts_at');
            $table->tinyInteger('time_status');
            $table->integer('last_bets_api_update');
            $table->timestamps();

            $table->unique(['bet365_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection(config('database.feed_connection'))->dropIfExists('matches');
    }
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@SimonAngatia so you are hardcoding the connection. Most likely that connection fails. Your env only shows one connection?

I assume the other migrations does not use this connection?

Schema::connection(config('database.feed_connection'))-
Sinnbeck's avatar

@SimonAngatia then you now know where to look. Find out what's wrong with it. If you need help, show the env and config for it

SimonAngatia's avatar

@Sinnbeck Thank you so much. I have been having this issue for the whole of yesterday. I never opened that migration to look at how it is configured. I am doing edits on that application and I never checked that migration. I was just trying to set it up on my machine. I used your suggestion to check the migration and fix it. Now it is working correctly and all the migrations have been migrated.

Please or to participate in this conversation.