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

tenzan's avatar

PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known

Hi,

I'm getting an error specified in the subject of this thread, when I run phpunit to test a feature. Laravel version is latest.

Concert.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Concert extends Model
{
    use HasFactory;

    protected $fillable = ['title','subtitle', 'date', 'ticket_price','venue','venue_address','city','state','zip','additional_information'];
}

ViewConcertListingTest.php:

<?php

namespace Tests\Feature;

// use Illuminate\Foundation\Testing\RefreshDatabase;

use App\Models\Concert;
use Carbon\Carbon;
use Tests\TestCase;

class ViewConcertListingTest extends TestCase
{
    /** @test */
    function user_can_view_a_concert_listing()
    {
        // Arrange
        // Create a concert
        $concert = Concert::create([
            'title' => 'Sample Title',
            'subtitle' => 'Sample Subtitle',
            'date' => Carbon::parse('December 13, 2016 8:00pm'),
            'ticket_price' => 3250,
            'venue' => 'Sample Venue',
            'venue_address' => '123 Sample Address',
            'city' => 'Some City',
            'state' => 'ON',
            'zip' => '12345',
            'additional_information' => 'For tickets, call (555) 555-5555.'
        ]);

        // Act
        // View the concert listing
        $this->visit('/concerts/'.$concert->id);

        // Assert
        // see the concert details
        $this->see('Sample Title');
        $this->see('Sample Subtitle');
        $this->see('December 13, 2016');
        $this->see('8:00pm');
        $this->see('32.50');
        $this->see('Sample Venue',);
        $this->see('123 Sample Address');
        $this->see('Some City, ON 12345');
        $this->see('For tickets, call (555) 555-5555.');
    }
}

docker-compose.yml:

# 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:
            - mysql
            - redis
            - meilisearch
            - selenium
    mysql:
        image: 'mysql/mysql-server:8.0'
        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-mysql:/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
    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
    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-mysql:
        driver: local
    sail-redis:
        driver: local
    sail-meilisearch:
        driver: local
0 likes
3 replies
mvd's avatar
mvd
Best Answer
Level 48

@boyjarv

Can you check your .env file , if this is correct DB_HOST=mysql ?

And run php artisan config:clear ?

If so, does sail test work?

2 likes
tenzan's avatar

Thanks a lot. It seems I had to run sail test command to run phpunit. My snippet from .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ticketbeast
DB_USERNAME=sail
DB_PASSWORD=password

Now when I run sail test, I'm having different error:

  PASS  Tests\Unit\ExampleTest
  ✓ that true is true

   FAIL  Tests\Feature\ViewConcertListingTest
  ⨯ user can view a concert listing

  ---

  • Tests\Feature\ViewConcertListingTest > user can view a concert listing
   Illuminate\Database\QueryException 

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.concerts' doesn't exist (SQL: insert into `concerts` (`title`, `subtitle`, `date`, `ticket_price`, `venue`, `venue_address`, `city`, `state`, `zip`, `additional_information`, `updated_at`, `created_at`) values (Sample Title, Sample Subtitle, 2016-12-13 20:00:00, 3250, Sample Venue, 123 Sample Address, Some City, ON, 12345, For tickets, call (555) 555-5555., 2022-08-17 13:40:15, 2022-08-17 13:40:15))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
    755▕         // If an exception occurs when attempting to run a query, we'll format the error
    756▕         // message to include the bindings with SQL, which will make this exception a
    757▕         // lot more helpful to the developer instead of just the database's errors.
    758▕         catch (Exception $e) {
  ➜ 759▕             throw new QueryException(
    760▕                 $query, $this->prepareBindings($bindings), $e
    761▕             );
    762▕         }
    763▕     }

      +18 vendor frames 
  19  tests/Feature/ViewConcertListingTest.php:28
      Illuminate\Database\Eloquent\Model::__callStatic()


  Tests:  1 failed, 1 passed
  Time:   0.56s
tenzan's avatar

I think we're good to close this post, as all I had to do is to run sail test. An error I'm having after running sail test is not related to the subject of the post.

Thanks again, @mvd !

1 like

Please or to participate in this conversation.