Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql配置:拒绝访问错误(1045)_Php_Mysql_Ubuntu_Hyper V - Fatal编程技术网

Php mysql配置:拒绝访问错误(1045)

Php mysql配置:拒绝访问错误(1045),php,mysql,ubuntu,hyper-v,Php,Mysql,Ubuntu,Hyper V,我在使用Ubuntu的Hyper-V虚拟机上遇到了mysql问题。我正在使用的一些可能相关的规范: PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 (cli) mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper Psy Shell v0.8.18 (PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 — cli) 我正在从事一个实

我在使用Ubuntu的Hyper-V虚拟机上遇到了mysql问题。我正在使用的一些可能相关的规范:

PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 (cli) 
mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper
Psy Shell v0.8.18 (PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 — cli)
我正在从事一个实践项目,对编码非常陌生,因此我可能会遗漏一些对非新手来说非常明显的东西。当我尝试时:

php artisan migrate
我得到以下错误:

In Connection.php line 664:

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas  
  sword: YES) (SQL: select * from information_schema.tables where table_schem  
  a = mytestproject and table_name = migrations)                                     

In PDOConnection.php line 50:

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas  
  sword: YES)                                                                  

In PDOConnection.php line 46:

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas  
  sword: YES)  
我已尝试按如下方式设置我的database.php、.env文件:

.env文件:

APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mytestproject
DB_USERNAME=root
DB_PASSWORD=password

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

CACHE_DRIVER=redis
QUEUE_DRIVER=redis
DB_PASSWORD=password
database.php文件:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'mytestproject'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', 'mypassword'),
    'unix_socket' => env('DB_SOCKET', ' '),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],
Mysql用户:

mysql> SELECT user, host FROM mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.06 sec)
在我搜索这个错误的基础上,我看到许多解决方案表明需要正确配置.env文件和database.php文件,我已经尝试过了。我尝试设置root用户权限,将localhost更改为127.0.0.1,等等。我尝试了在各种留言板上看到的所有建议,但我相信我的设置可能有一些特殊之处,或者我尚未检查以解决此错误并继续我的设置。任何帮助都将不胜感激。

您展示了:

在.env文件中:

APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mytestproject
DB_USERNAME=root
DB_PASSWORD=password

REDIS_HOST=127.0.0.1
REDIS_PORT=6379

CACHE_DRIVER=redis
QUEUE_DRIVER=redis
DB_PASSWORD=password
在database.php中:

'password' => env('DB_PASSWORD', 'mypassword'),

那么正确的密码在哪里?

您可以使用mysql客户端连接到数据库吗?您确定您的.env文件已加载吗?env不是一个php函数(也许你正在使用CakePHP?),试试
mysql>GRANT-ALL-ON*.*到带有GRANT选项的'root'@'localhost'
>刷新权限
这意味着它首先尝试从环境变量DB_password获取密码,如果找不到,则使用“secret”。所以,期待你的环境文件不工作。你试过这个吗-