PHP - 连接失败:用户“用户名”@“本地主机”的访问被拒绝(使用密码:是)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38159930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Connection failed: Access denied for user 'username'@'localhost' (using password: YES)
提问by SumIt Sahu
I am facing this error. tried all solutions given in other threads...creating a new user@localhost and grant privileges to it. But nothing works for me...
我正面临这个错误。尝试了其他线程中给出的所有解决方案......创建一个新的 user@localhost 并授予它特权。但对我来说没有任何作用......
still getting this error..
仍然收到此错误..
Connection failed: Access denied for user 'username'@'localhost' (using password: YES)
连接失败:用户“用户名”@“本地主机”的访问被拒绝(使用密码:是)
here is my code..
这是我的代码..
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else {
echo "Connection Successful";
}
mysqli_close($conn);
?>
回答by BeetleJuice
1 of 2 things is probably happening
2 件事情中的 1 件可能正在发生
- Your password is incorrect
username
is not authorized to access that database
- 您的密码不正确
username
无权访问该数据库
What do you get when you execute execute following query
执行以下查询时会得到什么
FLUSH PRIVILEGES; SHOW GRANTS;
You can probably find your problem here.
Update July 4,2016
2016 年 7 月 4 日更新
I suspect that the user's password was stored incorrectly. Try resetting the password (you may need to be connected as the root user for this to work):
我怀疑用户的密码存储不正确。尝试重置密码(您可能需要以 root 用户身份连接才能正常工作):
ALTER USER 'username'@'localhost'
-> IDENTIFIED BY 'cleartext_password';
If that doesn't work you could try the older syntax:
如果这不起作用,您可以尝试使用旧语法:
SET PASSWORD FOR 'username'@'localhost' = PASSWORD('cleartext_password');
Replace cleartext_password
with the password you wish to use. Then try to connect again.
替换cleartext_password
为您要使用的密码。然后再次尝试连接。
Does it work?
它有效吗?
still the same problem :'(
还是一样的问题:'(
Ok, let's start with a fresh new user. Execute the 3 commands below as the root
user.
好的,让我们从一个新用户开始。以root
用户身份执行以下 3 个命令。
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'cleartext_password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
If all 3 commands executed properly, try next to connect to your database as newuser
.
如果所有 3 个命令都正确执行,请尝试 next 以newuser
.
Did it work? If not, what error do you see?
它起作用了吗?如果没有,你看到什么错误?
回答by Manish Patil
Some thing wrong with username or password
用户名或密码有问题
If you are using windows by default username is "root" and password is "" (empty),
如果您使用 Windows,默认用户名是“root”,密码是“”(空),