php Laravel 5 数据库问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28209536/
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 23:44:43  来源:igfitidea点击:

Laravel 5 database issue

phpmysqllaravel-5

提问by Ajay Kulkarni

I've installed laravel 5 successfully by using this command:

我已经使用以下命令成功安装了 laravel 5:

composer create-project laravel/laravel test-laravel-5-project dev-develop --prefer-dist

I even verified the version of installed laravel by using php artisan -Vcommand. The output was

我什至使用php artisan -V命令验证了已安装的 Laravel 的版本。输出是

Laravel Framework version 5.0-dev

Then I went to app/config/database.php, gave dafault db as mysql and gave configurations as

然后我去 app/config/database.php,给 dafault db 作为 mysql 并给配置作为

'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'Logintestfive'), 'username'=> env('DB_USERNAME', 'root'), 'password'=> env('DB_PASSWORD', 'manasa'), 'charset'=> 'utf-8', 'collation'=> 'utf-8_unicode_ci', prefix=> '', 'strict'=> false, ]

Then I went to localhost:8000/auth/registerand filled up the form and submitted the data and this is the error which I got:

然后我去localhost:8000/auth/register填写表格并提交数据,这是我得到的错误:

PDOException in Connector.php line 47: SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

But I've neither used laravel homesteadfor installng laravel 5 in my system nor used vagrantto set up laravel homestead. And it tells me like this:

但是我既没有用于laravel homestead在我的系统中安装 laravel 5,也没有用于vagrant设置 laravel homestead。它告诉我是这样的:

in Connector.php line 47
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 47
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 20
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10545
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10541
at ConnectionFactory->make(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false), 'mysql') in compiled.php line 10459

How can I fix those issues?

我该如何解决这些问题?

回答by Yordi Uytersprot

I got a similar problem. I started my server using php artisan servecommand, and edited the .envfile and refreshed the web-page which did not reflect any changes!

我遇到了类似的问题。我使用php artisan serve命令启动了我的服务器,并编辑了.env文件并刷新了没有反映任何更改的网页!

I fixed it by stopping the webserver, editing the .env.phpfile and restarting the webserver!

我通过停止网络服务器,编辑.env.php文件并重新启动网络服务器来修复它!

So I guess it's a caching-issue of the .env.phpfile.

所以我想这是.env.php文件的缓存问题。

回答by MasoodUrRehman

Laravel 5.1.10
As the answers already given above by Ganesh and Yordi, But i'm combining the both of them into single one.

Laravel 5.1.10
正如上面 Ganesh 和 Yordi 已经给出的答案,但我将它们合并为一个。

Issue:
When i try to create a database in phpmyAdmin and want to create schema/table through Laravel Schema builder service i got the following errors:

问题
当我尝试在 phpmyAdmin 中创建数据库并想通过 Laravel Schema builder 服务创建模式/表时,出现以下错误:

PDOException in Connector.php line 50: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
in Connector.php line 50
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0'))
in Connector.php line 50
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22 at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) .............

