Description:
I am currently setting up a Laravel project using Docker and Laravel Sail. I am facing issues connecting to the MySQL service from the Laravel application. The error message indicates that the connection is being refused.
Environment:
- Laravel version: 10.10
- Docker version: v27.0.3
- MySQL version: 8.0.32
- Operating System: Windows 11 WSL version: 2.2.4.0 (Windows Subsystem for Linux 2)
Problem:
When I attempt to run the Laravel migrations or connect to the MySQL service from the Laravel container, I receive a “Connection refused” error.
Error Logs:
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'laravel' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)
Steps to Reproduce:
- Set up Laravel Sail with the following
docker-compose.yml
:
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:
- '${APP_PORT:-80}: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}'
IGNITION_LOCAL_SITES_PATH: '${PWD}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
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
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
- Environment configuration in
.env
and.env.testing
files:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=sail
DB_PASSWORD=newpassword
- Start the services:
./vendor/bin/sail up -d
- Attempt to run the migrations:
./vendor/bin/sail artisan migrate --env=testing
Expected Behavior:
Laravel should connect to the MySQL database and run the migrations successfully.
Actual Behavior:
The connection is refused, and the following error is returned:
SQLSTATE[HY000] [2002] Connection refused
Troubleshooting Steps Taken:
- Verified that MySQL is running:
./vendor/bin/sail exec mysql mysqladmin -u root -p status
Output indicates MySQL is running:
Uptime: 804 Threads: 2 Questions: 59 Slow queries: 0 Opens: 120 Flush tables: 3 Open tables: 39 Queries per second avg: 0.073
- Checked MySQL logs:
./vendor/bin/sail logs mysql
Output:
mysql-1 | [Entrypoint] MySQL Docker Image 8.0.32-1.2.11-server
mysql-1 | [Entrypoint] Starting MySQL 8.0.32-1.2.11-server
mysql-1 | 2024-07-25T20:31:03.744066Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql-1 | 2024-07-25T20:31:03.744807Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1
mysql-1 | 2024-07-25T20:31:03.750686Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql-1 | 2024-07-25T20:31:03.849883Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql-1 | 2024-07-25T20:31:03.967731Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql-1 | 2024-07-25T20:31:03.967761Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql-1 | 2024-07-25T20:31:03.981964Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
mysql-1 | 2024-07-25T20:31:03.982031Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32' socket: '/var/lib/mysql/mysql.sock' port: 0 MySQL Community Server - GPL.
- Verified MySQL is listening on port 3306:
./vendor/bin/sail exec mysql ss -tuln | grep 3306
Output confirms MySQL is listening on port 3306.
- Ensured correct MySQL user privileges:
./vendor/bin/sail exec mysql mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'sail'@'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
- Tried connecting to MySQL from the Laravel container:
./vendor/bin/sail exec laravel.test mysql -u sail -p -h mysql
Successful connection from the container.
- Verified Docker network configuration:
docker network inspect web2_sail
Output indicates the network is properly configured:
[
{
"Name": "web2_sail",
"Id": "d4b00fbf3c2778f704a2c3025e6f3db476acf6c4afe6b967a246bf4bbcccb91d",
"Created": "2024-07-25T20:31:03.142662932Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0ed0e32a6b8c725be03d78959399043140cf97fc995ca0e0d38e2acd2aa7fd8f": {
"Name": "web2-mysql-1",
"EndpointID": "d2b25fb88d43d80468d6830b4b4839f80c2475d6eefa683798bd4fb086313bf8",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"6541b649afb1482c08888c3b5590c04142979e1bf719598b8c52298a908bb04b": {
"Name": "web2-laravel.test-1",
"EndpointID": "0f655029a4258ab7fb7cfbee2d22535c25a844af30ba70045be234e92a85f669",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "sail",
"com.docker.compose.project": "web2",
"com.docker.compose.version": "2.28.1"
}
}
]
- Tested connectivity from Laravel container:
./vendor/bin/sail exec laravel.test telnet mysql 3306
Output:
Trying 172.18.0.2...
telnet: Unable to connect to remote host: Connection refused
./vendor/bin/sail exec laravel.test nc -zv mysql 3306
Output:
nc: connect to mysql (172.18.0.2) port 3306 (tcp) failed: Connection refused
- Verified MySQL configuration files:
./vendor/bin/sail exec mysql grep -r "bind-address" /etc/mysql
Output:
grep: /etc/mysql: No such file or directory
- Created the
testing
database manually:
./vendor/bin/sail exec mysql mysql -u sail -p
CREATE DATABASE IF NOT EXISTS testing;
Request:
I need assistance identifying why the Laravel application cannot connect to the MySQL service within the Docker setup. Any insights or recommendations to resolve this issue would be greatly appreciated.