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

I am using Laravel to connect to MySQL database.

I 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

Thanks in advance

This question is related to php mysql laravel laravel-4

The answer is


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.


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.


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

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.


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)


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)


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


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


write

php artisan config:cache 

in your terminal and it will be fixed


Sounds like you have an environment-specific config file somewhere that overrides the default database settings. Possibly app/config/local/database.php.


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


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

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!


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 :)


first clear your cache using this command

php artisan cache:clear

Then restart the server using this command

php artisan serve

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


  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.


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.


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

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

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=

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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-4

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel Can I do Model->where('id', ARRAY) multiple where conditions? how to fix stream_socket_enable_crypto(): SSL operation failed with code 1 Rollback one specific migration in Laravel How can I resolve "Your requirements could not be resolved to an installable set of packages" error? Define the selected option with the old input in Laravel / Blade Redirect to external URL with return in laravel laravel the requested url was not found on this server