laravel PDOException (1044) SQLSTATE[HY000] [1044] 用户 ''@'localhost' 对数据库 'forge' 的访问被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29986626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
提问by Hassan Saqib
I am using Laravel 5 and getting the following exception:
我正在使用 Laravel 5 并收到以下异常:
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
PDOException (1044) SQLSTATE[HY000] [1044] 用户 ''@'localhost' 对数据库 'forge' 的访问被拒绝
My database configuration file is:
我的数据库配置文件是:
<?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' => 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', ''),
'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,
],
],
];
How can I solve it?
我该如何解决?
回答by Ariful Haque
Put a .env
file in your root directory and paste this code there.
.env
在根目录中放置一个文件并将此代码粘贴到那里。
APP_ENV=local
APP_DEBUG=true
APP_KEY=1CaND3OKKvOGSBAlCg6IyrRmTQWwZjOO
DB_HOST = localhost
DB_DATABASE = YOUR_DATABASE_NAME
DB_USERNAME = USER_NAME
DB_PASSWORD = PASSWORD
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Update your database, username and password field here and it should solve your problem. In your configuration file env()
function is looking for this file and variables from here.
在此处更新您的数据库、用户名和密码字段,它应该可以解决您的问题。在您的配置文件中,env()
函数正在从此处查找此文件和变量。
UPDATE: You must create a blank database before running the app or migration.
更新:您必须在运行应用程序或迁移之前创建一个空白数据库。
回答by Udhav Sarvaiya
We forgot to set DB_USERNAME=
in the .envfile, so we got this error:
我们忘记DB_USERNAME=
在.env文件中设置,所以我们得到了这个错误:
SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
SQLSTATE[HY000] [1044] 用户 ''@'localhost' 对数据库 'forge' 的访问被拒绝
Open the .envfile and edit it. Just set up correct DB credentials:
打开.env文件并进行编辑。只需设置正确的数据库凭据:
DB_USERNAME= //Your Database Username
DB_USERNAME
should be set to root
if you do not have a default username in the installation time
DB_USERNAME
root
如果您在安装时没有默认用户名,则应设置为
After changes of .enventer this command in your terminal for clear cache:php artisan config:cache
更改.env后,在终端中输入此命令以清除缓存:php artisan config:cache
NOTE: If there is still error
注意:如果还有错误
If you are using the PHP's default web server (eg. php artisan serve
) you need to restartyour server
如果您使用的是 PHP 的默认 Web 服务器(例如php artisan serve
),则需要重新启动服务器
OR
或者
If you have used XAMPPthen restart your Apacheserver
如果您使用过XAMPP,则重新启动Apache服务器
回答by Premium Ayodele
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
The DB_USERNAME=
should be set to root
if you do not have a default username in the installation of MySQL in XAMPP.
该DB_USERNAME=
应设置为root
,如果你没有在XAMPP安装的MySQL默认用户名。
回答by David Segura
This problem can be caused by the file not having the expected ".env" extension.
此问题可能是由文件没有预期的“.env”扩展名引起的。
If you have file extensions hidden in Windows (which is on by default), then it can easily looklike the extension is ".env", when it is actually something like, like ".env.txt".
如果您在 Windows 中隐藏了文件扩展名(默认情况下处于启用状态),那么它很容易看起来像是“.env”,而实际上它类似于“.env.txt”。
This means that the program won't be able to find your ".env" file because it doesn't have the right extension.
这意味着程序将无法找到您的“.env”文件,因为它没有正确的扩展名。
Open the file with an editor like Sublime Text, and you'll see what the actualextension is. If it has a ".txt" extension, delete that part and re-save.
使用 Sublime Text 之类的编辑器打开文件,您将看到实际的扩展名是什么。如果它有“.txt”扩展名,请删除该部分并重新保存。
回答by raeeschaudhary
I was getting the same message with empty username. The username was always blank.
我收到相同的消息,用户名为空。用户名始终为空。
The issue was with MySQL. The SQLSTATE error message is being passed directly from MySQL.
问题出在 MySQL 上。SQLSTATE 错误消息直接从 MySQL 传递。
So then I deleted the database and user. Re-created the database in the Databases tab of PhpMyAdmin. And importantly separately added the user and privileges. This fixed the issue.
然后我删除了数据库和用户。在 PhpMyAdmin 的“数据库”选项卡中重新创建了数据库。并且重要的是分别添加了用户和权限。这解决了这个问题。
I could connect from the MySQL client and run migrations.
我可以从 MySQL 客户端连接并运行迁移。
回答by Carlos Avalos
You can check the current environment with: php artisan env
您可以使用以下命令检查当前环境: php artisan env
next
下一个
- create under config/development | staging (folder)
- save a file
database.php
withdevelopment | staging access
- edit
.env
fileAPP_ENV=development | staging | production
- 在配置/开发下创建 | 暂存(文件夹)
- 保存文件
database.php
与development | staging access
- 编辑
.env
文件APP_ENV=development | staging | production
回答by Ethicalguy Gopal Bogati
Try to go to database.php file correct your database name,username and password
尝试转到 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_OBJ,
/*
|--------------------------------------------------------------------------
| 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' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'msm'),----database name
'username' => env('DB_USERNAME', 'root'),--localhost username
'password' => env('DB_PASSWORD', ''),--localhost password
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'msm'),----database name
'username' => env('DB_USERNAME', 'root'),--localhost username
'password' => env('DB_PASSWORD', ''),--localhost password
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
/*
|--------------------------------------------------------------------------
| 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' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
回答by Krishneil
Sometime in the future. This will be a basic setup if using MySQL instead of Homestead. Config->database.php file
将来的某个时候。如果使用 MySQL 而不是 Homestead,这将是一个基本设置。配置->database.php 文件
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'yourDatabaseName'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and in your .env file
并在您的 .env 文件中
APP_ENV=local
APP_DEBUG=true
APP_KEY=ruA9CAKRJCFgLOD1nc5o1BmvaTGokasi
DB_HOST=localhost
DB_DATABASE=yourDatabaseName
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
and if you run the command it should work smoothly.
如果您运行该命令,它应该可以顺利运行。
回答by Harsh Pawar
If you don't require login while accessing the database
如果您在访问数据库时不需要登录
Try database usernameas root
and passwordfield leave blank
尝试数据库用户名作为root
和密码字段留空
This solves my problem
这解决了我的问题