“连接失败:用户“root”@“localhost”访问被拒绝; (使用密码:YES)”来自 php 函数
我编写了一些 php 网页使用的函数,以便与 mysql 数据库交互。当我在服务器上测试它们时,出现此错误:
"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)"
我可以在我的电脑上使用它们(使用 XAMPP),并且可以使用服务器中的命令行浏览数据库表。但网页无法连接。我检查了密码,但没有结果。这是正确的(否则我无法从命令行登录mysql)。
该函数的调用如下:
$conn = new mysqli("localhost", "root", "password", "shop");
我必须在我的服务器中设置一些东西吗?谢谢
编辑: PHP版本5.3.3-7+squeeze1 MySQL版本:5.1.49-3 都在 debian 上
I wrote some function used by a php webpage, in order to interact with a mysql database. When I test them on my server I get this error:
"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)"
I am able to use them on my pc (using XAMPP) and I can navigate through the tables of the database using the command line in the server. However, the webpage fails to connect. I've checked the password but with no results. It's correct (otherwise I could not log in to mysql from the command line).
The call of the function is the following:
$conn = new mysqli("localhost", "root", "password", "shop");
Do I have to set something in my server? Thanks
Edit:
PHP version 5.3.3-7+squeeze1
mysql version: 5.1.49-3
both on debian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我是这样解决的:
我使用root用户名登录
我创建了一个新用户
然后我创建了数据库
我为该数据库的新用户授予了权限
然后我注销了root并登录了新用户
我使用脚本重建了我的数据库
就是这样..现在从php 在调用时没有问题,
感谢大家的宝贵时间:)
I solved in this way:
I logged in with root username
I created a new user with
then I created the database
I granted privileges for new user for this database
Then I logged out root and logged in new user
I rebuilt my database using a script
And that's it.. Now from php works without problems with the call
Thanks to all for your time :)
数据库中是否有 root@localhost 的用户帐户条目?在MySQL中,您可以按主机设置不同的用户帐户权限。可能有多个不同的帐户具有相同的名称以及它们连接的主机。最常见的是 [email protected] 和 root@localhost。这些可以有不同的密码和权限。确保 root@localhost 存在并且具有您期望的设置。
根据您的解释,我愿意打赌这就是问题所在。从另一台电脑连接使用与 root@localhost 不同的帐户,我认为命令行使用 [电子邮件受保护]。
Is there a user account entry in the DB for root@localhost? In MySQL you can set different user account permissions by host. There could be several different accounts with the same name combined with the host they are connecting from. The most common are [email protected] and root@localhost. These can have different passwords and permissions. Make sure root@localhost exist and has the settings you expect.
I am willing to bet, based on your explanation, that this is the problem. Connecting from another PC uses a different account than root@localhost and the command line I think connects using [email protected].
从您到目前为止所说的来看,这听起来像是 PHP5.3 中的 MySQL 驱动程序无法连接到较旧的 MySQL 版本 4.1 的问题。查看 http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx
这里有一个类似的问题,有一些有用的答案,无法使用旧身份验证连接到 MySQL 4.1+
From what you've said so far, it sounds like a problem where the MySQL driver in PHP5.3 has trouble connecting to the older MySQL version 4.1. Have a look on http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx
There's a similar question here with some useful answers, Cannot connect to MySQL 4.1+ using old authentication
我也有这个问题。但这是因为另一个原因。
我的密码以字符
$
开头...例如$MyPassword
我已将其更改为
#MyPassword
,问题已解决。I had this problem too. But this was because of another reason.
My password began with character
$
... e.g.$MyPassword
I've changed it to
#MyPassword
and the problem is solved.简单的。
打开xampp控制面板->配置-> my.ini 用记事本编辑。现在将其添加到[mysqld]下面
保存。启动apache和mysql。
我希望对你有帮助
Easy.
open xampp control panel -> Config -> my.ini edit with notepad. now add this below [mysqld]
Save. Start apache and mysql.
I hope help you
也许在这里?
我认为代码应该是:
$connect = new mysqli("host", "root", "", "dbname");
因为root没有密码。
(使用密码:YES)
表示“您正在使用此用户的密码”Here maybe?
I believe that the code should be:
$connect = new mysqli("host", "root", "", "dbname");
because root does not have a password. the
(using password: YES)
is saying "you're using a password with this user"提示:
如果无法连接到服务器,请注释掉pid和socket文件。
mysql 可能使用不正确的 pid 和/或套接字文件进行连接,
因此,当您尝试通过命令行连接到服务器时,它“看起来”
在不正确的 pid/socket 文件...
TIP:
comment out pid and socket file, if you can't connect to server..
mysql could be connecting with incorrect pid and/or socket file,
so when you try to connect to server trough command-line it "looks"
at the incorrect pid/socket file...
最有可能的是它没有正确识别您的用户。根@localhost。
选择您的 MAMP 正在使用的 Mysql 版本。
/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql
我实际上在我的计算机上写了一个别名,这样我就不必记住如何到达 bin 目录。
alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'
这允许我运行这个:
mysqldumpMAMP -uroot -p db_name > test_db_dump.sql
您可以在 bash 配置文件
~/.bash_profile
或~/.bash_rc
中保存别名Most likely it is not identifying your user properly. root@localhost.
Select the Mysql version your MAMP is using.
/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql
I actually wrote an alias on my computer so I don't have to remember how to get to the bin directory.
alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'
Which allows me to run this:
mysqldumpMAMP -uroot -p db_name > test_db_dump.sql
You can save an alias in your bash profile
~/.bash_profile
or~/.bash_rc
可能未设置密码并且您将密码作为参数传递。
尝试在连接期间省略密码参数..
May be there password is not set and you are passing password as argument.
Try ommitting password argument during connection..
编辑您在 PHPmyAdmin (WAMP) 上的权限,请注意,您的密码和用户名尚未创建。因此,请创建或编辑它,以便它可以与 php.ini 中的 sql 连接一起使用。
希望它有效
Edit your privileges on PHPmyAdmin (WAMP), Note that your password and user name has not been created. So do create or edit it, that it might work with your sql connection in your php.
Hope it works
尝试初始化变量并在连接对象中使用它们:
Try initializing your variables and use them in your connection object: