Laravel 5.1 Forge—SQLSTATE[HY000][1045]访问被拒绝的用户“@'localhost”(使用密码:NO)

时间:2022-09-19 21:17:00

Ive just built my new app in Laravel 5.1 and im trying to push it live. When i try to composer install i am getting this error.

我刚在Laravel 5.1上开发了我的新应用,我正在尝试将它实时推送。当我尝试作曲时,我得到了这个错误。

[PDOException]                                                                     
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)  

Script php artisan clear-compiled handling the post-install-cmd event returned with an error

[RuntimeException]  
Error Output:       

I am using a environment file on both local and production so i have no idea whats going on. Any ideas?

我正在使用本地和生产环境文件,所以我不知道发生了什么。什么好主意吗?

Its like my environment file is not getting picked up on forge.....

就好像我的环境文件在福奇公司没有被取走……

config/datasbase.php

配置/ datasbase.php

<?php

return [

/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch' => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => env('DB_CONNECTION', 'mysql'),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path('database.sqlite'),
        'prefix'   => '',
    ],

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],

    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
    ],

],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => [

    'cluster' => false,

    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],

],

];

5 个解决方案

#1


1  

The reason this was happening is because i was using the 'Install Repo' functionality that forge provides to install my full project. Now because there was no environment set on the server it was failing upon install.

之所以出现这种情况,是因为我使用了forge提供的“安装Repo”功能来安装我的整个项目。现在,由于服务器上没有环境设置,所以安装失败。

To get around it, i had to hardcode the production database credentials, install the repo then create the env file. Then once this was done remove the hardcoded credentials (not great i know).

为了解决这个问题,我必须硬编码生产数据库凭据,安装repo,然后创建env文件。然后,一旦完成此操作,删除硬编码的凭据(我知道不是很好)。

It seems setting the .env before the install doesn't work either. So you wouldn't run into this sort of issue if you had your repo installed on a server from the beginning of your build.

在安装之前设置.env似乎也不起作用。因此,如果从构建开始就将repo安装在服务器上,就不会遇到这种问题。

But my project is now live and running correctly. Thanks for the replies.

但是我的项目现在已经正常运行了。谢谢你的回复。

#2


1  

The reason this is happening is because forge is running the post install command on your project and your are probably accessing the database in one of your service providers to for may be a a variable you want to attach to your views, try commenting the code in the service provider, install the repository and then push the code up again it should work fine

这是发生的原因是建立运行后安装命令在您的项目和你的可能是访问数据库的一个服务提供者可能是一个变量你想连接到你的观点,试着将服务提供者中的代码注释,安装存储库,然后推动起来应该正常工作的代码

#3


0  

Make sure MySQL user has all accessing privileges for given ip while connecting the MySQL. For further reference please go through the below link.

确保MySQL用户在连接MySQL时拥有所有访问给定ip的权限。如需进一步参考,请浏览以下连结。

http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html

http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html

#4


0  

It's hard to say anything for sure without seeing the .env file, but it looks like it's picking up the username from somewhere - the default is 'forge' and yet the error message has the username as an empty string - ''@localhost.

如果没有看到.env文件,就很难肯定地说什么,但它看起来像是从某个地方获取用户名——默认是“forge”,而错误消息的用户名是一个空字符串——“@localhost”。

Check your env file, make sure there are no spaces after the = and that the variable names match the ones in the database config. I've also had issues where I needed to wrap passwords and other values that had special characters in ''. Finally, remember that case matters - I've seen problems where everything worked locally and failed on the dev server because someone had changed the case of a variable. My mac didn't care, but the ubuntu server did.

检查您的env文件,确保=后面没有空格,并且变量名与数据库配置中的名称匹配。我也遇到过需要封装密码和其他具有特殊字符的值的问题。最后,请记住这个案例很重要——我在开发服务器上看到了所有东西都在本地工作并且失败的问题,因为有人更改了变量的情况。我的mac电脑不在乎,但ubuntu服务器在乎。

#5


0  

try this

试试这个

1

1

in app/database.php

Replace localhost with 127.0.0.1

'host'=> env('DB_HOST', 'localhost') -->'host' => env('DB_HOST', '127.0.0.1')

Also, in .env

DB_HOST=localhost --> DB_HOST=127.0.0.1

2

2

Try specify environment

php artisan migrate --env=local

3

3

Check to see if the MySQL is running by run

mysqladmin -u homestead -p status Enter password: secret

