PDOException (1044) SQLSTATE[HY000] [1044] 拒绝用户 ''@'localhost' 访问数据库 'forge'
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
我正在使用 Laravel 5 并收到以下异常:
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user
''@'localhost' to database 'forge'
我的数据库配置文件是:
<?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,
],
],
];
我该如何解决?
在您的根目录中放置一个 .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
在此处更新您的数据库、用户名和密码字段,它应该可以解决您的问题。在您的配置文件中 env()
函数正在从此处查找此文件和变量。
更新:您必须在 运行 应用程序或迁移之前创建一个空白数据库。
您可以通过以下方式检查当前环境:php artisan env
下一个
- 在 config/development 下创建 |暂存(文件夹)
- 保存文件
database.php
development | staging access
- 编辑
.env
文件APP_ENV=development | staging | production
尝试转到 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,
],
],
];
将来某个时候。如果使用 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,
],
并在您的 .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
如果您 运行 命令应该可以顺利运行。
如果您在访问数据库时不需要登录
尝试将数据库用户名设为root
,密码字段留空
这解决了我的问题
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
如果您在 XAMPP 中安装 MySQL 时没有默认用户名,则应将 DB_USERNAME=
设置为 root
。
此问题可能是由于文件没有预期的“.env”扩展名造成的。
如果您在 Windows 中隐藏了文件扩展名(默认情况下打开),那么它很容易 看起来 就像扩展名是“.env”,当它实际上类似于“.env.txt”。
这意味着该程序将无法找到您的“.env”文件,因为它没有正确的扩展名。
使用 Sublime Text 等编辑器打开文件,您将看到 实际 扩展名是什么。如果它有“.txt”扩展名,请删除该部分并重新保存。
我收到了相同的消息,但用户名为空。用户名始终为空。
问题出在 MySQL。 SQLSTATE 错误消息直接从 MySQL.
传递
然后我删除了数据库和用户。 Re-created PhpMyAdmin 的数据库选项卡中的数据库。重要的是分别添加了用户和权限。这解决了这个问题。
我可以从 MySQL 客户端和 运行 迁移连接。
我们忘记在 .env 文件中设置 DB_USERNAME=
,所以我们得到这个错误:
SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to
database 'forge'
打开.env文件并编辑它。只需设置正确的数据库凭据:
DB_USERNAME= //Your Database Username
如果安装时没有默认用户名,DB_USERNAME
应设置为root
更改 .env 后,在您的终端中输入此命令以清除缓存:php artisan config:cache
注意:如果还是报错
如果您使用 PHP 的默认 Web 服务器(例如 php artisan serve
),您需要 重新启动 您的服务器
或
如果您使用过 XAMPP 然后重新启动您的 Apache 服务器
当您在同一个 project/query
中使用多个数据库时,也会出现此问题
我通过对所有数据库使用相同的 DB_USERNAME 解决了同样的问题,
因为我在同一个项目中使用了多个数据库,并且每个数据库 DB_USERNAME 都是不同的,所以当我 运行 从多个数据库查询 select、更新、删除等时,就会出现此错误。
因为如果您在查询中使用多个 table 并且数据库 DB_USERNAME 不同,您将收到此错误。
解法:
如果您 using/connect 多个数据库,那么您应该对所有数据库使用相同的 DB_USERNAME
更改帐户所有权后遇到同样的问题,所以我最终执行了以下操作:
(这对我有用)
记下您的实际用户名和密码
转到您的 cPanel(如果您使用的是 cPanel)
在“数据库”选项卡中,select MySQL 个数据库。
导航到当前用户
删除与您有冲突的数据库对应的用户。
删除后,转到“将用户添加到数据库”选项卡并重新输入用户名和密码,这将重新生成具有当前所有者权限的连接。
我正在使用 Laravel 5 并收到以下异常:
PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
我的数据库配置文件是:
<?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,
],
],
];
我该如何解决?
在您的根目录中放置一个 .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
在此处更新您的数据库、用户名和密码字段,它应该可以解决您的问题。在您的配置文件中 env()
函数正在从此处查找此文件和变量。
更新:您必须在 运行 应用程序或迁移之前创建一个空白数据库。
您可以通过以下方式检查当前环境:php artisan env
下一个
- 在 config/development 下创建 |暂存(文件夹)
- 保存文件
database.php
development | staging access
- 编辑
.env
文件APP_ENV=development | staging | production
尝试转到 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,
],
],
];
将来某个时候。如果使用 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,
],
并在您的 .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
如果您 运行 命令应该可以顺利运行。
如果您在访问数据库时不需要登录
尝试将数据库用户名设为root
,密码字段留空
这解决了我的问题
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
如果您在 XAMPP 中安装 MySQL 时没有默认用户名,则应将 DB_USERNAME=
设置为 root
。
此问题可能是由于文件没有预期的“.env”扩展名造成的。
如果您在 Windows 中隐藏了文件扩展名(默认情况下打开),那么它很容易 看起来 就像扩展名是“.env”,当它实际上类似于“.env.txt”。
这意味着该程序将无法找到您的“.env”文件,因为它没有正确的扩展名。
使用 Sublime Text 等编辑器打开文件,您将看到 实际 扩展名是什么。如果它有“.txt”扩展名,请删除该部分并重新保存。
我收到了相同的消息,但用户名为空。用户名始终为空。
问题出在 MySQL。 SQLSTATE 错误消息直接从 MySQL.
传递然后我删除了数据库和用户。 Re-created PhpMyAdmin 的数据库选项卡中的数据库。重要的是分别添加了用户和权限。这解决了这个问题。
我可以从 MySQL 客户端和 运行 迁移连接。
我们忘记在 .env 文件中设置 DB_USERNAME=
,所以我们得到这个错误:
SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'
打开.env文件并编辑它。只需设置正确的数据库凭据:
DB_USERNAME= //Your Database Username
如果安装时没有默认用户名,DB_USERNAME
应设置为root
更改 .env 后,在您的终端中输入此命令以清除缓存:php artisan config:cache
注意:如果还是报错
如果您使用 PHP 的默认 Web 服务器(例如 php artisan serve
),您需要 重新启动 您的服务器
或
如果您使用过 XAMPP 然后重新启动您的 Apache 服务器
当您在同一个 project/query
中使用多个数据库时,也会出现此问题我通过对所有数据库使用相同的 DB_USERNAME 解决了同样的问题, 因为我在同一个项目中使用了多个数据库,并且每个数据库 DB_USERNAME 都是不同的,所以当我 运行 从多个数据库查询 select、更新、删除等时,就会出现此错误。
因为如果您在查询中使用多个 table 并且数据库 DB_USERNAME 不同,您将收到此错误。
解法:
如果您 using/connect 多个数据库,那么您应该对所有数据库使用相同的 DB_USERNAME
更改帐户所有权后遇到同样的问题,所以我最终执行了以下操作:
(这对我有用)
记下您的实际用户名和密码 转到您的 cPanel(如果您使用的是 cPanel) 在“数据库”选项卡中,select MySQL 个数据库。 导航到当前用户 删除与您有冲突的数据库对应的用户。 删除后,转到“将用户添加到数据库”选项卡并重新输入用户名和密码,这将重新生成具有当前所有者权限的连接。