首页 > 解决方案 > 迁移期间 Laravel 未知数据库错误

问题描述

我无法在 Homestead/Vagrant 框中运行 Laravel 迁移。这样的问题还有很多,但似乎没有一个适合我的答案。

我建立了一个博客网站,它在 VirtualBox 中的路径是/home/vagrant/code/blog. /home/vagrant/code/blog/database/database.sqlite然后我按照文档中的说明创建了一个代表 SQLite 数据库的空文件。此外,我编辑了/home/vagrant/code/blog/.env文件以包含“正确”的数据库信息:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=/home/vagrant/code/blog/database/database.sqlite
DB_USERNAME=homestead
DB_PASSWORD=secret

然后我使用这里php artisan make:migration create_users_table解释的方法创建了一个迁移。但是,当我尝试使用此迁移时,出现以下错误:php artisan migrate,

vagrant@homestead:~/code/blog$ php artisan migrate

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [1049] Unknown database '/home/vagrant/code/blog/database/database.sqlite' (SQL: select * from information_schema.tables where table_schema = /home/vagrant/code/blog/database/database.sqlite and table_name = migrations and table_type = 'BASE TABLE')

  at /home/vagrant/code/blog/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1049] Unknown database '/home/vagrant/code/blog/database/database.sqlite'")
      /home/vagrant/code/blog/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=/home/vagrant/code/blog/database/database.sqlite", "homestead", "secret", [])
      /home/vagrant/code/blog/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

所以它说即使我输入了它的绝对路径它也找不到数据库,这怎么可能?我知道路径必须正确,因为当我运行ls /home/vagrant/code/blog/database/database.sqlite它时它被列为现有文件..

我怎样才能解决这个问题?

标签: phpmysqldatabaselaravelmigration

解决方案


改变这个:

DB_CONNECTION=mysql

有了这个:

DB_CONNECTION=SQLite

在你的 .env 文件中


推荐阅读