Laravel PDOException(1044)SQLSTATE[HY000][1044]拒绝用户访问''@';本地主机';到数据库';锻造&x27;
我正在使用Laravel 5,并得到以下异常: 用户的PDO异常(1044)SQLSTATE[HY000][1044]访问被拒绝 ''@'localhost'到数据库'forge' 我的数据库配置文件是:Laravel PDOException(1044)SQLSTATE[HY000][1044]拒绝用户访问''@';本地主机';到数据库';锻造&x27;,laravel,laravel-5,database-migration,laravel-migrations,Laravel,Laravel 5,Database Migration,Laravel Migrations,我正在使用Laravel 5,并得到以下异常: 用户的PDO异常(1044)SQLSTATE[HY000][1044]访问被拒绝 ''@'localhost'到数据库'forge' 我的数据库配置文件是: <?php return [ /* |-------------------------------------------------------------------------- | PDO Fetch Style |------------------------------
<?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 | staging(文件夹)下创建
- 使用
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,这将是一个基本设置。Config->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
如果您运行该命令,它应该可以正常工作。如果您在访问数据库时不需要登录
尝试将数据库用户名作为根用户
和密码字段留空
这解决了我的问题
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传递
于是我删除了数据库和用户。在PhpMyAdmin的“数据库”选项卡中重新创建数据库。重要的是分别添加了用户和权限。这解决了问题
我可以从MySQL客户端连接并运行迁移 我们忘了在.env文件中设置DB\u USERNAME=
,因此出现以下错误:
用户“@'localhost”对的SQLSTATE[HY000][1044]访问被拒绝
数据库“forge”
打开.env文件并编辑它。只需设置正确的数据库凭据:
DB_USERNAME= //Your Database Username
如果在安装时没有默认用户名,DB_USERNAME
应设置为root
更改.env后,在终端中输入以下命令以清除缓存:php artisan config:cache
注意:如果仍然存在错误
如果您使用的是PHP的默认web服务器(例如PHP artisan serve
),则需要重新启动服务器
或
如果使用了XAMPP,请重新启动Apache服务器在同一项目/查询中使用多个数据库时,也会出现此问题
我通过对所有数据库使用相同的DB_用户名解决了相同的问题,
因为我在同一个项目中使用了多个数据库,并且每个数据库DB_用户名都不同,所以当我从多个数据库中运行查询select、update、delete等时,就得到了这个错误
因为如果在查询中使用多个表,并且数据库DB_用户名不同,则会出现此错误
解决方案:
如果您使用/连接多个数据库,那么您应该为所有数据库使用相同的DB_用户名您是否使用homestead作为开发环境?您是否也将.env
文件用于环境?如果你能共享你的数据库配置文件就更好了。我添加了配置文件。当前应用程序环境:productionThis.env文件不在我的根目录中。。但在创建它之后,问题仍然是一样的。我解决了它。我的数据库和用户名都是“forge”。我更改了我的数据库名称和用户名并解决了它。。。感谢@Ariful haque感谢php-artisan-config:cache
-->+1Nice-response@krishnel。