Laravel PDOException SQLSTATE[HY000][1049]未知数据库“forge”

时间:2022-10-14 15:49:22

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 PDO database? and why the forge database 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

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

我发现这条线保护$table = 'users';在我的用户。php文件,我将它改为protected $table = 'user';因为我的数据库中的表是用户而不是用户

Update 2

I wrote this in my Route

这是我在路上写的。

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

and I added UsersController.php in my controllers folder

我和UsersController补充道。我的controller文件夹中的php

and inside UsersController.php I 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/

我将此url命名为http://localhost:8082/laravel/public/users/。

I am using Windows 7 with Laravel 4.2

我使用的是Windows 7和Laravel 4.2

Thanks in advance

谢谢提前

9 个解决方案

#1


8  

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

#2


5  

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

Hope your problem will get resolved.

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

#3


2  

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

听起来好像您有一个特定于环境的配置文件,可以覆盖默认的数据库设置。可能app / config /地方/ database.php。

#4


2  

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.

注:有一次我不小心有一个空间在我的数据库名称,如mydatabase mydatabase,phpmyadmin不会显示空间,但如果你运行它从mysql命令行接口,比如mysql - u the_user - p然后显示数据库,您将能够看到空间。

#5


1  

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.

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

#6


0  

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

遇到过这个问题好几次,注意我正在通过流浪者运行laravel。以下是对我有用的修正:

  • Try again several times (refresh page)
  • 再试几次(刷新页面)
  • Reload vagrant (vagrant reload)
  • 重载的流浪汉(流浪的重载)

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

您可以尝试重新加载您的服务器,而不是流浪(即MAMP)

#7


0  

OK, found solution.

好的,找到解决方案。

In the file database.php, by default, it comes the "mysql" part:

在数据库文件。php默认是“mysql”部分:

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

#8


0  

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

确保在laravel文件夹中没有重复的.evn文件。如果存在,删除它。只保留.evn文件。

#9


-3  

I had the same problem and this is what i resolved the error.

我有同样的问题,这就是我解决错误的方法。

BEFORE:

之前:

$conn= mysqli_connect($server_name, $mysql_Username, $mysql_Password, $db_name);

AFTER:
Just added "";

后:添加“”;

$conn= "mysqli_connect($server_name, $mysql_Username, $mysql_Password, $db_name)";

#1


8  

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

#2


5  

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

Hope your problem will get resolved.

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

#3


2  

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

听起来好像您有一个特定于环境的配置文件,可以覆盖默认的数据库设置。可能app / config /地方/ database.php。

#4


2  

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.

注:有一次我不小心有一个空间在我的数据库名称,如mydatabase mydatabase,phpmyadmin不会显示空间,但如果你运行它从mysql命令行接口,比如mysql - u the_user - p然后显示数据库,您将能够看到空间。

#5


1  

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.

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

#6


0  

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

遇到过这个问题好几次,注意我正在通过流浪者运行laravel。以下是对我有用的修正:

  • Try again several times (refresh page)
  • 再试几次(刷新页面)
  • Reload vagrant (vagrant reload)
  • 重载的流浪汉(流浪的重载)

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

您可以尝试重新加载您的服务器,而不是流浪(即MAMP)

#7


0  

OK, found solution.

好的,找到解决方案。

In the file database.php, by default, it comes the "mysql" part:

在数据库文件。php默认是“mysql”部分:

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

#8


0  

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

确保在laravel文件夹中没有重复的.evn文件。如果存在,删除它。只保留.evn文件。

#9


-3  

I had the same problem and this is what i resolved the error.

我有同样的问题,这就是我解决错误的方法。

BEFORE:

之前:

$conn= mysqli_connect($server_name, $mysql_Username, $mysql_Password, $db_name);

AFTER:
Just added "";

后:添加“”;

$conn= "mysqli_connect($server_name, $mysql_Username, $mysql_Password, $db_name)";