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

后端 未结 23 2138
醉酒成梦
醉酒成梦 2020-12-25 11:51

I am using Laravel to connect to MySQL database.

I got this exception:

PDOException
SQLSTATE[HY000] [1049] Unknown database \'forge\'
相关标签:
23条回答
  • 2020-12-25 12:32

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

    0 讨论(0)
  • 2020-12-25 12:32

    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

    0 讨论(0)
  • 2020-12-25 12:33

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

    0 讨论(0)
  • 2020-12-25 12:33

    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
    
    0 讨论(0)
  • 2020-12-25 12:34

    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'),
    
    0 讨论(0)
  • 2020-12-25 12:35
    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.

    0 讨论(0)
提交回复
热议问题