感谢您阅读我的问题,我花了一周时间研究这个错误,阅读了 ServerFault、Stackoverflow 和 AskUbuntu 中的问题,但无法找出问题所在。
我有
- Lubuntu 16.04(Ubuntu 发行版)
- Nginx
- Laravel 4.2.17
- MySQL 5.7
我有一个从 GitHub 克隆的预先存在的项目,一切进展顺利,但是当我转到必须连接到数据库的页面(例如尝试登录)时,出现此错误:
PDOException(1045)帮助
SQLSTATE[HY000] [1045] 拒绝用户 'homestead'@'localhost' 访问(使用密码:是)
涉及文件
我在项目中有一个 .env 文件,我将其命名为.env.本地.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,
];
我有一个数据库.php/var/www/kinbuweb/app/config/local/database.php 中的文件我突出显示了我一直在阅读的 /config/local 路径,如果我有一个本地环境,则该路径是配置(并且我将我的文件命名为 .env。当地的.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)中有一个名为的文件数据库.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,
],
],
];
我曾尝试过
使用本地的凭证(/var/www/kinbuweb/app/config/local/database.php)数据库.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, ];
将端口聚合到本地文件夹中的数据库配置,如下所示:
<?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)帮助
SQLSTATE[HY000] [1045] 拒绝用户 'homestead'@'localhost' 访问(使用密码:是)
有什么办法可以解决这个问题吗?谢谢
答案1
尝试在您的 mysql 数据库中为您的用户“homestead”添加权限。
登录mysql并添加权限如下:
mysql -u root -p
输入你的MySQL密码
mysql > grant all privileges on homestead *.* to 'homestead'@'localhost' identified by 'password';
这里的“密码”在你的 database.php 中
如果您在本地环境中,database.php 的路径是:
你的项目文件夹/app/config/local
mysql > FLUSH PRIVILEGES;