且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

lavaral 5错误{(SQLSTATE [HY000] [1045]用户'root'@'localhost'的访问被拒绝(使用密码:是)}

更新时间:2022-04-21 21:56:13

默认情况下,laravel假设您要针对不同的环境使用不同的配置.例如.在测试环境中,您可能希望使用不同的用户名和密码,在生产环境中使用不同的用户名和密码.由于laravel有太多配置文件,因此迅速地管理所有这些文件成为噩梦.因此,laravel利用了PHP的环境变量.

By default laravel assumes that you will want to have different configurations for different environments. E.g. in a testing environment, you might wish to have a different username and password and in a production environment different. Since laravel has so many configuration files, it quickly becomes a nightmare to manage all those. Hence laravel makes use of PHP's environment variables.

请参阅此处的文档.

基本上说的是,如果您想使用laravel默认使用的环境"变量,则必须将所有配置都放置在已提及的env()方法中.

What is basically says is that if you wish to use the "environment" variables, which laravel uses by default, you have to place all your configurations in the env() method as already mentioned.

如果您不希望这样做,例如对于简单的项目,只需从代码中删除环境,就像这样.

If you do not wish to do this, e.g. for simple projects, simply remove the env from your code, like this.

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

请注意,您可以混合搭配.也就是说,您可以在env中拥有一些变量,也可以具有一些独立变量.

Note that you can mix and match. i.e you can have some of the variables in env and some stand-alone.

可以说您的应用程序中有100个测试人员都放置在不同的位置.在laravel中,您必须编写大约8-10个配置文件.另外,您还需要version-control这些文件.所以您手头有两个问题:

Lets say your application has 100 testers all placed in different locations. In laravel you have to code approximately 8-10 configuration files. Also you need to version-control those files. So you have two problems at hand:

  1. 您不希望向所有100个用户发送相同的凭据.同样,他们可能使用不同的数据库,缓存服务器等,这意味着它们将具有不同的配置.因此,每个用户都必须手动维护这8-10个配置文件.
  2. 您不希望将这些配置文件发送到版本控制.因为如果这样做,整个世界都会知道您的API机密,并且可能会利用它(就像密码一样).另外,如果您查看laravel conf文件,您会注意到conf文件中还包含其他信息,例如时区,调试属性等,并且您确实希望对它们进行版本控制.那么,如何对这些配置文件进行版本控制,同时仍然隐藏您的敏感信息.

答案是env变量. Laravel使用dotenv,其文档可在此处找到.基本上,这些是存在于键-值对中名为.env的文件中的变量.例如

The answer is env variables. Laravel uses dotenv whose documentation can be found here. Basically these are variables that live in one file called .env in a key-value pair. E.g.

.env文件的示例内容

APP_DEBUG=false
APP_KEY=ABCDEFGH
...

一旦定义了.env文件,就可以使用env('APP_DEBUG')这样的键来获取值.

Once you define your .env file as this, you can get the value using the key as such env('APP_DEBUG').

因此,这可以通过以下方式解决上述问题:

So this solves the above mentioned problem in following ways:

  1. 您将.env文件保留给自己.而且,您还声明了另一个名为.env.example的文件,该文件是原始文件的精确副本,只是它包含样本值而不是敏感值.然后,您将这个新的示例文件传递给所有人.他们将用自己的敏感信息替换示例数据.
  2. 由于您要对示例文件进行版本控制,因此可以对所有conf文件进行版本控制,因为它们不包含机密信息.秘密在.env文件中.所有这些conf文件包含的是类似这些env('APP_KEY')的值,并在运行时使用您的.env文件替换实际值.
  1. you keep the .env file to yourself. And you also declare another file called .env.example which is an exact replica of original file except the fact that it contains sample values, not your sensitive values. Then you pass this new example file to everyone. They will replace the sample data with their own sensitive information.
  2. Since you are version-controlling the example file, you can version control all your conf files because they don't contain the secret. The secret is in .env files. All those conf files contain is values like these env('APP_KEY') and the actual value is replaced at run time using your .env file.