Laravel SQLSTATE[HY000] [1049]未知数据库+ PHP Artisan迁移

qojgxg4l  于 2022-12-14  发布在  PHP
关注(0)|答案(3)|浏览(159)

我以前安装过Laravel,从来没有遇到过数据库未知的问题。我正在尝试在新项目上安装默认的Laravel迁移。
我已经在服务器上创建了数据库。
我正在使用MAMP Apache端口:80 MYSQL端口:3306
我的env设置如下所示:

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

当我尝试使用PHP工匠迁移;出现数据库未知错误

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))

如何解决此错误

2vuwiymt

2vuwiymt1#

步骤1:

在项目内激发此命令

php artisan optimize:clear

步骤2:

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

php artisan serve

然后连接数据库。

nhaq1z21

nhaq1z212#

来自@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文件,您将发现错误
或者如果您在linux中,您可以使用php artisan migrate -v。它将显示错误详细信息。
数据库是你创建的吗
检查您是否有bootstrap\cache\config.php文件。如果有,请删除它。
我没有发现任何pdoException错误,但是如果您更改了

`config->database.php`   file

这是我的config->databse.php尝试比较这些代码取自LaravelFramework7.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

可能是工匠会发生错误。现在这是我的工匠从LaravelFramework7.30.4和php 7.2看看它,如果你没有发现任何可疑的东西,没有改变你的工匠文件采取副本或备份。并尽快给我看你的错误日志。

#!/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);
6psbrbz9

6psbrbz93#

如果你在一个使用MAMP的MAC上,把这个添加到我的.env文件中对我很有效
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

相关问题