I am using Laravel to connect to MySQL database.
I got this exception:
PDOException
SQLSTATE[HY000] [1049] Unknown database \'forge\'
Make sure you do not have a duplicated .env
file in the laravel folder. If it exists, delete it. Only keep the .env
file.
In my case the error was due to incorrect port number (the error is definitely due to incorrect credentials i.e. host/port/dbname/username/password
).
Solution:
.env
file;config/database.php
.Clear cache
php artisan config:cache
Run migration
php artisan migrate
Sounds like you have an environment-specific config file somewhere that overrides the default database settings. Possibly app/config/local/database.php.
If you've used Homestead, make sure the database name in your .env file below
DB_DATABASE=homestead
Is the same as the value in your Homestead.yaml file.
databases:
- homestead
OK, found solution.
In the file database.php, by default, it comes the "mysql" part:
'mysql' => [
'driver' => 'mysql',
'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', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
all you need to do is change the values :
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
by your database name (you must create one if you dont have any) and by that database username
like this
'database' => env('DB_DATABASE', 'MyDatabase'),
'username' => env('DB_USERNAME', 'MyUsername'),
php artisan cache:clear
php artisan migrate:install
Hope your problem will get resolved.