I got

Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012

Which mean it's running.

4

4

Check MySQL UNIX Socket 

5 make sure that your .env file looks something like this:

确保你的。env文件看起来是这样的:

APP_ENV=local
APP_DEBUG=true
APP_KEY=YOURAPPKEY

DB_HOST=localhost
DB_DATABASE=YOURDBNAME
DB_USERNAME=YOURPHPMYADMINUSERNAME
DB_PASSWORD=YOURPHPMYADMINPASS

6

6

try changing localhost into 127.0.0.1 in .env DB_HOST or try add your mysql port 

#1


1  

The reason this was happening is because i was using the 'Install Repo' functionality that forge provides to install my full project. Now because there was no environment set on the server it was failing upon install.

之所以出现这种情况,是因为我使用了forge提供的“安装Repo”功能来安装我的整个项目。现在,由于服务器上没有环境设置,所以安装失败。

To get around it, i had to hardcode the production database credentials, install the repo then create the env file. Then once this was done remove the hardcoded credentials (not great i know).

为了解决这个问题,我必须硬编码生产数据库凭据,安装repo,然后创建env文件。然后,一旦完成此操作,删除硬编码的凭据(我知道不是很好)。

It seems setting the .env before the install doesn't work either. So you wouldn't run into this sort of issue if you had your repo installed on a server from the beginning of your build.

在安装之前设置.env似乎也不起作用。因此,如果从构建开始就将repo安装在服务器上,就不会遇到这种问题。

But my project is now live and running correctly. Thanks for the replies.

但是我的项目现在已经正常运行了。谢谢你的回复。

#2


1  

The reason this is happening is because forge is running the post install command on your project and your are probably accessing the database in one of your service providers to for may be a a variable you want to attach to your views, try commenting the code in the service provider, install the repository and then push the code up again it should work fine

这是发生的原因是建立运行后安装命令在您的项目和你的可能是访问数据库的一个服务提供者可能是一个变量你想连接到你的观点,试着将服务提供者中的代码注释,安装存储库,然后推动起来应该正常工作的代码

#3


0  

Make sure MySQL user has all accessing privileges for given ip while connecting the MySQL. For further reference please go through the below link.

确保MySQL用户在连接MySQL时拥有所有访问给定ip的权限。如需进一步参考,请浏览以下连结。

http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html

http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html

#4


0  

It's hard to say anything for sure without seeing the .env file, but it looks like it's picking up the username from somewhere - the default is 'forge' and yet the error message has the username as an empty string - ''@localhost.

如果没有看到.env文件,就很难肯定地说什么,但它看起来像是从某个地方获取用户名——默认是“forge”,而错误消息的用户名是一个空字符串——“@localhost”。

Check your env file, make sure there are no spaces after the = and that the variable names match the ones in the database config. I've also had issues where I needed to wrap passwords and other values that had special characters in ''. Finally, remember that case matters - I've seen problems where everything worked locally and failed on the dev server because someone had changed the case of a variable. My mac didn't care, but the ubuntu server did.

检查您的env文件,确保=后面没有空格,并且变量名与数据库配置中的名称匹配。我也遇到过需要封装密码和其他具有特殊字符的值的问题。最后,请记住这个案例很重要——我在开发服务器上看到了所有东西都在本地工作并且失败的问题,因为有人更改了变量的情况。我的mac电脑不在乎,但ubuntu服务器在乎。

#5


0  

try this

试试这个

1

1

in app/database.php

Replace localhost with 127.0.0.1

'host'=> env('DB_HOST', 'localhost') -->'host' => env('DB_HOST', '127.0.0.1')

Also, in .env

DB_HOST=localhost --> DB_HOST=127.0.0.1

2

2

Try specify environment

php artisan migrate --env=local

3

3

Check to see if the MySQL is running by run

mysqladmin -u homestead -p status Enter password: secret

I got

Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012

Which mean it's running.

4

4

Check MySQL UNIX Socket 

5 make sure that your .env file looks something like this:

确保你的。env文件看起来是这样的:

APP_ENV=local
APP_DEBUG=true
APP_KEY=YOURAPPKEY

DB_HOST=localhost
DB_DATABASE=YOURDBNAME
DB_USERNAME=YOURPHPMYADMINUSERNAME
DB_PASSWORD=YOURPHPMYADMINPASS

6

6

try changing localhost into 127.0.0.1 in .env DB_HOST or try add your mysql port