“连接失败:用户“root”@“localhost”访问被拒绝; (使用密码:YES)”来自 php 函数

发布于 2024-11-16 20:17:37 字数 466 浏览 6 评论 0原文

我编写了一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

又爬满兰若 2024-11-23 20:17:37

我是这样解决的:
我使用root用户名登录

mysql -u root -p -h localhost

我创建了一个新用户

CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';

然后我创建了数据库

CREATE DATABASE shop;

我为该数据库的新用户授予了权限

GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';

然后我注销了root并登录了新用户

quit;
mysql -u francesco -p -h localhost

我使用脚本重建了我的数据库

source shop.sql;

就是这样..现在从php 在调用时没有问题,

 $conn = new mysqli("localhost", "francesco", "some_pass", "shop");

感谢大家的宝贵时间:)

I solved in this way:
I logged in with root username

mysql -u root -p -h localhost

I created a new user with

CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';

then I created the database

CREATE DATABASE shop;

I granted privileges for new user for this database

GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';

Then I logged out root and logged in new user

quit;
mysql -u francesco -p -h localhost

I rebuilt my database using a script

source shop.sql;

And that's it.. Now from php works without problems with the call

 $conn = new mysqli("localhost", "francesco", "some_pass", "shop");

Thanks to all for your time :)

半夏半凉 2024-11-23 20:17:37

数据库中是否有 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].

梦在夏天 2024-11-23 20:17:37

从您到目前为止所说的来看,这听起来像是 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+

SELECT `User`, `Host`, Length(`Password`) FROM mysql.user

对于使用旧密码的帐户,这将返回 16;对于使用新密码的帐户,这将返回 41(对于根本没有密码的帐户,这将返回 0,您可能也需要处理这些帐户)。
使用 MySQL 前端的用户管理工具(如果有)或

SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges

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

SELECT `User`, `Host`, Length(`Password`) FROM mysql.user

This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well).
Either use the user managements tools of the MySQL front end (if there are any) or

SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges
我们的影子 2024-11-23 20:17:37

我也有这个问题。但这是因为另一个原因。
我的密码以字符 $ 开头...例如 $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.

岁吢 2024-11-23 20:17:37

简单的。

打开xampp控制面板->配置-> my.ini 用记事本编辑。现在将其添加到[mysqld]下面

skip-grant-tables

保存。启动apache和mysql。

我希望对你有帮助

Easy.

open xampp control panel -> Config -> my.ini edit with notepad. now add this below [mysqld]

skip-grant-tables

Save. Start apache and mysql.

I hope help you

你与清晨阳光 2024-11-23 20:17:37

也许在这里?

我认为代码应该是:

$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"

无法回应 2024-11-23 20:17:37

提示:

如果无法连接到服务器,请注释掉pid和socket文件。
mysql 可能使用不正确的 pid 和/或套接字文件进行连接,
因此,当您尝试通过命令行连接到服务器时,它“看起来”
在不正确的 pid/socket 文件...

### comment out pid and socket file, if you can't connect to server..
#pid-file=/var/run/mysql/mysqld.pid #socket=/var/lib/mysql/mysql.sock

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...

### comment out pid and socket file, if you can't connect to server..
#pid-file=/var/run/mysql/mysqld.pid #socket=/var/lib/mysql/mysql.sock
晒暮凉 2024-11-23 20:17:37

最有可能的是它没有正确识别您的用户。根@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

心作怪 2024-11-23 20:17:37

可能未设置密码并且您将密码作为参数传递。
尝试在连接期间省略密码参数..

May be there password is not set and you are passing password as argument.
Try ommitting password argument during connection..

红墙和绿瓦 2024-11-23 20:17:37

编辑您在 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

落在眉间の轻吻 2024-11-23 20:17:37

尝试初始化变量并在连接对象中使用它们:

$username ="root";
$password = "password";
$host = "localhost";
$table = "shop";
$conn = new mysqli("$host", "$username", "$password", "$table");

Try initializing your variables and use them in your connection object:

$username ="root";
$password = "password";
$host = "localhost";
$table = "shop";
$conn = new mysqli("$host", "$username", "$password", "$table");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文