I have a problem when I use Terminal to migrate or make some command, I am getting error
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for database failed: Name or service not known
My ENV:
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=products
DB_USERNAME=homestead
DB_PASSWORD=secret
These informations are in the Docker too. And code works on localhost when I open app, but in terminal I am getting error. When I change DB_HOST to localhost or 127.0.0.1 then it works in terminal, but it doesn't work on app on web and it doesn't save data in same database. I don't know what else to do. Is there any way to make to work both and terminal and the web.
database:
image: mysql:5.7
container_name: database
ports:
- ${FORWARD_DB_PORT:-3306}:3306
environment:
MYSQL_DATABASE: products
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- .docker/database/entrypoint/:/docker-entrypoint-initdb.d
networks:
- app-network
CodePudding user response:
There are several possible causes for this error:
- The database server may be down or not accessible from the network.
- The hostname of the database server may be incorrect or mistyped.
- There may be a problem with the DNS server or the network configuration that prevents the hostname from being resolved.
To troubleshoot the issue, you can try the following steps:
- Verify that the database server is running and accessible from the network.
- Check the hostname of the database server and make sure it is correct.
- Try pinging the hostname of the database server to see if it can be resolved to an IP address.
- Check the network configuration and DNS settings to ensure that they are correct.
CodePudding user response:
I solved this by executing commands (php artisan...) in terminal of Docker
or executing it calling firstly docker exec -it php-fpm /bin/bash
`(php-fpm is my container name)