Roboroads/laravel-tinker

Database access results in php_network_getaddresses error

matthewknill opened this issue · 20 comments

Bug description
Database access does not work resulting in the following error:

Illuminate\Database\QueryException with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known (SQL: select * from `orders` where `orders`.`id` = 2 limit 1)'

To Reproduce
Run any code that requires access to the database. If the code does not require access to the database, it seems to work fine.
I can also confirm that my application has access to the database and running tinker locally with sail artisan tinker also works fine with the database.

The following is the code that I used:

//Tinker away!

use App\Models\Order;

Order::find(2);

Screenshots
image

Environment (please complete the following information):

  • IDE & Version: IntelliJ IDEA 2022.1.2 (Ultimate Edition)
  • PHP Version: 8.1.7
  • OS: Windows 10
  • Laravel Tinker plugin version: 2.3.4
  • Interpreter Type: Docker (using Laravel Sail)
  • Laravel Version: 8.83
  • Tinker version: 2.7.2

This usually means that the interpreter you are using not the one is you are developing with. Not a problem with tinker - just that where you run PHP cannot reach your DB from there.

Check which container you are using - try to docker exec -it containername php artinsan tinker to see if that works (as it probably shouldn't).

Thanks for that, when I ran docker exec -it containername php artisan tinker, it worked fine. Is that not to be expected?

Then you should check your php interpreter again. Something is not making it connect to your database.

It's definitely the correct image and I've tried changing the path mappings to /var/www/html and PHP executable to the following to no avail:

whereis php # run in Laravel sail shell
php: /usr/bin/php8.1 /usr/bin/php /usr/lib/php /etc/php /usr/include/php /usr/share/php

image

Your help is much appreciated thanks...

I'm still 98% sure this is a thing in your setup that's not working. The plugin is not doing anything with your networking. Does a sail command do some extra linking whilst running as just a docker container isn't, maybe?

I could take a gander at it over a screenshare on discord if you want to. If you'd like, message me and we'll figure out a time. Roboroads#1337.

Thanks, request sent

Solution was to use docker-compose.yml and setting it as docker-compose exec:
image

I then faced the following issue which, I believe, can be solved by upgrading to the latest PHPStorm version: #14

Hello, I'm using OS Win11 with WSL2, in the current project I use Laradock with Laravel 7.

I tried to run the command as shown below, but the error is returned in the output.

image

CLI
image

Do I need to configure anything else in PhpStorm?

So that means the settings in your .env (or config/database.php ofc.) Are not valid for thst docker container. Make sure that the docker container has a way to connect to the database (make sure they are in the same network?)

I would suggest using docker compose, the setup is a little more user friendly.

I use Laradock in this project, the settings in the .env file are correct, even because I use migrations and access externally in DBMS normally.
That's why I thought it could have some additional configuration.

Where does Laravel Tinker get the credentials/host to access the database? from the .env file?

The plugin runs PHP like you configured. So in your case, Tinker runs as if you were to docker run --rm -it blankedout-workspace:latest php artisan tinker. You need to be sure to also add all the other settings to be able to connect to your DB container.

I just checked the Laradock compose file, and it's using the following compose file for MySQL:

   mysql:
      # Redacted unuseful information
      ports:
        - "${MYSQL_PORT}:3306"
      networks:
        - backend

PHPStorm, using this method, spins up a new docker container. This new container is probably not in this composes "backend" network - which you need to add to your interpreter settings. The reason your webcontainer is able to connect is because following the compose file, they are in the same network (as your migrations are working) and since port 3306 is exposed to yor hostcomputer, your DBMS is able to connect to localhost:3306.

Dont just use --network backend. Since it's a compose file the name is probablyt a little different. Check which network it is using docker network list.

In the latest PHPStorm there is no option for "docker-compose exec". When I run docker exec -it containername php artisan tinker it works fine and my interpreter settings are all correct

image

@quantumwebco
I, using the latest PHPStorm, have the option to choose.
image

You're not using compose btw, just raw docker. Make sure your newly spun up container can reach the mysql instance :)

@Roboroads What version? I'm on 2023.1.4 and it simply doesn't have that option as per my screenshot above. Am I in the wrong place? Settings-> PHP -> CLI Interpreters ...

My container reaches mysql fine, I can tinker from within the container by doing sail shell or running docker exec -it containername php artisan tinker

I've been through every issue and thing I can find and I'm just at a total loss now

I really regret moving my workflow to docker, it's been a nightmare, might go back to VMs

image

@quantumwebco Your interpreter is "Docker", not "Docer compose".

image
(when creating an interpreter)

If you want to keep on using docker, the container needt all the settings it needs to reach the mysql container.

Aaah I see, thank you so much!! Just got it working. Might be worth mentioning that you need to choose "Docker for windows - default" in the server settings as I tried with WSL first but that didn't work. It's my ignorance tho, I just haven't got my head round docker at all yet

@quantumwebco Everyone has to learn some time, and once you get the hang of docker, it's going to be great!

@Roboroads I had the same issue with the database using docker. But when I changed php to docker-compose Tinker stopped work totally
Could you help, please?

Ok, I just wasted like a few hours on this too.

I have reasons to believe that phpstorm has problems with parsing the docker compose file which is version 3.
I asked chatgpt to convert the file to version 2.4 (just don't forget to back up).

Then I reconfigured the interpreter, server and testing frameworks in phpstorm. Then it started working...

image
image
image
image

I have reasons to believe that phpstorm has problems with parsing the docker compose file which is version 3. I asked chatgpt to convert the file to version 2.4 (just don't forget to back up).

That should not be the case.. Compose is just a way of running docker..