Connector.php 第 50 行中的 PDOException: SQLSTATE[HY000] [1045]
Connector.php 第 50 行
中 PDO->__construct('mysql:host=localhost ) 用户'homestead'@'localhost' 的访问被拒绝(使用密码:YES);dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0'))
在 Connector.php 第 50 行
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collat​​ion' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name ' => 'mysql'), array('0', '2', '0', false, '0')) 在 MySqlConnector.php 第 22 行在 MySqlConnector->connect(array('driver' => 'mysql ', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collat​​ion' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) ...... ......

Solution 1:
Then I realised that the issues were in the env file, I forgot to change the values in the env file. The .env file is located in the laravel 5 root directory open the file and change the following values with your ones

解决方案 1:
然后我意识到问题出在 env 文件中,我忘记更改 env 文件中的值。.env 文件位于 Laravel 5 根目录打开文件并更改以下值

DB_DATABASE = localhost,
DB_USERNAME = databse-username,
DB_PASSWORD = databse-user-password

DB_DATABASE = 本地主机,
DB_USERNAME = 数据库用户名,
DB_PASSWORD = 数据库用户密码

Solution 2:
Or change in the database.php file without env() as Ganesh syas:

解决方案 2:
或者在没有 env() 的情况下将 database.php 文件更改为 Ganesh syas:

    // instead of this block
    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'your database name'),
        'username'  => env('DB_USERNAME', 'your database username'),
        'password'  => env('DB_PASSWORD', 'your database password'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    // use this one

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'your database name',
        'username'  => 'your database username',
        'password'  => 'your database password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ]

The both solutions worked for me.

这两种解决方案都对我有用。

回答by ajtrichards

Laravel is using the variables contained in your .envfile.

Laravel 正在使用.env文件中包含的变量。

From: http://laravel.com/docs/master/configuration

来自:http: //laravel.com/docs/master/configuration

It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.

根据应用程序运行的环境使用不同的配置值通常很有帮助。例如,您可能希望在本地使用与在生产服务器上不同的缓存驱动程序。使用基于环境的配置很容易。

Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.

Laravel 使用了 Vance Lucas 的 DotEnv PHP 库。在全新的 Laravel 安装中,应用程序的根目录将包含一个 .env.example 文件。如果你通过 Composer 安装 Laravel,这个文件会自动重命名为 .env。否则,您应该手动重命名文件。

All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. You may use the env helper to retrieve values from these variables. In fact, if you review the Laravel configuration files, you will notice several of the options already using this helper!

当您的应用程序收到请求时,此文件中列出的所有变量都将加载到 $_ENV PHP 超级全局变量中。您可以使用 env 助手从这些变量中检索值。事实上,如果你查看 Laravel 配置文件,你会注意到一些已经在使用这个助手的选项!

Feel free to modify your environment variables as needed for your own local server, as well as your production environment. However, your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.

您可以根据自己的本地服务器以及生产环境的需要随意修改环境变量。但是,您的 .env 文件不应提交给应用程序的源代码管理,因为使用您的应用程序的每个开发人员/服务器可能需要不同的环境配置。

If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.

如果您正在与团队一起开发,您可能希望继续在您的应用程序中包含 .env.example 文件。通过在示例配置文件中放置占位符值,您团队中的其他开发人员可以清楚地看到运行您的应用程序需要哪些环境变量。

The default .env file looks something like:

默认的 .env 文件类似于:

APP_ENV=local
APP_DEBUG=true
APP_KEY=YOUR_KEY_HERE

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file

回答by Eason Hung

I got the same problem. After I use the command "php artisan config:clear" to clear the old configuration cache file, it can work. Maybe you can try it.

我遇到了同样的问题。在我使用命令“php artisan config:clear”清除旧的配置缓存文件后,它可以工作。也许你可以试试。

By the way, my English is not good, hope you can understand.

顺便说一句,我的英语不好,希望你能理解。

回答by Ganesh Jogam

Laravel is using .env file so change the .env file or change in the database.php file without env()

Laravel 正在使用 .env 文件,因此更改 .env 文件或在没有 env() 的情况下更改 database.php 文件

'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'your database name',
        'username'  => 'your database username',
        'password'  => 'your database password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ]

回答by Sunil

In Config->database.php

在 Config->database.php

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'DBName'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

In .env file :

在 .env 文件中:

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=DBName
DB_USERNAME=root
DB_PASSWORD=

Close the laravel application server and restart again for clearing the cache.

关闭laravel应用服务器,再次重启清除缓存。

  php artisan serve

Or you can do :

或者你可以这样做:

  php artisan config:clear

This will clear the cache in config files.

这将清除配置文件中的缓存。

Now it will definitely work! Cheers!

现在它肯定会起作用!干杯!

回答by nitinsridar

Laravel config/database.php uses the .env file for database name,username asnd password, Please change your .env file

Laravel config/database.php 使用 .env 文件作为数据库名称、用户名和密码,请更改您的 .env 文件

DB_HOST=localhost

DB_HOST=本地主机

DB_DATABASE=my_project

DB_DATABASE=my_project

DB_USERNAME=root

DB_USERNAME=root

DB_PASSWORD=myproject

DB_PASSWORD=我的项目

After changing this run the below command:

更改后运行以下命令:

composer dump-autoload

作曲家转储自动加载

then run your project

然后运行你的项目

php artisan serve

php工匠服务

回答by Barnabas Kecskes

I had a very similar issue and didn't know where to look. I cleared the cache, restarted server, etc., no joy. Until I found it...

我有一个非常相似的问题,不知道去哪里找。我清除了缓存,重新启动了服务器等,没有任何乐趣。直到我发现...

Before I reverted to a previous version on the Git repo, I used to have my session driver set to use the database! Silly thing, but it's not necessarily the database connection what you need to look at but instead where you are invoking the database to be used at the first place.

在我恢复到 Git 存储库上的先前版本之前,我曾经将会话驱动程序设置为使用数据库!愚蠢的事情,但不一定是您需要查看的数据库连接,而是首先调用要使用的数据库的位置。

I hope that somebody who'll end up on this page the way I did will find this helpful.

我希望以我的方式最终出现在此页面上的人会发现这很有帮助。

回答by vuhung3990

i have same issui, edit file .envon root folder and change database config DB_DATABASEDB_USERNAMEDB_PASSWORD

我有同样的问题,编辑.env根文件夹上的文件并更改数据库配置DB_DATABASEDB_USERNAMEDB_PASSWORD

回答by vuhung3990

Inside your laravel eg. c:/www/laravel, create a new directory called 'local' inside app/config.Copy database.php (app/config/database.php).This works for me

在你的 Laravel 里面,例如。c:/www/laravel,在 app/config.Copy database.php (app/config/database.php) 中创建一个名为“local”的新目录。这对我有用