douzhong3887
douzhong3887
2014-06-09 11:57

Laravel PDOException SQLSTATE [HY000] [1049]未知数据库'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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

10条回答

  • doubeishuai6598 doubeishuai6598 7年前

    You have to clear the cache like that (because your old configuration is in you cache file) :

    php artisan cache:clear
    

    The pdo error comes from the fact Laravel use the pdo driver to connect to mysql

    点赞 评论 复制链接分享
  • donglu9872 donglu9872 2年前

    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=
    
    点赞 评论 复制链接分享
  • dousi6192 dousi6192 7年前

    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.

    点赞 评论 复制链接分享
  • dongshuan8722 dongshuan8722 4年前

    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'),
    
    点赞 评论 复制链接分享
  • dongye9820 dongye9820 2年前

    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)

    点赞 评论 复制链接分享
  • dppcyt6157 dppcyt6157 3年前

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

    点赞 评论 复制链接分享
  • du5591 du5591 6年前

    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)

    点赞 评论 复制链接分享
  • dongqie2028 dongqie2028 6年前
    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.

    点赞 评论 复制链接分享
  • dshdb64088 dshdb64088 7年前

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

    点赞 评论 复制链接分享
  • dpq59734 dpq59734 6年前

    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.

    点赞 评论 复制链接分享