我之前设置过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
IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
如何解决这个错误
第一步:
在项目中启动此命令
php artisan optimize:clear
步骤2:
然后在本地启动项目:
php artisan serve
数据库已连接。
@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
。它将显示错误的详细信息。
那个数据库是你创建的吗?
检查是否有文件bootstrapcacheconfig.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框架7.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(IlluminateContractsConsoleKernel::class);
$status = $kernel->handle(
$input = new SymfonyComponentConsoleInputArgvInput,
new SymfonyComponentConsoleOutputConsoleOutput
);
/*
|--------------------------------------------------------------------------
| 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);
如果您在MAC上使用MAMP,将此添加到我的.env
文件中对我有效
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock