用户root @ localhost被拒绝访问


access denied for user root @ localhost

我试图连接到mysql,但它给出了一个警告mysqli_connect(): (HY000/1045):用户'root'@'localhost'访问被拒绝(使用密码:YES)我认为用户名和密码是正确的,因为我可以在命令行访问mysql。我还创建了另一个用户,并授予他所有权限,但我仍然有这个警告。

我可以访问phpMyAdmin。我使用wamp服务器(apache 2.4.9)和mysql 5.1

     <?php
      $conn = mysqli_connect("localhost", "root", "password");
       ?>

如果user存在且blahblah不重要,则让以下两行失败:

create user 'root'@'localhost' identified by 'blahblah';
create user 'root'@'127.0.0.1' identified by 'blahblah';

Do your grants:

grant all on *.* to 'root'@'localhost';
grant all on *.* to 'root'@'127.0.0.1';

将密码更改为您将记住的内容:

set password for 'root'@'localhost' = password('NewPassword');
set password for 'root'@'127.0.0.1' = password('NewPassword');

查看您有多少根用户。真正的用户是用户/主机的组合。密码将以散列形式显示:

select user,host,password from mysql.user where user='root';

select user,host,authentication_string from mysql.user where user='root';

上面的第二个是MySQL 5.7

如果您有以上两个以上的用户,则删除其他用户,例如:

drop user 'root'@'%';   -- this is the wildcard hostname, can be a security risk
drop user 'root'@'::1';

还是只有2个?我希望如此。使用上面的select stmts来检查。

不要使用root连接用户应用。Root仅用于维护。无论它是服务器端代码,还是管理员正在运行它。不安全和/或注入有害语句的代码将以root身份运行。

wamp的mysql默认配置为root用户名,没有密码

$conn = mysqli_connect("localhost", "root", "");

上面的代码应该可以工作

如果您使用的是本地主机。它没有密码。你只需像这样设置它:

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("database name here") or die(mysql_error());
?>