Isu sambungan MySQL Laravel Sail Docker
P粉949190972
P粉949190972 2023-08-27 00:43:45
0
1
583
<p>Saya cuba mengakses pangkalan data melalui aplikasi Laravel yang digunakan dalam bekas Docker, tetapi saya mendapat mesej ralat berikut: </p> <pre class="brush:php;toolbar:false;">SQLSTATE[HY000] [2002] Sambungan ditolak PILIH * DARI `posts`</pre> <p>Terdapat jadual yang dipanggil siaran dalam pangkalan data yang saya cuba akses daripada localhost/siaran.</p> <p>/posts的Blade模板如下:</p> <pre class="brush:php;toolbar:false;">@extends('app') @section('kandungan') <h1>Halaman indeks catatan</h1> @forelse ($posts sebagai $post) <h2>{{ $post->tajuk }}</h2> @kosong <h2>Tiada siaran!</h2> @endforelse @endsection</pre> <p>负责/posts的web.php中的路由如下:</p> <pre class="brush:php;toolbar:false;">Route::resource('/posts', PostController::class)->only(['index', 'store']);< /pra> <p>PostController的index函数如下:</p> <pre class="brush:php;toolbar:false;">indeks fungsi awam() { $posts = Post::semua(); return view('posts.index', compact('posts')); }</pre> <p>我的.env文件如下:</p> <pre class="brush:php;toolbar:false;">APP_NAME=Laravel APP_ENV=tempatan APP_KEY=base64:1HEk/HaN8IL/wV8q4DCzpvmM/cYRDvGAIe+0DxS9ggo= APP_DEBUG=benar APP_URL=http://localhost LOG_CHANNEL=timbunan LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=nyahpepijat DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3307 DB_DATABASE=laravel_essentials_app DB_USERNAME=layar DB_PASSWORD=kata laluan BROADCAST_DRIVER=log CACHE_DRIVER=fail FILESYSTEM_DISK=tempatan QUEUE_CONNECTION=segerak SESSION_DRIVER=fail 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-timur-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;"># Untuk maklumat lanjut: https://laravel.com/docs/sail versi: '3' perkhidmatan: laravel.test: membina: konteks: ./vendor/laravel/sail/runtimes/8.1 dockerfile: Dockerfile args: WWWGROUP: '${WWWGROUP}' imej: layar-8.1/app extra_hosts: - 'host.docker.internal:host-gateway' pelabuhan: - '${APP_PORT:-80}:80' - '${VITE_PORT:-5173}:${VITE_PORT:-5173}' persekitaran: WWWUSER: '${WWWUSER}' LARAVEL_SAIL: 1 XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}' XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}' jilid: - '.:/var/www/html' rangkaian: - belayar bergantung kepada: - mysql - redis mysql: imej: 'mysql/mysql-server:8.0' pelabuhan: - '${FORWARD_DB_PORT:-3307}:3306' persekitaran: 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 jilid: - 'sail-mysql:/var/lib/mysql' - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh' rangkaian: - belayar pemeriksaan kesihatan: ujian: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"] cuba semula: 3 tamat masa: 5s redis: imej: 'redis:alpine' pelabuhan: - '${FORWARD_REDIS_PORT:-6379}:6379' jilid: - 'sail-redis:/data' rangkaian: - belayar pemeriksaan kesihatan: ujian: ["CMD", "redis-cli", "ping"] cuba semula: 3 tamat masa: 5s rangkaian: belayar: pemandu: jambatan jilid: sail-mysql: pemandu: tempatan sail-redis: pemandu: tempatan</pre> <p>我的操作系统是Windows 11,我在上面运行MySQL Workbench。这就是为什么我暴露3307端卫口而。 ;/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=layar DB_PASSWORD=kata laluan</pre></p>
P粉949190972
P粉949190972

membalas semua(1)
P粉547420474

Tetapkan nilai DB_HOST kepada mysql

dalam fail .env anda
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan