Why won't Laravel Sail allow me to connect to the DB - php

Anyone familiar with using Laravel Sail? I can't find for the life of me figure out why I can't connect to myDb using the credentials provided when installing Sail.
My DB credentials in the .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3307
DB_DATABASE=myDb
DB_USERNAME=sail
DB_PASSWORD=password
I've tried:
switching DB_HOST back and forth from 127.0.0.1 to localhost
but still getting that same error.
switching DB_HOST to mysql but still getting that same error.
running docker ps -a | grep mysql and the output is: 8b1ef6c28b4e mysql/mysql-server:8.0 "/entrypoint.sh mysq…" 4 minutes ago Up 4 minutes (healthy) 3306/tcp, 33060-33061/tcp, 0.0.0.0:3307->3307/tcp myApp-mysql-1 so no issues with the connection per the output.
running ./vendor/bin/sail sail mysql -u root -p and it allowed me access to mysql on the CLI
almost everything else out there to no avail
Not sure what else to do. How can I connect to my db in a GUI?
docker-compose.yml file:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${HMR_PORT:-8080}:8080'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3307}:3307'
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
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local

Change the DB_HOST variable to mysql as the following:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=myDb
DB_USERNAME=sail
DB_PASSWORD=password

Related

Laravel Sail: Run Mysql PHP Migration inside docker container

I have currently a laravel sail project, means a docker container split up in different environments.
I just created a migration under /database/migrations, in order to create an App Model for an e-mail form.
Hoewever, I am not able to run a php migration inside my main docker container
I receive the following error message:
Illuminate\Database\QueryException
SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format > the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕ }
• Database name seems incorrect: You're using the default database name laravel. >This database does not exist.
Edit the .env file and use the correct database name in the DB_DATABASE key.
https://laravel.com/docs/master/database#configuration
I have the correct host url inside .env ($ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_container_ID>
), replacing it with my mysql-docker container name led to the same issue.
What do I need to edit, in order to run a migration correctly ?
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:ZOKQ6iEjuvyp0yrUq1C14VMHNw4Z/emBbrGAHD/DsW4=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=172.19.0.4 //or "vs-webpage-mysql-1"
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- meilisearch
- selenium
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sailmeilisearch:/data.ms'
networks:
- sail
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"]
retries: 3
timeout: 5s
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
sailmeilisearch:
driver: local
For anyone who encounters the same issue:
I had to replace the database name of "laravel" to "mysql" !
However, it works now!
DB_CONNECTION=mysql
DB_HOST=vs-webpage-mysql-1
DB_PORT=3306
DB_DATABASE=mysql
DB_USERNAME=root
DB_PASSWORD=

How to run queue work on Laravel with docker?

I create an docker setup on my Laravel project using sail.
În my project I use jobs and I want when I run docker-compose up to start command php artisan queue:work. In my docker-compose.yml I defined an service php-queue like this:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
php-queue:
restart: always
image: sail-8.0/app:latest
command: 'php artisan queue:work'
volumes:
- '.:/var/www/html'
mysql:
image: 'mysql:5.7'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
when I run docker-conpose up in docker desktop it shows this error:
error: failed switching to "php": unable to find user php: no matching entries in passwd file
what I missed?
change command: 'php artisan queue:work' to command: ['/bin/sh', '-c', 'php artisan queue:work' thats should work
Also this could help you :)
https://laravel.com/docs/8.x/sail#executing-sail-commands
Running Artisan commands locally...
php artisan queue:work
Running Artisan commands within Laravel Sail...
sail artisan queue:work

Web and CLI mysql won't connect at the same time

I have setup my Laravel application using Laravel Sail (Docker based). Everything's working fine except the MySQL. MySQL server is behaving differently for web (local.mysite.com:8080) and for CLI (ex: php artisan migrate).
Configruation (1)
If I use the following configuration in my .env file,
...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307
the web works, not the CLI. When I run php artisan migrate command I get SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Configuration (2)
And if I use the following configuration,
...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=local.mysite.com
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307
The CLI works fine, not the web. I get a Connection Refured error by MySQL on web.
I've been banging my head with different tries, no luck...
What am I doing wrong?
Here's my docker-compose.yml for reference (phpmyadmin is working real good btw):
# For more information: https://laravel.com/docs/sail
version: '3'
services:
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8081:80'
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- sail
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
extra_hosts:
- '${APP_DOMAIN}:127.0.0.1'
hostname: '${APP_DOMAIN}'
domainname: '${APP_DOMAIN}'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
I realized my mistake.
I should be using sail artisan migrate instead of php artisan migrate as clearly explained in the official Laravel Sail documentation.
The correct configuration therefore is:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307

Laravel sail problem with migrations, the console does not return anything

I have a problem with laravel sail. My problem is I can't migrate models. I used the command vendor/bin/sail artisan migrate but the console gives me nothing, no error, It also did not create any tables in the database. When I went to the project website and tried to create a new user, I could click the migration button and everything worked. sail node --version or similar commands also do not return anything
# For more information: https://laravel.com/docs/sail
version: '3'
services:
web:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '80:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
platform: linux/amd64
container_name: clinic_db
restart: unless-stopped
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
phpmyadmin:
image: 'phpmyadmin:latest'
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
Works on macbook pro m1. Can anyone fix this error?
I ran into this in my project. I had to add this to my .env
APP_SERVICE=laravel
where "laravel" was the the name of the container for the app. so in your case it would be
APP_SERVICE=web
My particular project that had the issue was an old laravel 5.7 project that we recently updated to laravel 8.x. I didn't run into this issue on newer installs. Maybe more recent versions of laravel do not need this extra .env variable?

Why am I getting SQLSTATE[HY000] [2002] Connection refused from Laravel on Docker?

This is my docker-compose:
version: '3'
services:
web:
build: .
image: citadel/php7.3
ports:
- "80"
volumes:
- ./src:/home/app/src:cached
container_name: 'studentlaptops'
restart: unless-stopped
tty: true
environment:
- XDEBUG_CONFIG='remote_host=<my-host-name>'
- VIRTUAL_HOST=studentlaptops.docker
#MySQL Service
db:
image: mysql:8.0.1
container_name: sl-db
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: studentlaptops
MYSQL_ROOT_PASSWORD: <password>
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
#Volumes
volumes:
dbdata:
driver: local
When I run docker-compose, it comes up fine. When I connect via a gui client, I can access the sl-db database in the container. When I run php artisan migrate, it migrates successfully.
However when I hit the application in the web browser SQLSTATE[2002]Connection refused...
My env db section is:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=studentlaptops
DB_USERNAME=root
DB_PASSWORD=<password>
In your case I would change
DB_HOST=127.0.0.1
to
DB_HOST=db
which is the name of your mysql in docker

Categories