Anastasie Laurent
Anastasie Laurent

Reputation: 1179

Laravel PDOException SQLSTATE[HY000] [1049] Unknown database 'forge'

I am using Laravel to connect to MySQL database and got this exception:

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

and this is my config.database.php

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'laravel',
            'username'  => 'Anastasie',
            'password'  => 'A@Laurent',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

why is the error referring to PDO database? and why the forge database name? I have already changed it.

Should I do anything to tell Laravel that I am using MySQL database?

Update 1

I found this line `protected $table = 'users';` in my user.php file and I have changed it to `protected $table = 'user';` because the table in my database is `user` not `users`

Update 2

I wrote this in my Route
Route::resource('users', 'UsersController');

and I added UsersController.php in my controllers folder

and inside UsersController.php I have this:

class UsersController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $users = User::all();
        return View::make('users.index', compact('users'));
    }

and I call this url http://localhost:8082/laravel/public/users/

I am using Windows 7 with Laravel 4.2

Upvotes: 39

Views: 175527

Answers (30)

Akram Elhaddad
Akram Elhaddad

Reputation: 314

Remove files from this path bootstrap\cache\

Upvotes: 0

imane hp
imane hp

Reputation: 1

in my experience I had like this message error ,and i tray all of this solution but nothing to become good ,whay because my fault is i unstall Xampp and i read install again and i forget to create new Database with table that's way in the message say : "Unknown database" it's not exist simply ,now what i do is:

  • install Xampp (if you have problem in it ofcaurse)
  • i create now database + table short you don't see the error message again . NB ( I'm new in Programation ) < my conseil to be patient when you write your code >

Upvotes: 0

DareLekan
DareLekan

Reputation: 39

I got this error because MySQL could not access the database configured in the environment file.

To resolve, I simply created the database that MySQL and the app needs and all works fine.

Upvotes: 0

ganji
ganji

Reputation: 844

Every body have mentioned to php artisan cache:clear. Sometimes it doesn't work. But in my experience after clearing the cache simply delete the database and create it again.Then run this again

php artisan migrate

Surely it will work.

Upvotes: 0

user17214816
user17214816

Reputation: 11

you need just create database or import your data by localhost phpMyAdmin and don't forget the name should be same named

Upvotes: 1

Usman Khan
Usman Khan

Reputation: 1

php artisan config:cache that command fix my 1049 database not found.

Upvotes: 0

hamid
hamid

Reputation: 79

In the fourth solution this question was answered but I prefer to answer again to explain this differently . I make my database in phpmyadmin that is http://localhost/phpmyadmin and the problem was solved .this is my code :

<?php

$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "myDbn2";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "Database created successfully<br>";


$sql = 'create table if not exists fruits (id integer  , name text , color text 
, price float) ;';

$conn->exec($sql);
echo 'the table created successfully </br>';


$sql2 = 'insert into fruits values (1 , "apple", "red" , 23.2);';
$conn->exec($sql2);

echo 'insert was successfully';
} catch (PDOException $e) {
echo $e->getMessage();
}

$conn = null;


?>

The code make a database then make a table and insert a record in it . if you don't make your data base in phpmyadmin site you get the exception also you don't need to use sql statement CREATE DATABASE name ; and it works without it .

Upvotes: 0

helper
helper

Reputation: 11

Check your port in WAMP (it places on top of the phpMyAdmin) match with DB_port in the .env file.

My DB_port in .env is 3306. My server port in WAMP (phpMyAdmin) is 3308 - you should match them.

Upvotes: 1

Abdullah Yousufzai
Abdullah Yousufzai

Reputation: 1

I have the same problem, I see the my databases list the name of my database which doesn’t exist.

I create the database at the same name manually then I run this command it works for me.

  1. create database in phpmyadmin
  2. php run artisan

Upvotes: -1

salma muhamad
salma muhamad

Reputation: 61

write

php artisan config:cache 

in your terminal and it will be fixed

Upvotes: 6

Vivek Tiwari
Vivek Tiwari

Reputation: 11

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:

  1. right click on WAMP tray;
  2. click (drag your cursor) on MySQL;
  3. see the port number used by MySQL;
  4. add same in your Laravel configuration:
    • .env file;
    • config/database.php.

Clear cache php artisan config:cache

