【发布时间】:2019-11-18 10:23:42
【问题描述】:
我无法在 Homestead/Vagrant 盒子中运行 Laravel 迁移。这样的问题还有很多,但似乎没有一个适合我的答案。
我建立了一个博客网站,它在 VirtualBox 中的路径是/home/vagrant/code/blog。然后我按照docs 中的说明在/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 创建了一个迁移,正如here 所解释的那样。但是当我尝试使用 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 时,它被列为现有文件..
我该如何解决这个问题?
【问题讨论】:
-
您必须将 DB_CONNECTION 变量从 mysql 更改为 SQLite
-
按照 Giacomo 的说法,你应该改变你的环境文件。如果您阅读异常消息,您的应用正在尝试连接到 MySQL 中名为“/home/vagrant/code/blog/database/database.sqlite”的数据库
-
但这会导致错误
InvalidArgumentException : Database [SQLite] not configured.
标签: php mysql database laravel migration