php Laravel 4.2 中的用户“root”@“localhost”(使用密码:YES)拒绝 Laravel 访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39035201/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:47:44  来源:igfitidea点击:

Laravel Access denied for user 'root'@'localhost' (using password: YES) in laravel 4.2

phplaravellaravel-4laravel-5.2

提问by iCoders

I have old project which built using Laravel 4.2.I am getting following error

我有使用 Laravel 4.2 构建的旧项目。我收到以下错误

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

PDOException (1045) SQLSTATE[HY000] [1045] 用户“root”@“localhost”的访问被拒绝(使用密码:YES)

I have googled and tried every think but i couldn't able to fix it

我用谷歌搜索并尝试了每一个想法,但我无法解决它

.envfile

.env文件

APP_KEY=az9tq5VHQCV9g5m2CsgY89jtijrCMgEA
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234

database.php

数据库.php

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'billing',
            'username'  => 'root',
            'password'  => '1234',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

enter image description here

在此处输入图片说明

Can any one guide me where m doing wrong ?

任何人都可以指导我哪里做错了吗?

Note: Before asking question i tried by updating composer update as well as most of the stackoverflow answers.

注意:在提出问题之前,我尝试更新 composer update 以及大多数 stackoverflow 答案。

Updated

更新

I have tested this connection by creating php file

我已经通过创建 php 文件测试了这个连接

<?php
$servername = "localhost";
$username = "root";
$password = "1234";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

i will get Connected successfully message

我会收到连接成功的消息

采纳答案by Magna Centipede

Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.

Laravel 4.x 甚至不支持 ENV 文件。您只需要查看 ./config/[env name]/database.php 中的设置是否正确。

回答by Alexey Mezenin

The error says it all, Laravel can't connect to a DB. Check privelegesand make sure the DB exists. Also, try to connect to a DB with a client, like MySQL Workbench using same login and password. This will give a hint about what you can do to fix this. If you can't do this, it's not a Laravel issue.

错误说明了一切,Laravel 无法连接到数据库。检查特权并确保数据库存在。此外,尝试使用客户端连接到数据库,例如使用相同的登录名和密码的 MySQL Workbench。这将提示您可以采取哪些措施来解决此问题。如果你不能这样做,那不是 Laravel 的问题。

回答by emotality

DB_HOST=localhostworked for me on Laravel 5.7

DB_HOST=localhost在 Laravel 5.7 上为我工作

回答by Parithiban

Try This

尝试这个

.env file

.env 文件

APP_ENV=local

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234

config/database.php

配置/数据库.php

default' => env('DB_CONNECTION', 'mysql'),


'mysql' => array(
            'driver'    => 'mysql',
            'host'      => env('DB_HOST','localhost'),
            'database'  => env('DB_DATABASE','billing'),
            'username'  => env('DB_USERNAME','root'),
            'password'  => env('DB_PASSWORD', '1234'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        )

Else checkout the .env file and check the connection

否则签出 .env 文件并检查连接

回答by Francois Stander

I just ran into the same problem on a new Laravel install. The problem was that I was connecting to my ubuntu localhost mysql server instead of to the vagrant box's mysql server that was on it's own ip address '192.168.10.10'. I changed over to that and all worked a charm :)

我刚刚在新的 Laravel 安装中遇到了同样的问题。问题是我连接到我的 ubuntu localhost mysql 服务器,而不是连接到它自己的 IP 地址“192.168.10.10”上的流浪盒的 mysql 服务器。我改过来了,一切都很有魅力:)

回答by Naeem Ijaz

Try In .ENV

尝试 .ENV

APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234 

And DeleteCache Files From Root/boostrap/chache/filesand Run The App

并从Root/boostrap/chache/files 中删除缓存文件并运行应用程序

回答by Carlos Vieira

I'm test in Laravel 5.6.33 and i see this error: "Access denied for user 'root'@'localhost'", but the username and password is correct. In this version i think of this is a BUG, i change DB_HOST, from 127.0.0.1 to localhost and works!

我在 Laravel 5.6.33 中进行测试,我看到此错误:“用户 'root'@'localhost' 拒绝访问”,但用户名和密码正确。在这个版本中,我认为这是一个 BUG,我将 DB_HOST 从 127.0.0.1 更改为 localhost 并且可以正常工作!

DB_HOST=127.0.0.1(before)
DB_HOST=localhost (after)

DB_HOST=127.0.0.1(之前)
DB_HOST=localhost(之后)

回答by Kaushik shrimali

Access denied for user 'root'@'localhost' (using password: YES)

用户 'root'@'localhost' 访问被拒绝(使用密码:YES)

The error was coming? The possible reason is your database connection.If the database is not connect then throw the error.

So Check the database releted all things and If all things is correct.

If not solved then change

错误来了?可能的原因是您的数据库连接。如果数据库未连接,则抛出错误。

所以检查数据库releted所有的东西,如果所有的东西都是正确的。

如果没有解决,那么改变

DB_HOST=127.0.0.1 TO DB_HOST=localhost

回答by Dpetrov

Laravel 6 just set secret instead of empty space.

Laravel 6 只是设置了秘密而不是空白空间。

DB_PASSWORD=secret

回答by Jacman

Make sure your .envis using the correct port number. If you're using phpMyAdminthis is how is setup:

确保您.env使用的是正确的端口号。如果您使用的phpMyAdmin是这样的设置:

MariaDB defaults to 3306

and

MySQL defaults to 3308

So change your .envto use port 3308 for MySQL. I hope this helps!

因此,更改您.env的 MySQL 使用端口 3308。我希望这有帮助!