我正在使用Laravel 5并获得以下异常:
PDOException(1044)SQLSTATE [HY000] [1044]拒绝用户访问'' localhost'到数据库' 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,
],
],
];
我该如何解决?
答案 0 :(得分:4)
将.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()
中,函数正在从此处查找此文件和变量。
更新:您必须在运行应用程序或迁移之前创建空白数据库。
答案 1 :(得分:1)
将来的某个时间。如果使用MySQL而不是Homestead,这将是一个基本设置。 Config-&gt; 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
如果您运行该命令,它应该可以顺利运行。
答案 2 :(得分:1)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
如果在xampp中安装MySQL时没有默认用户名,则应将用户名设置为root。
答案 3 :(得分:0)
You can check the current environment with: php artisan env
next
database.php
with development | staging access
.env
file APP_ENV=development | staging | production
答案 4 :(得分:0)
尝试转到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,
],
],
];
答案 5 :(得分:0)
此问题可能是由于文件没有预期的“ .env”扩展名引起的。
如果Windows中隐藏了文件扩展名(默认情况下处于打开状态),则实际上扩展名是“ .env”时,它很容易看起来。 env.txt”。
这意味着该程序将没有正确的扩展名,因此无法找到您的“ .env”文件。
使用Sublime Text之类的编辑器打开文件,您将看到 actual 扩展名是什么。如果扩展名为“ .txt”,请删除该部分并重新保存。
答案 6 :(得分:0)
我收到相同的消息,但用户名为空。用户名始终为空。
问题出在MySQL。直接从MySQL传递SQLSTATE错误消息。
因此,我删除了数据库和用户。在PhpMyAdmin的“数据库”选项卡中重新创建数据库。并且重要的是分别添加了用户和特权。这解决了问题。
我可以从MySQL客户端连接并运行迁移。
答案 7 :(得分:0)
我们忘记在 .env 文件中设置ems
,因此出现此错误:
SQLSTATE [HY000] [1044]用户“ @'localhost”的访问被拒绝 数据库“ forge”
打开 .env 文件并进行编辑。只需设置正确的数据库凭据即可:
DB_USERNAME=
如果在安装时没有默认用户名,则 DB_USERNAME= //Your Database Username
应设置为DB_USERNAME
更改 .env 后,请在终端中输入以下命令以清除缓存:root
注意:如果仍然存在错误
如果您使用的是PHP的默认网络服务器(例如php artisan config:cache
),则需要重新启动服务器
OR
如果您使用过 XAMPP ,请重新启动 Apache 服务器
答案 8 :(得分:0)
在同一个项目/查询中使用多个数据库时也会出现此问题
我通过对所有数据库使用相同的 DB_USERNAME 解决了同样的问题, 因为我在同一个项目中使用了多个数据库,并且每个数据库 DB_USERNAME 都不同,所以当我从多个数据库中运行查询选择、更新、删除等时,会出现此错误。
因为如果您在查询中使用多个表并且数据库 DB_USERNAME 不同,您将收到此错误。
解决方案:
如果您使用/连接多个数据库,那么您应该为所有数据库使用相同的 DB_USERNAME
答案 9 :(得分:0)
更改帐户所有权后遇到了同样的问题,所以我最终做了以下操作:
(这对我有用)
记下您的实际用户名和密码 转到您的 cPanel(如果您使用的是 cPanel) 从数据库选项卡中,选择 MySQL 数据库。 导航到当前用户 删除与您有冲突的数据库对应的用户。 删除后,转到将用户添加到数据库选项卡并重新输入用户名和密码,这将重新生成具有当前所有者权限的连接。
答案 10 :(得分:-2)
如果您在访问数据库时不需要登录 尝试将数据库用户名作为“root”,密码字段留空 这是解决我的问题