【问题标题】:SQLSTATE[HY000] [2002] Connection refused Laravel 5.8.35SQLSTATE[HY000] [2002] 连接被拒绝 Laravel 5.8.35
【发布时间】:2020-03-26 21:06:09
【问题描述】:

使用 Mac OS X、MAMP 5.5 和 Laravel 5.8.35

当我使用“php artisan migrate”时,我收到以下错误消息:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection denied (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');
    }
}

【问题讨论】:

  • 你试过运行php artisan config:clear
  • 是的,都使用 php artisan config:cache 和 php artisan config:clear
  • 在迁移文件中分享您的查询
  • 您确定您的数据库已经在运行吗?连接被拒绝意味着您的应用无法连接到数据库
  • @SalunkeAkash 添加了

标签: database laravel mamp laravel-artisan


【解决方案1】:

试试这个改变

  DB_PORT=33060

或者

 DB_PORT=8889

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

【讨论】:

  • 谢谢,我也改成 config/database.php 'port' => env('DB_PORT', '8889'),
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 2017-09-20
    • 2020-12-04
    • 2020-02-27
    • 2016-11-03
    • 1970-01-01
    • 2021-07-10
    • 2021-12-17
    • 2019-08-21
    相关资源
    最近更新 更多