【问题标题】:SQLSTATE[HY000] [2002] No such file or directory when i try to php artisan migrateSQLSTATE[HY000] [2002] 当我尝试 php artisan 迁移时没有这样的文件或目录
【发布时间】:2021-12-11 14:30:21
【问题描述】:

刚刚创建了一个新的 laravel 项目并尝试迁移表,我得到了这个:

SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL: select * from information_schema.tables where table_schema = sugarDaddy and table_name = migrations and table_type = 'BASE TABLE')

我尝试清除缓存、路由、配置,并将 DBHOST 从 127.0.0.1 更改为 localhost 并返回,正如我在几篇文章中看到的那样,但没有任何效果。我将把迁移和 ss 留在这里。

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->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');
    }
}

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sugarDaddy
DB_USERNAME=root
DB_PASSWORD=

【问题讨论】:

  • 运行php artisan migrate:fresh的输出是什么?问题中的那个还是其他?另外,我会尝试用sugardaddysugar_daddy 替换sugarDaddy,以确保大小写不是问题。
  • php artisan migrate:fresh 显示:SQLSTATE[HY000] [2002] Connection denied (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
  • 尝试将DB_HOST更改为localhost再试一次
  • 还是一样:SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL:select * from information_schema.tables where table_schema = sugardaddy and table_name = migrations and table_type = 'BASE TABLE')
  • 你确定当你有DB_HOST=127.0.0.1时你传递了正确的连接数据吗?我的意思是用户名、密码?

标签: php laravel database-migration mamp


【解决方案1】:

从截图中可以看出你的phpMyAdmin正在8889端口连接数据库,你的envDB_PORT=3306,改成DB_PORT=8889

评论区小结:

Dan 正在使用 MAMP,因此 mysql 连接的配置应包含(对于默认 MAMP 设置):

DB_HOST=localhost
DB_PORT=8889
DB_USERNAME=root
DB_PASSWORD=root

【讨论】:

  • 现在我得到了这个:SQLSTATE[HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:NO)(SQL:SHOW FULL TABLES WHERE table_type = 'BASE TABLE' )
  • 这是一个明显的错误,你有一些数据库密码,但你没有将它传递给配置。您可能必须在连接到 phpMyAdmin 时输入登录名和密码。将与 DB_USERNAMEDB_PASSWORD 相同的登录名和密码传递给 laravel .env 文件。
  • 但我没有设置
  • 我不必登录到 phpmyadming 只是写了链接并在没有登录或其他的情况下进入
  • 您写道您正在使用 MAMP,在快速谷歌之后我找到了that。试试DB_USERNAME=rootDB_PASSWORD=root
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多