問題描述
我正在嘗試連接到服務(wù)器上的 MySQL,但出現(xiàn)以下錯誤.用戶已創(chuàng)建并被授予所有權(quán)限.
I am trying to connect to MySQL on the server but gives following error. User is created and granted all privileges.
它可以在本地機器上運行,但在部署到服務(wù)器之后就不行了.
還有 mysql_connect
函數(shù)可以工作,但是 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();
?>
推薦答案
要么用戶沒有數(shù)據(jù)庫所需的權(quán)限,要么密碼錯誤.
Either the user does not have the required rights for the database or the password is wrong.
否則刪除用戶并使用以下語句創(chuàng)建:
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";
這篇關(guān)于連接失敗:用戶 ''username'@'localhost' 訪問被拒絕(使用密碼:YES)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!