Run migration php artisan migrate

Upvotes: 1

CodeToLife
CodeToLife

Reputation: 4141

My APP_NAME variables in .env.example and .env and app.php were with space e.g. The App . Surounding that by ' and php artisan cache:clear and setting new generated app key to APP_KEY variable through env files and relaunching the server by php artisan serve solved this issue

Upvotes: 0

Mosaib
Mosaib

Reputation: 99

clear your Cache php artisan cache:clear and then restart your server php artisan serve 127.0.0.1:8000

Upvotes: 0

suraj mishra
suraj mishra

Reputation: 21

first clear your cache using this command

php artisan cache:clear

Then restart the server using this command

php artisan serve

Upvotes: 1

حصه
حصه

Reputation: 9

I had this problem for several days, it turns out if I created the db inside phpMyAdmin it wouldn't appear to Laravel, so, I created the db through MySqlWorkbench, and it worked :)

Upvotes: 0

Altamish Ahmadi
Altamish Ahmadi

Reputation: 1

Here is my response to the problem described in the question.

in cmd write:

php artisan cache:clear

then try to do this code in your terminal

php artisan serve

note: this will start again the server

Upvotes: 0

Mohamad Shabihi
Mohamad Shabihi

Reputation: 130

I did all of them but didn't work, I find out should stop php artisan serve(Ctrl + C) and start php artisan serve again.

Upvotes: 3

Chester
Chester

Reputation: 79

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

Upvotes: 1

zakaria joy
zakaria joy

Reputation: 49

  1. Stop the server then run php artisan cache:clear.
  2. Change .env file DB_PORT=3308 (3308 For me) mysql port

Upvotes: 3

Robert White
Robert White

Reputation: 53

In my particular case, I finally realised that my phpMyAdmin was using port 3308 while Laravel was attempting to connect through 3306. so my advice would be to ensure you have the correct connection string!

Upvotes: 0

davidkihara
davidkihara

Reputation: 533

  1. Stop the server then run php artisan cache:clear.
  2. Start the server and should work now

Upvotes: 3

St&#233;phane Ripa
St&#233;phane Ripa

Reputation: 11

You need to modify the name of the DB in the file .env (and if need in .env.example) I solved my problem with this little correction.

Upvotes: 0

Izaz Khan
Izaz Khan

Reputation: 1

sometimes its because DB_CONNECTION=mysql and you want to use SQLite database. A solution to that is to make DB_CONNECTION=sqlite. hope it helps

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=C:\xampp\htdocs\jbtibl\database\database.sqlite
DB_USERNAME=root
DB_PASSWORD=

Upvotes: -3

Farhad
Farhad

Reputation: 11

Make sure you do not have a duplicated .env file in the laravel folder. If it exists, delete it. Only keep the .env file.

Upvotes: 0

Joey Garcia
Joey Garcia

Reputation: 313

Using phpMyAdmin (or whatever you prefer), I just created a database called "forge" and re-ran the php artisan migrate command and it all worked.

Upvotes: 10

mmd amin
mmd amin

Reputation: 887

I had the same problem... If you have set your DB name and username and pass correctly in .env file and its still not working run the blow code in terminal:(this will clean the caches that left from previous apps)

php artisan cache:clear

and then run the command php artisan serve again (if you are running it stop and run it again)

Upvotes: 3

Mistura
Mistura

Reputation: 1

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'),

Upvotes: 0

user5320639
user5320639

Reputation: 151

  1. First you have to Create your related Database.
  2. Then:php artisan cache:clear
  3. Now run php artisan migrate:install

Hope your problem will get resolved.

Upvotes: 15

dud3
dud3

Reputation: 419

Note: Once it happened that I accidentally had a space before my database name such as mydatabase instead of mydatabase, phpmyadmin won't show the space, but if you run it from the command line interface of mysql, such as mysql -u the_user -p then show databases, you'll be able to see the space.

Upvotes: 4

michaeltintiuc
michaeltintiuc

Reputation: 671

Encountered this issue quite a few times, note that I'm running laravel via Vagrant. So here are the fixes that work for me:

  • Try again several times (refresh page)
  • Reload vagrant (vagrant reload)

You may try reloading your server instead of vagrant (ie MAMP)

Upvotes: 0

Related Questions