【问题标题】:Laravel SQLSTATE[HY000] [1049] Unknown database + PHP artisan migrateLaravel SQLSTATE[HY000] [1049] 未知数据库 + PHP 工匠迁移
【发布时间】:2021-12-03 13:51:58
【问题描述】:

我之前设置过 Laravel,但从未遇到过数据库未知问题。我正在尝试在新项目上安装默认的 Laravel 迁移。

我已经在我的服务器上创建了数据库。

我正在使用 MAMP Apache 端口:80 MYSQL 端口:3306

我的环境设置如下所示:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=thevinesa
DB_USERNAME=root
DB_PASSWORD=******

当我尝试使用 PHP artisan 迁移时;我收到一个数据库未知错误

SQLSTATE[HY000] [1049] Unknown database 'thevinesa' (SQL: select * from information_schema.tables where table_schema = thevinesa and table_name = migrations and table_type = 'BASE TABLE') at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
        699▕         // If an exception occurs when attempting to run a query, we'll format the error
        700▕         // message to include the bindings with SQL, which will make this exception a
        701▕         // lot more helpful to the developer instead of just the database's errors.
        702▕         catch (Exception $e) {
        703▕             throw new QueryException(
        704▕                 $query, $this->prepareBindings($bindings), $e
        705▕             );
        706▕         }
        707▕     }
    
          +33 vendor frames 
      34  artisan:37
          Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

我该如何解决这个错误

【问题讨论】:

  • 您确定创建了thevinesa 数据库,即运行CREATE DATABASE thevinesa;
  • 永远不要共享您的密码,即使它只是一个本地主机数据库。您确定以区分大小写的方式正确输入了数据库吗?端口是否正确?用户名和密码是否正确?可以通过 CLI 连接到数据库吗?
  • 使用 sequal pro,我可以连接到数据库,但是通过 laravel,我无法
  • @ThandoHlophe 你能在这里查看我的答案和发布错误日志,以便我提供帮助。

标签: php laravel laravel-migrations


【解决方案1】:

@ThandoHlophe 的建议

*

我发现了问题,所以我的 ~bash 目录的 $PATH 不正确,我更新了 $PATH 并且能够解决问题

这是一个简单的错误,您仍然没有在mysql或phpmyadmin中创建名为thevinesa的数据库

DB_DATABASE=thevinesa

Laravel 尝试找出数据库名称 thevinesa 进行连接,但他们没有找到名称为 thevinesa 的数据库。尝试创建数据库。 然后迁移。

php artisan cache:clear
php artisan config:clear

php artisan migrate

与数据库相关的命令

  migrate:fresh        Drop all tables and re-run all migrations


migrate:install      Create the migration repository
  migrate:refresh      Reset and re-run all migrations
  migrate:reset        Rollback all database migrations
  migrate:rollback     Rollback the last database migration
  migrate:status       Show the status of each migratioin

或者

Laravel 无法连接到您的数据库。

转到Storage->logs->laravel.log文件你会发现你的错误

或者你可以使用php artisan migrate -v,如果你在linux。它将显示错误详细信息。

您是否创建了该数据库?

检查您是否有文件bootstrap\cache\config.php。如果是这样,请将其删除。

我没有发现任何 pdoException 错误,但如果你更改了

`config->database.php`   file

这是我的config->databse.php 尝试比较这些 此代码取自 Laravel Framework 7.30.4,可能有所不同,但您可以有一个基本的想法。

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

如果你想检查你的版本

php artisan -v

工匠可能会发生错误。现在这是我来自 Laravel Framework 7.30.4 和 php 7.2 的工匠,如果你没有发现任何可疑的东西,没有改变你的工匠文件,请复制 oe 备份。并尽快将您的错误日志显示给我。

#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
    $input = new Symfony\Component\Console\Input\ArgvInput,
    new Symfony\Component\Console\Output\ConsoleOutput
);

/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/

$kernel->terminate($input, $status);

exit($status);

【讨论】:

  • 如果您解决了您的问题,您是否可以批准我的回答以帮助他人
  • 数据库已经存在于phpmyadmin中,但返回的不是foudn
  • 当然去 storage->logs->laravel.log 并在这里发布你的错误日志,这样我就可以检查为什么 laravel 拒绝连接
  • 您是否更改了这样的核心文件 vendor/laravel/framework/src/Illuminate/Database/Connection.php:703 .. 如果您记得不幸的是您更改了核心,那么您可以告诉我们。
  • 我发现了问题,所以我的 ~bash 目录的 $PATH 不正确,我更新了 $PATH 并且能够解决问题
【解决方案2】:

第 1 步:

在你的项目中触发这个命令

php artisan optimize:clear

第 2 步:

如果在本地,则启动项目:

php artisan serve

然后连接数据库。

【讨论】:

  • 嗨 Ali,我可以运行 php artisan serve,但问题是当我尝试使用 PHP artisan migrate 时,我无法将迁移添加到数据库中
  • 你运行第一个命令了吗?在运行步骤 1 之前 ===> 然后重新开始 php artrisan serve ===> 之后你可以检查 php artisan migrate
  • 我按照您的步骤操作,但我仍然没有得到 SQLSTATE[HY000] [1049] 未知数据库 'thevinesa' 我已经构建了一个 Laravel 项目。
  • 我能看到你要迁移的代码吗
  • 我发现了问题,所以我的 ~bash 目录的 $PATH 不正确,我更新了 $PATH 并且能够解决问题
猜你喜欢
  • 2020-02-08
  • 2020-10-21
  • 2016-02-14
  • 2014-07-29
  • 2018-09-24
  • 2020-08-20
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
相关资源
最近更新 更多