连接失败:用户 ''username'@'localhost' 访问被拒绝(使用

Connection failed: Access denied for user #39;#39;username#39;@#39;localhost#39; (using password: YES)(连接失败:用户 username@localhost 访问被拒绝(使用密码:YES))
本文介绍了连接失败:用户 ''username'@'localhost' 访问被拒绝(使用密码:YES)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试连接到服务器上的 MySQL,但出现以下错误.用户已创建并被授予所有权限.

I am trying to connect to MySQL on the server but gives following error. User is created and granted all privileges.

它可以在本地机器上运行,但在部署到服务器之后就不行了.

还有 mysql_connect 函数可以工作,但是 new mysqli() 给出如下所述的拒绝访问.

Also mysql_connect function works but new mysqli() gives access denied as mentioned below.

也尝试添加端口.

警告: mysqli::mysqli(): (HY000/1045): Access denied for user ''usernam'@'localhost' (using password: YES) in/home/domainname/public_html/autopublish/test.php on8号线连接失败:用户 ''username'@'localhost' 访问被拒绝(使用密码:YES)

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''usernam'@'localhost' (using password: YES) in /home/domainname/public_html/autopublish/test.php on line 8 Connection failed: Access denied for user ''username'@'localhost' (using password: YES)

<?php
    $servername = "localhost";
    $username = "'xxxxx";
    $password = "xxxxx";
    $dbname = "xxxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password);  **//line 8**
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        echo "no connection";
    }

    $sql = "SELECT * FROM pages";
    $result = $conn->query($sql);
    
    $data = array();
    $arr = array();
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            
        }
    } else {
        echo "No Token";
    }
    print_r(json_encode($data));
    $conn->close();
    
?>

推荐答案

要么用户没有数据库所需的权限,要么密码错误.

Either the user does not have the required rights for the database or the password is wrong.

否则删除用户并使用以下语句创建:

Otherwise remove the user and use the following statement to create:

1.CREATE USER 'user'@'localhost' IDENTIFIED BY '123456789';
2.
3.GRANT ALL PRIVILEGES ON project.* TO 'user'@'localhost' WITH GRANT OPTION;

或者试试这个:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);

echo "Yaaay";

这篇关于连接失败:用户 ''username'@'localhost' 访问被拒绝(使用密码:YES)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)