SQLSTATE[HY000] [2002] Connection refused是指在使用Laravel 5.8.35时出现了连接被拒绝的错误。

3

使用 Mac OS X、MAMP 5.5 和 Laravel 5.8.35。

当我使用 'php artisan migrate' 命令时,出现以下错误信息:

Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = auth and table_name = migrations and table_type = 'BASE TABLE')。

位于 /Applications/MAMP/htdocs/auth/vendor/laravel/framework/src/Illuminate/Database/Connection.php 的第664行。

    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

文件 .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=auth
DB_USERNAME=etn
DB_PASSWORD=pass

同样的事情。
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=auth
DB_USERNAME=etn
DB_PASSWORD=pass

迁移文件
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

第二个迁移文件:
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

你确定你的数据库已经在运行吗?连接被拒绝意味着你的应用程序无法连接到数据库。 - azisuazusa
@SalunkeAkash已添加 - Daniele Mastroberti
@DanieleMastroberti 请检查您的xampp/MAMP是否正在运行。 - Prashant Deshmukh.....
@DanieleMastroberti 你可以使用MySQL CLI手动连接到数据库。 - azisuazusa
打开命令提示符,登录到您的根帐户并运行 SHOW VARIABLES WHERE Variable_name = 'port'; - SalunkeAkash
显示剩余5条评论
2个回答

2
尝试这些更改。
  DB_PORT=33060

或者
 DB_PORT=8889

在修改后停止并重新启动服务器。


谢谢,我也已经在config/database.php中更改了'port' => env('DB_PORT', '8889')。 - Daniele Mastroberti
嗨!这个工作了吗?为什么? - Valentino

1

其中一个原因是,服务器XAMPP未启动,请尝试检查您是否在本地主机上准确启动了服务器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接