Laravel Sail Docker MySQL connection issue
P粉949190972
2023-08-27 00:43:45
<p>I'm trying to access a database through a Laravel application deployed in a Docker container, but I'm getting the following error message: </p>
<pre class="brush:php;toolbar:false;">SQLSTATE[HY000] [2002] Connection refused
SELECT * FROM `posts`</pre>
<p>There is a table called posts in the database that I am trying to access from localhost/posts.</p>
<p>/posts的Blade模板如下:</p>
<pre class="brush:php;toolbar:false;">@extends('app')
@section('content')
<h1>Posts index page</h1>
@forelse ($posts as $post)
<h2>{{ $post->title }}</h2>
@empty
<h2>No posts!</h2>
@endforelse
@endsection</pre>
<p>负责/posts的web.php中的路由如下:</p>
<pre class="brush:php;toolbar:false;">Route::resource('/posts', PostController::class)->only(['index', 'store']);</pre>
<p>PostController的index函数如下:</p>
<pre class="brush:php;toolbar:false;">public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}</pre>
<p>我的.env文件如下:</p>
<pre class="brush:php;toolbar:false;">APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:1HEk/HaN8IL/wV8q4DCzpvmM/cYRDvGAIe 0DxS9ggo=
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=3307
DB_DATABASE=laravel_essentials_app
DB_USERNAME=sail
DB_PASSWORD=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=6379
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}"</pre>
<p>我的docker-compose.yml文件如下:</p>
<pre class="brush:php;toolbar:false;"># 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'
- '${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
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3307}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "${DB_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:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local</pre>
<p>我的操作系统是Windows 11,我在上面运行MySQL Workbench。这就是为什么我暴露3307端口而不是3306端口的原因。</p>
<p>使用当前设置,我可以在wsl终端中无问题地运行php artisan migrate。</p>
<p>为了避免连接被拒绝错误,我可以做什么来从localhost/posts访问posts?</p>
<p><strong>解决方案</strong>
通过将.env中的DB_HOST更改为mysql,同时将DB_PORT更改为3306来解决问题。</p>
<p><strong>编辑后的.env文件</strong></p>
<pre class="brush:php;toolbar:false;">DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_essentials_app
DB_USERNAME=sail
DB_PASSWORD=password</pre></p>
Set the value of DB_HOST to
in your .env filemysql