_christoph's avatar

getaddrinfo for mysql failed: nodename nor servname provided

Ok, this is a really weird one. I have my environment set up in docker and whenever I'm developing, I run sail up to start all the containers and I'm off to the races. The last time I did any development on my site was on Sunday and everything was working great. Today, however, when I try to access my pages, I'm getting the following error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided

Now, I know what you are going to say -- something changed in your .env file w/r/t connection information. Except that it didn't. These are the settings I have:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
FORWARD_DB_PORT=3306
DB_DATABASE=communal_haven
DB_USERNAME=root
DB_PASSWORD=IM_NOT_THAT_CRAZY

When I run php artisan serve (on my local machine, which is where I've always run it), this is what I'm seeing as it pertains to the MySQL image:

communal-haven-mysql-1        | 2022-01-25T23:23:05.676945Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
communal-haven-mysql-1        | 2022-01-25T23:23:05.686315Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
communal-haven-mysql-1        | 2022-01-25T23:23:05.820621Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
communal-haven-mysql-1        | 2022-01-25T23:23:06.019745Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
communal-haven-mysql-1        | 2022-01-25T23:23:06.019797Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
communal-haven-mysql-1        | 2022-01-25T23:23:06.020758Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
communal-haven-mysql-1        | 2022-01-25T23:23:06.020819Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
communal-haven-mysql-1        | 2022-01-25T23:23:06.048244Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
communal-haven-mysql-1        | 2022-01-25T23:23:06.048491Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.

As far as I can tell, everything looks copacetic there.

In docker, for my server image (sail-8.0/app), I open it up in the CLI and from that command line I can run

root@6406065745f6:/var/www/html# mysql -h mysql -u root -p

(where that second mysql is what is set up as the DB_HOST in the .env file) and I'm able to connect to it just fine. Additionally, if I run php artisan tinker while on that command line I can run User::get() no problem to retrieve all the users.

I'm not sure what else to look at/for because as far as I can tell, everything is working as it should. I don't suppose anyone has any ideas as to where I can look next?

EDIT: What's interesting is if, in my site docker shell, I run php artisan serve there then it starts working again. As stated previously, I would always run that on my local machine (outside of docker) and it never had a problem connecting to MySQL prior to today.

christoph

0 likes
11 replies
bryanfunk's avatar

Been seeing this go years back. No one seems to have an answer, and those who know aren't saying anything.

bobbybouwmann's avatar

How are you running your commands? With sail you should perform all actions through sail. So for migrate you run sail artisan migrate

2 likes
bryanfunk's avatar

I found the culprit after about 10 hours of looking and trying everything. Within /vendor/laravel/framework/src/Illuminate/Database/Migrations there were somehow 10+ migrations build and fighting against each other somehow. I ran it by the documentation and committed each step to a github repo so you can see I do no more no less. I deleted ALL of those migrations. ran php artisan migrate with the result of nothing to migrate.

bryanfunk's avatar

I thought I was able to post screenshots here but I guess not. I just went to commit my work and noticed these migrations go back to 2014, and 2020, and than my migration that I setup yesterday/today. I'll have to pull down the laravel package again for macOS but I'm thinking the installer package is possibly shipping with those and causing the trouble?

bobbybouwmann's avatar

That is very odd behaviour :P This has probably nothing to do with docker itself. Anyway, glad you fixed it!

richardazia's avatar

I got this error today. It occured because I had experimented with laravel sail. It changed the .env file to give DB_HOST=mysql when it should have stayed as 127.0.0.1. Once I corrected this my database connection was re-established. It also changed the db username so I had to change that too. I hope that this helps people with this issue, in future. The fix is simple, when you figure out where to look. It took me a while to figure it out.

4 likes
Dirk313's avatar

@twoarmtom Did you get this resolved ? because i have the same issue using mariadb with docker

In Connection.php line 760:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: nodename nor servname provided, or not known (SQL: select * from information _schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

In Exception.php line 18:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: nodename nor servname provided, or not known

In PDOConnection.php line 40:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: nodename nor servname provided, or not known

In PDOConnection.php line 40:

PDO::__construct(): php_network_getaddresses: getaddrinfo for mariadb failed: nodename nor servname provided, or not known

twoarmtom's avatar

@Dirk313 I just changed my DB_HOST back to 127.0.0.1 and it was resolved. Installing Sail changed it to DB_HOST=mysql. But my problem wasn't with Docker, it was after switching back to using Valet.

blueminded's avatar

@twoarmtom are you using sail? It's that the case, you just need to run your artisan commands with sail like:

sail artisan route:list or whatever command you need.

1 like
thomasjb's avatar

Not sure if this is still relevant, but as I just punched myself in the head over a similar issue: Run your migrations inside the Docker Container / sail app. Using your IDE terminal outside the Docker container will not work.

Please or to participate in this conversation.