简体   繁体   English

使用PHP拒绝访问MySQL数据库

[英]Access denied to mySQL database using PHP

So I've been trying to solve this problem for a couple of hours now. 因此,我已经尝试解决这个问题几个小时了。 And I have to make something clear. 我必须弄清楚一点。

  1. I can access the mySQL database from terminal using 我可以使用以下命令从终端访问mySQL数据库

    mysql -u root mysql -u root

  2. I can also access the database from mySQL workbench 我也可以从MySQL工作台访问数据库
  3. My root user on mySQL has no password 我在MySQL上的root用户没有密码

Now my code online is pretty simple. 现在我的在线代码非常简单。 It is PHP calling the mysqli 是PHP调用mysqli

<?php
$database = "myDatabase";

// Create new connection
$conn = new mysqli("localhost", "root", "", $database);

if ($conn -> connect_error) {
  die ("Something went wrong: " . $conn -> connect_error);
} else {
  echo "It works";
}
?>

Now I always get the error code 现在我总是得到错误代码

Something went wrong: Access denied for user 'root'@'localhost' (using password: NO) 发生问题:用户'root'@'localhost'的访问被拒绝(使用密码:NO)

If I could get any help that would be greatly appreaciated. 如果我能得到任何帮助,将不胜感激。 I've already tried to grant access but I think I'm doing it wrong. 我已经尝试授予访问权限,但我认为我做错了。

mysqli_connect may be interpreting the empty password as no password at all mysqli_connect可能会将空密码解释为根本没有密码

using password: NO 使用密码:否

Either try to set a password for the root user, or create a new user with a password. 尝试为root用户设置密码,或使用密码创建新用户。 And via Tripp Kinetics comment, make sure the permissions are set properly for the new user. 并通过Tripp Kinetics注释,确保为新用户正确设置权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM