Ubuntu

PDOException (1045) SQLSTATEH是000H是000HY000104510451045Laravel 4.2 MySQL 5.7

  • February 6, 2017

感謝您閱讀我的問題,自從在 ServerFault、Stackoverflow 和 AskUbuntu 中閱讀問題一周以來,我一直在解決這個錯誤,但無法找出問題所在。

我有

  • Lubuntu 16.04(ubuntu 發行版)
  • Nginx
  • Laravel 4.2.17
  • MySQL 5.7

我有一個從 GitHub 複製的預先存在的項目,一切進展順利,但是當我轉到一個必須連接到數據庫的頁面時(比如嘗試登錄),我收到了這個錯誤:

PDOException (1045) 幫助

SQLSTATE$$ HY000 $$ $$ 1045 $$使用者 ‘homestead’@’localhost’ 的訪問被拒絕(使用密碼:YES)

錯誤的圖形描述

涉及的文件

我在項目中有一個 .env 文件,我將其命名為**.env.local.php**這是文件:

<?php
/**
* Variables de entorno .env.php
* Si se requiere cargar otros valores para un entorno diferente al de produccion (local, development, etc.)
* cree un archivo con el nombre asi:
*  - local => .env.local.php
*  - development => .env.development.php
*/
return [

'DATABASE_NAME' => 'DATABASE_NAME',
'DATABASE_USER' => 'DATABASE_USER',
'DATABASE_PASSWORD' => 'DATABASE_PASSWORD',

/*
* social
* '$RED$_IDENTIFIER'   => '',
* '$RED$_SECRET'       => '',
* '$RED$_CALLBACK_URI' => '',
*/

'GOOGLE_IDENTIFIER'     => 'GOOGLE_IDENTIFIER',
'GOOGLE_SECRET'         => 'GOOGLE_SECRET',
'GOOGLE_CALLBACK_URI'   => 'GOOGLE_CALLBACK_URI',

'FACEBOOK_IDENTIFIER'   => 'FACEBOOK_IDENTIFIER',
'FACEBOOK_SECRET'       => 'FACEBOOK_SECRET',
'FACEBOOK_CALLBACK_URI' => 'FACEBOOK_CALLBACK_URI',

/**
* PayU configs
* PAYU_TEST: 1 = Modo prueba, 0 = Modo Produccion
*/
'PAYU_MACRO_ACCOUNT_ID' => 654321,
'PAYU_MICRO_ACCOUNT_ID' => 123456,
'PAYU_MERCHANT_ID' => 123456,
'PAYU_API_KEY' => 'ABCabc123DEFdef456GHIghi78',
'PAYU_API_LOGIN' => '123abc456def789',
'PAYU_TEST' => 1,

];

我在 /var/www/kinbuweb/app/config/local/database.php 中有一個database.php文件,如果我有本地環境(並且我已將我的文件命名為 .env.local .php) 路徑中的文件**:**

<?php

return [

/*
|--------------------------------------------------------------------------
| 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' => [

   'mysql' => [
       'driver'   => 'mysql',
       'host'     => 'localhost',
       'database' => 'homestead',
       'username' => 'homestead',
       'password' => 'secret',
       'charset'  => 'utf8',
       'collation' => 'utf8_unicode_ci',
       'prefix'   => '',
   ],

   'pgsql' => [
       'driver'  => 'pgsql',
       'host'    => 'localhost',
       'database' => 'homestead',
       'username' => 'homestead',
       'password' => 'secret',
       'charset' => 'utf8',
       'prefix'  => '',
       'schema'  => 'public',
   ],

],

];

另一方面,我在本地文件夾(路徑:/var/www/kinbuweb/app/config)中有一個文件也稱為database.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'     => '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' => __DIR__ . '/../database/production.sqlite',
       'prefix' => '',
   ],

   'mysql'  => [
       'driver'   => 'mysql',
       'host'     => 'localhost',
       'database' => $_ENV['DATABASE_NAME'],
       'username' => $_ENV['DATABASE_USER'],
       'password' => $_ENV['DATABASE_PASSWORD'],
       'charset'  => 'utf8',
       'collation' => 'utf8_unicode_ci',
       'prefix'   => '',
   ],

   'pgsql'  => [
       'driver'  => 'pgsql',
       'host'    => 'localhost',
       'database' => 'forge',
       'username' => 'forge',
       'password' => '',
       'charset' => 'utf8',
       'prefix'  => '',
       'schema'  => 'public',
   ],

   'sqlsrv' => [
       'driver' => 'sqlsrv',
       'host'   => 'localhost',
       'database' => 'database',
       'username' => 'root',
       'password' => '',
       '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,
   ],

],

];

我試過的

  1. 使用本地 (/var/www/kinbuweb/app/config/local/database.php) database.php配置中的憑據,如下所示:
<?php
/**
* Variables de entorno .env.php
* Si se requiere cargar otros valores para un entorno diferente al de produccion (local, development, etc.)
* cree un archivo con el nombre asi:
*  - local => .env.local.php
*  - development => .env.development.php
*/
return [

'DATABASE_NAME' => 'homestead',
'DATABASE_USER' => 'homestead',
'DATABASE_PASSWORD' => 'secret',



'GOOGLE_IDENTIFIER'     => 'GOOGLE_IDENTIFIER',
'GOOGLE_SECRET'         => 'GOOGLE_SECRET',
'GOOGLE_CALLBACK_URI'   => 'GOOGLE_CALLBACK_URI',

'FACEBOOK_IDENTIFIER'   => 'FACEBOOK_IDENTIFIER',
'FACEBOOK_SECRET'       => 'FACEBOOK_SECRET',
'FACEBOOK_CALLBACK_URI' => 'FACEBOOK_CALLBACK_URI',


'PAYU_MACRO_ACCOUNT_ID' => 654321,
'PAYU_MICRO_ACCOUNT_ID' => 123456,
'PAYU_MERCHANT_ID' => 123456,
'PAYU_API_KEY' => 'ABCabc123DEFdef456GHIghi78',
'PAYU_API_LOGIN' => '123abc456def789',
'PAYU_TEST' => 1,

];
  1. 將埠聚合到本地文件夾中的數據庫配置,如下所示:
<?php

return [

'connections' => [

'mysql' => [
   'driver'   => 'mysql',
   'host'     => 'localhost:3306',
   'database' => 'homestead',
   'username' => 'homestead',
   'password' => 'secret',
   'charset'  => 'utf8',
   'collation' => 'utf8_unicode_ci',
   'prefix'   => '',
   ],

'pgsql' => [
   'driver'  => 'pgsql',
   'host'    => 'localhost',
   'database' => 'homestead',
   'username' => 'homestead',
   'password' => 'secret',
   'charset' => 'utf8',
   'prefix'  => '',
   'schema'  => 'public',
  ],

],

];

雖然我遇到了同樣的錯誤: PDOException (1045) HELP

SQLSTATE$$ HY000 $$ $$ 1045 $$使用者 ‘homestead’@’localhost’ 的訪問被拒絕(使用密碼:YES)

我有什麼想法可以解決這個問題嗎?謝謝

嘗試在您的數據庫中為您的使用者“宅基地”添加權限。

登錄 mysql 並添加如下權限:

mysql -u root -p

輸入你的 MySQL 密碼

mysql > grant all privileges on homestead *.* to 'homestead'@'localhost' identified by 'password'; 

這裡的“密碼”在您的 database.php 中

如果您在本地環境中,則 database.php 的路徑為:

你的項目文件夾/應用程序/配置/本地

mysql > FLUSH PRIVILEGES;

引用自:https://serverfault.com/questions/830587