【问题标题】:Access denied error with php xamppphp xampp的访问被拒绝错误
【发布时间】:2017-02-14 00:28:30
【问题描述】:

当我尝试使用我的网络应用程序访问数据库时,我得到:

mysqli::mysqli(): (HY000/1045): 在 C:\xampp\htdocs\Forum\accounts\PlayerAccount\Login.php 中用户 'php'@'localhost' 的访问被拒绝(使用密码:YES)在第 4 行 连接到 MySQL 数据库时出错 (1045) 用户 'php'@'localhost' 的访问被拒绝(使用密码:YES)

我没有为应用程序使用 root。我创建了一个拥有所有权限的名为 PHP 的用户。我正在使用 xampp mysql 数据库,但它仍然无法正常工作。

编辑:根据要求,这里是代码:

<?php

  function getAccountRow($uuid){
  $configs = include('config.php');
  $mysqli = new mysqli($configs["host"], $configs["username"], $configs["password"], "account");    if($mysqli->connect_errno){
      die("Error connecting to MySQL database (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    $stmt = $mysqli->prepare("SELECT * FROM accounts WHERE uuid = ?");
    $stmt->bind_param("s", $uuid);
    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc();
    return $row;
  }
    $file = fopen("test.txt","a");
    $post_json = file_get_contents("php://input");
    $post = json_decode($post_json, true);
    foreach($post as $key=> $value) {
        $message = $key . ":" . $value . "\n";
        file_put_contents("login_test", $message);
        fwrite($file,$message);
    }
    fclose($file);
    $response = array();
  $row = getAccountRow($post["Uuid"]);
  if($row["id"] == NULL){
$configs = include('config.php');
  $mysqli = new mysqli($configs["host"], $configs["username"], $configs["password"], "Account");    if($mysqli->connect_errno){
      die("Error connecting to MySQL database (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    //$_stmt = $mysqli->prepare("INSERT INTO accounts (uuid, name) values(?, ?)");
    //$_stmt->bind_param("ss", $post["Uuid"], $post["Name"]);
    //$_stmt->execute();
  }
  $row = getAccountRow($post["Uuid"]);
  $_rankperm = $row["rankPerm"];
  $rperm = false;
  if($_rankperm == 1){
    $rperm = true;
  }
  $response["AccountId"] = $row["id"];
  $response["Name"] = $row["name"];
  $response["Rank"] = $row["rank"];

  $response["RankPerm"] = $rperm;
  $response["RankExpire"] = (int) $row["rankExpire"];
  $response["EconomyBalance"] = 100;
  $response["LastLogin"] = (int) $row["lastLogin"];

    die(json_encode($response));
?>

【问题讨论】:

  • 然后猜测你的密码拼写错误,或者你设置的用户不正确,或者你创建了一个名为 PHP 的用户并在 php 的代码中使用了用户 ID
  • 添加用户后是否刷新权限?
  • 你好像没有设置root用户来连接,你能公开你用来连接的代码吗?
  • @OscarDavid 他说他没有使用root 这不是使用root 的先决条件,事实上这是人们犯的那些菜鸟错误之一
  • 确保您的用户名和密码正确。重置它,如果你必须的话。确保用户/主机组合具有 account 数据库的权限,并且该数据库存在。

标签: php mysql xampp


【解决方案1】:

在你的 Mysql 上通过 line 命令执行下面的语句

> grant all privileges on your-database-name.* to 'php'@'localhost'  identified by 'your_pass_here';
> flush privileges;

它将授予您用于连接的用户访问权限。

【讨论】:

  • 现在添加 ALTER USER 'php'@'localhost' PASSWORD EXPIRE NEVER 可能很有用,或者在 30 天内它可能会再次停止工作,并评论最近的 mysql 5.7 更改
  • 将所有内容授予所有数据库是一个巨大的安全漏洞。它应该只设置为所需的数据库。
  • 听听@aynber
  • 我如何使用 phpmyadmin 来做到这一点
  • 如果你有root用户,只执行SQL,但如果你使用共享服务,你可以尝试通过向导给用户权限
【解决方案2】:

你有 mysql auth 的问题。尝试使用用户名连接到您的数据库:root,密码:''(空字符串)。这是 XAMPP 的默认设置。

【讨论】:

  • 我知道您缺少代表,但这只是一个评论。这种类型的答案会吸引反对票或被标记为我没有 DV,如果发生这种情况,您将失去代表点并且需要更长的时间才能达到您需要评论的 50 个代表任何问题。在那之前,坚持那些问得好,因此无需澄清就能轻松回答的问题。 meta.stackexchange.com/questions/214173/…
  • 感谢您的反馈))我知道这种类型的答案不好)但我不会写 cmets :D
猜你喜欢
  • 2014-05-23
  • 2020-07-09
  • 1970-01-01
  • 2016-04-02
  • 2018-10-03
  • 2011-11-24
  • 2018-08-10
  • 1970-01-01
  • 2016-06-26
相关资源
最近更新 更多