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

Makka's avatar
Level 2

Cannot connect to DB?

Hello, back to playing around in Laravel after a long break. I've gone down the docker route using WSL on windows running ubuntu this time. It's all quite fancy.

While my .env fine has the MySQL host set to 127.0.0.1 I can connect to the MySQL container in the terminal, and my DB program and using artisan migrate and tinker all work. however, when I try to connect to the DB in the browser through I route I get the following error.

SQLSTATE[HY000] [2002] Connection refused

I read online you can change the host to the container name but when i do this i get the following error in the browser and also all functions that were working in the command line stop working.

browser error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql-1 failed: Temporary failure in name resolution

command line error (pa migrate)

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql-1 failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations and table_type = 'BASE TABLE')

Not sure what else to try. Pretty new to this whole docker thing and ive tried everything google had to offer.

My .env file if usefull.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=sail
DB_PASSWORD=password

Any help would be awesome, Thanks!

0 likes
12 replies
Makka's avatar
Level 2

@Sinnbeck

    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
Makka's avatar
Level 2

@Sinnbeck Seems the web will still work while the host it set to this however the command line will not. I think this is what made me change it to 127.0.0.1 in the first place.

$ php artisan migrate:status

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations and table_type = 'BASE TABLE')
Makka's avatar
Level 2

@Sinnbeck Sorry this is a little confusing to me, so running commands inside wsl after you have used sail up is incorrect? could you give me an example of the correct way to run an artisan command, please? or possibly point me to some reading / watching on the subject?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Makka if you are using sail, you can run sail artisan migrate for instance

Makka's avatar
Level 2

@Sinnbeck Ok, and that works along with solving my issue. I can see it here https://laravel.com/docs/9.x/sail#executing-sail-commands I'm trying to wrap my head around how this is all working but I'm failing to understand the relationship between win, WSL, docker & sail correctly I think. I'm still pretty new to docker and only understand the basics (i think). Do you know of an ESLI5 explanation or could provide one to clear it up a little for me?

Sinnbeck's avatar

@Makka yeah it's a bit hard to wrap your head around. I don't know of any videos sadly as I learned docker 4 years ago, but feel free to ask as anything

Quick explanation.

Wsl2 is just a kernel for Linux that allows you to run a Linux os inside windows (Ubuntu in this case). I personally just run Ubuntu directly (no windows)

Now for docker. Imagine a tiny network of computers (containers) which each have their own service they run. 1 runs php, another mysql and so on. Inside the network, they can talk to each other using their hostname (for instance "mysql"). 127.0.0.1 won't work as that would mean that the computer is connection to itself. You can open ports into this network to connect to one of the computers (this is why you could connect to mysql from the outside). But if you want to work on the php computer you first need to log into it and run the command (sail does this for you)

Hope that helps a bit

Makka's avatar
Level 2

@Sinnbeck Thank you. It's always a lot to relearn when you come back to anything development related!

Personally, I've also always either used Ubuntu on dual boot or Mac when I had my MBP. but I thought this docker route with WSL sounded good because I could have a Linux feel on Win, along with docker being a good tool to learn.

Can you tell me why WSL is needed for docker to work? Does it use the base ubuntu install as an image for each of the nodes? I would assume it would be a stripped-down kernel-only thing with only the bare minimum to run the service on that node?

So from what I understand it is basically like a mini cluster with each service on its own node and in this case sail is providing an easy way to interface with each node? While looking at the docker desktop app I can see there are 6 nodes with mysql being one. I seem to understand what most of the nodes are for apart from selenium and laravel.test. is laravel.test the "apache" node? I have to admit, docker is some real next-level gypsy magic. just from my limited understanding, it seems super powerful.

Sinnbeck's avatar

@Makka docker can run on windows without wls2/Ubuntu but as they are different file systems it can be slow. Wsl2 fixes this issue.

Each container is a tiny Linux image but has nothing to do with your os. One container might run Ubuntu, a second debian and a third alpine. So instead of having one huge virtual machine that does everything, you have many tiny ones that speak with each other on a local virtual network.

Selenium is chrome. It's used for testing your site with dusk. I think the laravel.test is php and php artisan serve (don't use sail myself as I prefer lando)

Makka's avatar
Level 2

--Just a quick update, it seems that the container name wasn't showing in full on the "Overview" page so I had it set incorrectly "mysql-1" instead of "blog-mysql-1".

This has fixed the browser error however none of the command line functions work now. Is there a happy middle ground here to get both working?

$ php artisan migrate:status

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for blog-mysql-1 failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations and table_type = 'BASE TABLE')

Please or to participate in this conversation.