lavaral 5 ERROR{ (SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)}

后端 未结 2 1829
暖寄归人
暖寄归人 2020-12-22 13:13
  \'default\' => env(\'DB_CONNECTION\', \'mysql\'),


    \'connections\' => [

    \'sqlite\' => [
        \'driver\'   => \'sqlite\',
        \'databas         


        
相关标签:
2条回答
  • 2020-12-22 13:31

    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.

    See the docs here.

    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,
        ],
    

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

    So why use env at all?

    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. You do not wish to send all 100 users the same credentials. Also they might use different database, cache server, etc which means that they will have different configurations. So every user has to maintain those 8-10 configuration files by hand.
    2. You do not wish to send these configuration files to version control. Because if you do, whole world will know your API secrets and possibly will take advantage of that (just like password). Also if you look at laravel conf files, you will notice that there are other information such as timezone, debug property, etc that are also in conf files, and you do want to version-control them. So how do you version-control such configuration files and still hide your sensitive information.

    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.

    Sample contents of .env file

    APP_DEBUG=false
    APP_KEY=ABCDEFGH
    ...
    

    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. 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.
    0 讨论(0)
  • 2020-12-22 13:51

    Make sure you have to set up right server credentials into .env file on your Laravel project:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=myapp_db
    DB_USERNAME=root
    DB_PASSWORD=pass
    

    Try to clean up artisan cache and restart the artisan,

    php artisan config:clear 
    restart php artisan
    
    0 讨论(0)
提交回复
热议问题