Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

laravel

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

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

this is my database.php file :

'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 question and this question but none of them was not helpful.

like image 579
Saeed Vaziry Avatar asked Nov 13 '15 09:11

Saeed Vaziry


4 Answers

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

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

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

like image 74
Andrius Avatar answered Nov 09 '22 20:11

Andrius


Please run this command:

php artisan config:cache
like image 31
Mehadi Hassan Avatar answered Nov 09 '22 19:11

Mehadi Hassan


php artisan cache:clear

AND

restart your server

I had to kill my server and re-run php artisan serve for 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.

like image 6
Michaela Burns Avatar answered Nov 09 '22 21:11

Michaela Burns


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

  1. .env file in the project folder
  2. .database.php file inside config folder
like image 4
Sulthan Allaudeen Avatar answered Nov 09 '22 21:11

Sulthan Allaudeen