连接失败:用户“ username'@'localhost”的访问被拒绝(使用密码:是) [英] Connection failed: Access denied for user ''username'@'localhost' (using password: YES)

查看:929
本文介绍了连接失败:用户“ username'@'localhost”的访问被拒绝(使用密码:是)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到服务器上的mysql,但给出以下错误。创建用户并授予用户所有特权。



它在本地计算机上工作,但在部署到服务器后不起作用。



也可以使用 mysql_connect 函数,但是 new mysqli()会导致访问被拒绝,如下所述。 / p>

也尝试添加端口。


警告:mysqli :: mysqli(): (HY000 / 1045):第8行
中/home/domainname/public_html/autopublish/test.php中用户'usernam'@'localhost'的访问被拒绝(使用密码:是)
连接失败:拒绝访问用户``username'@'localhost'(使用密码:是)




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

//创建连接
$ conn = new mysqli($ servername,$ username,$ password); ** //第8行**
//检查连接
是否($ 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){
//每行的输出数据
while($ row = $ result-> fetch_assoc()) {

}
}否则{
echo No Token;
}
print_r(json_encode($ data));
$ conn-> close();

?>


解决方案

该用户没有权限数据库或密码错误。



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

  1。创建用户'user'@'localhost'由'123456789'标识; 
2.
3.授予项目上的所有特权。*授予用户 @本地主机的权限;

或尝试以下操作:

 <?php 
$ servername = localhost;
$ username =用户名;
$ password =密码;
$ dbname = dbname;
//创建
$ conn =新的mysqli($ servername,$ username,$ password,$ dbname);
//检查
是否($ conn-> connect_error){
die(连接失败:。$ conn-> connect_error);
}
echo Yaaay;
?>


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

Its working on local machine but not after deploying to server.

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

Also Tried adding port.

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;

or try this:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
$conn = new mysqli($servername, $username, $password, $dbname);
//Check
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Yaaay";
?>

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