Laravel Sail SQL Connection timed out - laravel

I just started using Laravel Sail, but Laravel can't connect to the MySQL server. Even Artisan in the container gives Connection timed out. I can connect from MySQL Workbench on my host machine.
My .env file has been updated for use with sail:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Laravel sail does not come with phpMyAdmin to visualize your database. so, you need to go with one of these:
add phpMyAdmin to your docker-compose.yml file manually.
services:
...
phpmyadmin:
image: phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- 8080:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: "${DB_USERNAME}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
...
volumes:
...
phpmyadmin:
driver: local
restart your project again.
sail up -d
sail now will download phpMyAdmin image to your docker container where you
will able to view database on port:8080 [http://localhost:8080/].
use a graphical database management application like TablePlus as mentioned in sail documentation.

I recently ran across this issue too, when upgrading an old Laravel project from using homestead to sail. As per the docs you need to set DB_HOST=mysql in your .env file so that your application container can communicate with the MySQL container.

You must write in "DB_HOST" are domain or ip
DB_HOST=1.2.3.4
or
DB_HOST=example.com

Related

Laravel docker either artisan or model display connection refused

The application is hosted in a docker container locally on my machine as I am working on the app. The php artisan migrate command is working fine. The problem occurs when I try to load the content onto a web page using any of the Models.
Explanation below:
The Env content:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
The migration command works.
php artisan migrate
But, when I try to display the data on a page:
public function index(){
return App\Models\User::all();
}
Laravel says:
SQLSTATE[HY000] [2002] Connection refused
My docker-compose.yml content for mysql:
services:
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
When I change DB_HOST to the name of the container;
DB_HOST=mysql
Laravel starts to display the data, but php artisan migrate does not work.
How it tries to connect to it:
PDO::__construct("mysql:host=mysql;port=3306;dbname=my_db", "root", "", [])
Then fails:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: No such host is known. (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
I need to understand why it is doing so, and how I can resolve this problem.
I would also like to know if this will have the same problem when I deploy the application on production.
Thank you.

Trouble with connecting PostgreSQL in docker-compose

I am trying to create a Laravel project on Docker with a PostgreSQL database locally. The structure of my project is described below:
nginx
conf.d
default.conf
php
Dockerfile
src
Laravel Project
docker-compose-yml
I can run the project successfully on the 8080 port, but I'm having trouble connecting to PostgreSQL.
Here is my docker-compose.yml:
version: '3.8'
networks:
laravel:
services:
nginx:
image: nginx:alpine
command: nginx -g "daemon off;"
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d
depends_on:
- php
networks:
- laravel
php:
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
db:
container_name: postgres
image: postgres
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=root
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_DB=expense
volumes:
- ./postgres:/var/lib/postgresql/data"
ports:
- "5446:5432"
restart: always
networks:
- laravel
In case you want to know, here is the Dockerfile for PHP:
FROM php:8.0.3-fpm
RUN docker-php-ext-install pdo
And also I want to mention the .env that is used by Laravel project:
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=postgres
with this configuration first I've run the docker-compose build and after running the docker-compose up I'm getting the below result for Postgres:
And when I want to reach 127.0.0.1:5432 there's nothing to show me. How can I solve this problem?
In your service db your POSTGRES_DB=expense set the default database name to expense (doc)
So your .env should use DB_DATABASE=expense and not postgres
And a last thing I am not sure about (because I can't find the documentation about it) is your DB_HOST: I don't know if you should use the container_name -> postgres or the service_name -> db, I think it's the service_name so db in your case

Laravel in Docker giving 'php_network_getaddresses: getaddrinfo failed: Name does not resolve'

I am having an issue with my Laravel in Docker.
Currently when I run php artisan migrate inside my container I get the error
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not
resolve (SQL: select * from information_schema.tables where table_schema =
app_database and table_name = migrations and table_type = 'BASE TABLE')
However, I am able to connect to the mysql using Sequel Pro and I am able to see the database created app_database
My docker_compose.yml is as below:
version: '3'
services:
nginx:
build:
context: .
dockerfile: Dockerfile_nginx
container_name: nginx_webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- laravel
database:
image: mysql:8.0
container_name: docker_database
environment:
- "MYSQL_DATABASE=app_database"
- "MYSQL_USER=app_db_user"
- "MYSQL_PASSWORD=app_db_password"
- "MYSQL_ROOT_PASSWORD=password"
volumes:
- ./mysql/db_data:/var/lib/mysql
ports:
- "3306:3306"
php:
build:
context: .
dockerfile: Dockerfile_php
container_name: my_app
volumes:
- ./src:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "9000:9000"
networks:
- laravel
depends_on:
- database
networks:
laravel:
driver: bridge
The .env of my laravel app is
DB_CONNECTION=mysql
DB_HOST=docker_database
DB_PORT=3306
DB_DATABASE=app_database
DB_USERNAME=app_db_user
DB_PASSWORD=app_db_password
Can anybody share some insight?
I have tried everything online.
It looks like you need to add network to database container
networks:
- laravel
I got a similar error. In my case the mistake was using docker run which spun up a new container which was in it's own isolated network instead of using docker compose run which will run the command on one of the containers spun up after docker compose up which will have access to the other containers in the network

Unable to connect to pgsql in Laravel in Docker

I am developing a Web application using Laravel. I am an experienced Laravel developer. But, now I am trying to use Docker as my development environment. But I am so new to Docker. Now I am trying to connect to the Postgres database. I added the Postgres docker image in the docker-composer.yml as well. But when I run migration, I am getting error and it is not connecting to the database.
This is my docker-compose.xml file.
version: '2'
services:
web:
build:
context: ./
dockerfile: docker/web.docker
volumes:
- ./:/var/www
ports:
- "80:80"
- "443:443"
- "9000:9000"
links:
- app
app:
build:
context: ./
dockerfile: docker/app.docker
volumes:
- ./:/var/www
links:
- mysql
- redis
- beanstalk
- cache
environment:
- "DB_PORT=3306"
- "DB_HOST=mysql"
- "REDIS_PORT=6379"
- "REDIS_HOST=redis"
mysql:
image: mysql:5.7.18
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=docker"
ports:
- "3306:3306"
pgsql:
image: postgres:10.1
restart: always
environment:
- POSTGRES_DB=docker
- POSTGRES_USER=root
- POSTGRES_PASSWORD=secret
ports:
- 5431:5431
volumes:
- ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/
redis:
image: redis:3.0
ports:
- "6379:6379"
beanstalk:
image: schickling/beanstalkd
ports:
- "11300:11300"
cache:
image: memcached:alpine
ports:
- "11211:11211"
I know I added the Mysql image as well. When I connect to the Mysql image, it was working. When I connect to the Postgres, I am getting error.
This is my database settings in env file for connecting to the Postgres.
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5431
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=secret
When I run migration in the terminal like this
docker-compose exec app php artisan migrate --seed
I got this error.
In Connection.php line 647:
could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
In PDOConnection.php line 50:
could not find driver
In PDOConnection.php line 46:
could not find driver
What is wrong with my installation?
You didn't add your docker/app.docker details, but if this is PHP running, you need to make sure both php7-pgsql and php7-pdo are installed on this container. driver is usuallu JUST THAT phpX-mysql or phpX-pgsql. X is the optional version number, depending on which repo you're using for php. i.e with default ubuntu repo php-pgsql will be just fine, but with alpine images you have to use php7-pgsql.
with sury repo, you'll have to use specific version i.e php7.2-pgsql.

SQLSTATE[HY000] [2002] Connection refused Laravel 5.6 and Laradock

I have a problem with my database mysql. I can not access it, I have a laravel project which I added Laradock to be able to use the containers with Docker. I launched two containers typing docker-compose up -d mysql nginx in my laradock directory. Once the containers are launched, I can access Laravel's home page, but when I want to communicate with my database I have the following error:
SQLSTATE [HY000] [2002] Connection refused.
Someone has an idea of ​​the source of my problem. Thank you .
i am on ubuntu 17.10
Docker version 17.12.0-ce, build c97c6d6
my .env file in project is here
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db_authors
DB_USERNAME=root
DB_PASSWORD=root
.env file on laradock folder is
MYSQL_VERSION=8.0
MYSQL_DATABASE=db_authors
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
and here is mysql container in my docker-compose.yml
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
my containers are well launched here is the result I get by doing
docker-compose up -d mysql nginx
Creating network "laradock_default" with the default driver
Creating laradock_mysql_1 ... done
Creating network "laradock_backend" with driver "bridge"
Creating laradock_workspace_1 ... done
Creating laradock_applications_1 ...
Creating laradock_php-fpm_1 ... done
Creating laradock_php-fpm_1 ...
Creating laradock_nginx_1 ... do
try the following:
1 - Enter the MySQL container: docker-compose exec mysql bash.
2 - Enter mysql: mysql -uroot -proot
3 - See all users: SELECT User FROM mysql.user;
4 - Run any commands show databases, show tables, select * from......
Does it work? Can you log on MySQL like that?

Resources