MySQL 连接在同一查询上失败。尝试与默认用户重新连接

发布于 2024-12-19 22:51:51 字数 999 浏览 0 评论 0原文

我在同一查询中收到以下错误: 警告:mysql_query():用户“www-data”@“localhost”访问被拒绝(使用密码:NO)

该错误仅在从任何特定 IP 第一次尝试时发生。刷新页面后,脚本运行正常。更具体地说,我将在一个位置从浏览器运行它,它会出错。如果我刷新页面,问题就解决了。直到我从另一个位置尝试。

据我所知,它失去了与数据库服务器的连接并尝试使用默认用户名重新连接。让我困惑的是它每次都在同一个查询上失败。我们将此称为查询_X。多个查询在 query_X 之前运行,包括选择、插入和更新。 Query_X 如下所示:

UPDATE game_users 
SET status_cd=$tmp_status, 
    expire_date=date_add('$currentExpire', interval $l_license_duration_days day) 
WHERE game_user_id=$l_game_user_id

作为示例,传递的变量为:

$tmp_status = 1; 
$currentExpire = '2011-12-05'; 
$l_license_duration_days = 30; 
$l_game_user_id = 1;

该查询导致连接失败的原因是什么?为什么刷新页面可以解决问题?

仅供参考,我正在使用持久连接。

编辑:我刚刚从昨晚尝试过的 IP 再次尝试。我再次收到错误。这是第一次运行脚本时的情况。我第二次运行该页面后,效果很好。

编辑:第一次在特定浏览器上运行时也会出错,即使它在同一IP上,即在firefox上出错一次然后就好了,在chrome上出错一次然后就没事了,等等。

编辑:我已将其范围缩小到在 query_X 之前发生的 mail() 函数。我仍然不知道为什么邮件功能会关闭 mySQL 连接。我决定将邮件功能(可以正常工作并发送电子邮件)放在 php 文件的末尾,无论如何我都会在其中关闭连接。这是一个 hack,但我已经在这上面花了太多时间了。

I am getting the following error on the same query:
Warning: mysql_query(): Access denied for user 'www-data'@'localhost' (using password: NO)

The error only occurs on the first try from any particular IP. After I refresh the page, the script runs fine. To be more specific, I will run it from a browser in one location, and it will error. If I refresh the page, the problem is fixed. That is until I try from another location.

From what I gather, its losing the connection to the database server and trying to reconnect with the default username. What's confusing me is that it fails on the same query each time. Let's call this query_X. Multiple queries run before query_X including selects, inserts, and updates. Query_X looks like this:

UPDATE game_users 
SET status_cd=$tmp_status, 
    expire_date=date_add('$currentExpire', interval $l_license_duration_days day) 
WHERE game_user_id=$l_game_user_id

As an example, the variables being passed are:

$tmp_status = 1; 
$currentExpire = '2011-12-05'; 
$l_license_duration_days = 30; 
$l_game_user_id = 1;

What is it about this query that makes the connection fail, and why does refreshing the page fix the problem?

Fyi, I'm using persistent connect.

Edit: I just tried again from an ip that I tried last night. And I received the error again. That is on the first run of the script. After I ran the page a second time, it worked fine.

Edit: It will also error the first time it's run on a particular browser, even if it is on the same IP, i.e. it will error once on firefox and then be fine, error on chrome once and then be fine, etc.

Edit: I've narrowed it down to a mail() function that was happening just before query_X. I still don't know why the mail function is closing the mySQL connection. I've decided to put the mail function (which works properly and sends the email) at the end of the php file, where I would close the connection anyways. It's a hack, but I've spent too much time on this already.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无需解释 2024-12-26 22:51:51
  • 看起来您在调用 mysql_query() 之前关闭了连接。访问被拒绝的消息应该发生在 mysql_connect() 阶段。
  • 您确定错误是在查询中而不是在连接/关闭函数中吗?
  • It looks like you closed the connection before some call to mysql_query(). The message of access denied should happen on the mysql_connect() phase.
  • Are you sure the error is in the query and not in the connect / close functions?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文