Laravel SQLSTATE[HY000] [1049] 未知数据库“previous_db_name”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33689376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:41:12  来源:igfitidea点击:

Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'

phpmysqllaravel

提问by Saeed Vaziry

I have this error when i use php artisan migratein my Laravel project.

php artisan migrate在 Laravel 项目中使用时出现此错误。

[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'

this is my database.phpfile :

这是我的database.php文件:

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'current_db_name'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
],

I saw this error in this questionand this questionbut none of them was not helpful.

我在这个问题这个问题中看到了这个错误,但没有一个没有帮助。

回答by Andrius

It clearly mentions that there's no such database named previous_db_name.

它清楚地提到没有名为previous_db_name 的数据库。

From what it seems the database.phpfile is not where the variable is from.

从看起来database.php文件不是变量的来源。

Check the .envfile in your Laravel installation folder to see if that's the database name that you have wrongly specified.

检查.ENV文件在你的Laravel安装文件夹,看看是否是数据库的名称,您已经错误地指定。

回答by Michaela Burns

php artisan cache:clear

php artisan cache:clear

AND

restart your server

重启你的服务器

I had to kill my server and re-run php artisan servefor my project to register the change of my DB_DATABASE name. I kept getting the error even after clearing the cache because I didn't realize this.

我不得不php artisan serve关闭我的服务器并重新运行我的项目以注册我的 DB_DATABASE 名称的更改。即使在清除缓存后,我仍然收到错误消息,因为我没有意识到这一点。

回答by Sulthan Allaudeen

In Laravel 5.x You should define the DB Details in two files

在 Laravel 5.x 中,您应该在两个文件中定义 DB 详细信息

  1. .envfile in the project folder
  2. .database.phpfile inside configfolder
  1. .env项目文件夹中的文件
  2. .database.php文件config夹内的文件

回答by Scamp Shaw

If you are using PHP artisan serve just restart it. (just in case someone comes searching as I did).

如果您使用的是 PHP artisan serve,只需重新启动它。(以防万一有人像我一样来搜索)。

回答by Farshad

set your database name in .env file too.

在 .env 文件中设置你的数据库名称。

回答by androCoder-BD

I know, it is super late but for people like me, new in laravel and following tutorial from artisansweb. Note that, migrate will not create your database. Rather, it will just create all the tables. Assuming, you have set up your .env file. Now the important part is create the database and users (if you decided to go with custom users) manually. Then, do the php artisan migrate command

我知道,这太晚了,但对于像我这样的人来说,laravel 中的新手并遵循 artisansweb 的教程。请注意,迁移不会创建您的数据库。相反,它只会创建所有表。假设您已经设置了 .env 文件。现在重要的部分是手动创建数据库和用户(如果您决定使用自定义用户)。然后,执行php artisan migrate 命令

hope that helps.

希望有帮助。