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
Related
I have another MySQL 5.7 container. I wanted the MySQL 8 that comes with Laravel 9 to use port 3308 instead of 3306 so I have this .env:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:yiU0J8TWntz0c20bwsC1D/LqyZ6Hc+/BUczEiGYfPxM=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3308
DB_DATABASE=store
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=memcached
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6380
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello#example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700
and docker-compose.yml:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.2
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.2/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '8088:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
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
- mailhog
- selenium
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '3308: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
redis:
image: 'redis:alpine'
ports:
- '6380:6379'
volumes:
- 'sail-redis:/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:
- 'sail-meilisearch:/meili_data'
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'
extra_hosts:
- 'host.docker.internal:host-gateway'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local
sail-meilisearch:
driver: local
However, trying ./vendor/bin/sail php artisan migrate leads to MySQL error 2002:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
information_schema.tables where table_schema = store and table_name =
migrations and table_type = 'BASE TABLE')
What is the issue there and how could I solve it.
Update (1)
In .env I changed The DB_HOST and DB_PORT to:
DB_HOST=store-mysql-1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=sail
Where store-mysql-1 is the name of MySQL8 container of Laravel.
I got the following Error:
SQLSTATE[HY000] [1045] Access denied for user 'sail'#'172.20.0.7'
(using password: NO) (SQL: select * from information_schema.tables
where table_schema = store and table_name = migrations and table_type
= 'BASE TABLE')
The most important notice here, the IP 172.20.0.7 is not the IP of the container named `store-mysql-1 which I could determine it using the command:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' store-mysql-1 and it returns: 172.20.0.2
Update (2)
Using the command:
$ ./vendor/bin/sail mysql
returns the error:
ERROR 1045 (28000): Access denied for user 'sail'#'localhost' (using
password: NO)
Update (3) the solution
Using the following command I removed the images and then run sail up again, everything is solved with the same settings of of both .env and docker-compose.yml files except, to set a password in .env DB_PASSWORD=pwd123+ and uncomment #MYSQL_PASSWORD: '${DB_PASSWORD}' in docker-compose.yml:
./vendor/bin/sail down --rmi all -v
However, I saw this solution somewhere in this community but I have no idea why does it solve the issue and what is the root cause of it?
The .env must be using the docker composer container name, that is how the container knows where to point to (host), so DB_HOST must be mysql literally, because that is the one you want (MySQL 8).
And the DB_PORT must be equal to the internal port 3306. You are exposing 3308 to outside connections, but internally is still using 3306.
Then, whatever command you execute, it must be run inside the PHP container, I think sail already does this for you, but if you want to manually go inside the PHP container just do docker compose exec laravel.test bash
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
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=
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
I am new to docker, I am trying to connect the existing MySQL container to my Laravel application through docker-compose.
Where MySQL docker-compose.yml file is like
version: '3'
services:
web:
container_name: ${APP_NAME}_web
build:
context: ./docker/web
ports:
- "9000:80"
volumes:
- ./:/var/www/app
depends_on:
- db
db:
container_name: ${APP_NAME}_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
volumes:
dbdata:
driver: local
driver_opts:
type: none
device: /storage/docker/laravel_mysql_data
o: bind
This script is downloading the latest mysql version and making a new container. I just want to connect MYSQL container with my application
Where my existing MYSQL container is created with a specific IP using a bridge network and running on docker-machine.
I am wondering, can I define the existing MYSQL container configurations on the db context?
Try this docker-compose file:
version: '3.5'
services:
db:
container_name: myapp_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
networks:
- myapp-network
networks:
myapp-network:
driver: bridge
Situation has two cases:
1. If your laravel instance is in docker too - just add to .env file next rows:
DB_HOST=myapp_db
DB_PORT=3306
DB_DATABASE=laravel_docker_db
DB_USERNAME=root
DB_PASSWORD=root
2.If you don't use docker for laravel - just change variable DB_HOST=localhost
In .env file:
DB_HOST=db
DB_PORT=3306
DB_DATABASE=your database name
DB_USERNAME=root
DB_PASSWORD=root