php Laravel PDOException SQLSTATE[HY000] [1049] 未知数据库“伪造”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24119818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:09:48  来源:igfitidea点击:

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

phpmysqllaravellaravel-4

提问by Anastasie Laurent

I am using Laravel to connect to MySQL database.

我正在使用 Laravel 连接到 MySQL 数据库。

I got this exception:

我得到了这个例外:

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

and this is my config.database.php

这是我的 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 PDOdatabase? and why the forgedatabase name? I have already changed it.

为什么错误是指PDO数据库?为什么是forge数据库名称?我已经改变了它。

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

我应该做些什么来告诉 Laravel 我正在使用 MySQL 数据库吗?

Update 1

更新 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 usernot users

protected $table = 'users';在我的 user.php 文件中找到了这一行 ,我已将其更改为 protected $table = 'user';因为我的数据库中的表user不是users

Update 2

更新 2

I wrote this in my Route

我在我的路线中写了这个

Route::resource('users', 'UsersController');

and I added UsersController.phpin my controllers folder

UsersController.php在我的控制器文件夹中添加了

and inside UsersController.phpI have this:

在里面UsersController.php我有这个:

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/

我把这个网址叫做 http://localhost:8082/laravel/public/users/

I am using Windows 7 with Laravel 4.2

我正在使用带有 Laravel 4.2 的 Windows 7

Thanks in advance

提前致谢

采纳答案by Freelancer

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

pdo 错误来自 Laravel 使用 pdo 驱动程序连接到 mysql 的事实

回答by user5320639

  1. First you have to Create your related Database.
  2. Then:php artisan cache:clear
  3. Now run php artisan migrate:install
  1. 首先,您必须创建相关的数据库。
  2. 然后:php artisan cache:clear
  3. 现在运行 php artisan migrate:install

Hope your problem will get resolved.

希望你的问题能得到解决。

回答by Joey Garcia

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

使用 phpMyAdmin(或您喜欢的任何东西),我刚刚创建了一个名为“forge”的数据库并重新运行该php artisan migrate命令,一切正常。

回答by Martin Bean

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

听起来你在某处有一个特定于环境的配置文件,它覆盖了默认的数据库设置。可能是app/config/local/database.php

回答by mohammad amin Sarbazi

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)

我遇到了同样的问题...如果您已经设置了数据库名称和用户名并在 .env 文件中正确传递,但它仍然无法正常工作,请在终端中运行打击代码:(这将清除以前应用程序留下的缓存)

php artisan cache:clear

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

然后php artisan serve再次运行该命令(如果您正在运行它,请停止并再次运行它)

回答by dud3

Note: Once it happened that I accidentally had a space before my database name such as mydatabaseinstead 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 -pthen show databases, you'll be able to see the space.

注意:一旦我不小心在我的数据库名称前面有一个空格,例如mydatabase而不是mydatabase,phpmyadmin 不会显示该空格,但是如果您从 mysql 的命令行界面运行它,例如mysql -u the_user -pthen show databases,您将能够看空间。

回答by davidkihara

  1. Stop the server then run php artisan cache:clear.
  2. Start the server and should work now
  1. 停止服务器然后运行php artisan cache:clear
  2. 启动服务器,现在应该可以工作了

回答by zakaria joy

  1. Stop the server then run php artisan cache:clear.
  2. Change .env file DB_PORT=3308 (3308 For me) mysql port
  1. 停止服务器然后运行 ​​php artisan cache:clear。
  2. 更改 .env 文件 DB_PORT=3308 (3308 对我来说) mysql 端口

回答by Mohamad Shabihi

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.

我做了所有这些但没有工作,我发现应该停止 php artisan serve(Ctrl + C) 并再次启动 php artisan serve。

回答by Altamish Ahmadi

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

这是我对问题中描述的问题的回应。

in cmd write:

在cmd中写:

php artisan cache:clear

then try to do this code in your terminal

然后尝试在您的终端中执行此代码

php artisan serve

note: this will start again the server

注意:这将再次启动服务器