我正在尝试连接到服务器上的mysql,但会出现以下错误。创建用户并授予所有权限。
它在本地计算机上工作,但在部署到服务器之后却没有。
同样 mysql_connect 功能有效,但 mysqli ()会拒绝访问,如下所述。
还尝试添加端口。
请帮助。
帮助将不胜感激。 谢谢,
警告:mysqli :: mysqli():( HY000 / 1045):在/ home / domainname / public_html / autopublish / test中拒绝用户''usernam'@'localhost'(使用密码:YES) .php在第8行 连接失败:访问被拒绝用户''用户名'@'localhost'(使用密码:是)
<?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();
?>
答案 0 :(得分:2)
用户没有数据库所需的权限或密码错误。
否则删除用户并使用以下语句创建:
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
$conn = new mysqli($servername, $username, $password, $dbname);
//Check
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Yaaay";
?>