PHP MySQL 查询中的 MySQL/Apache 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2900/
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
MySQL/Apache Error in PHP MySQL query
提问by Mesidin
I am getting the following error:
我收到以下错误:
Access denied for user 'apache'@'localhost' (using password: NO)
用户 'apache'@'localhost' 访问被拒绝(使用密码:NO)
When using the following code:
使用以下代码时:
<?php
include("../includes/connect.php");
$query = "SELECT * from story";
$result = mysql_query($query) or die(mysql_error());
echo "<h1>Delete Story</h1>";
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)){
echo '<b>'.$row[1].'</b><span align="right"><a href="../process/delete_story.php?id='.$row[0].'">Delete</a></span>';
echo '<br /><i>'.$row[2].'</i>';
}
}
else {
echo "No stories available.";
}
?>
The connect.phpfile contains my MySQL connect calls that are working fine with my INSERTqueries in another portion of the software. If I comment out the $result = mysql_queryline, then it goes through to the else statement. So, it is that line or the content in the if.
该connect.php文件包含我的 MySQL 连接调用,这些调用与我INSERT在软件的另一部分中的查询正常工作。如果我注释掉该$result = mysql_query行,那么它会通过 else 语句。所以,它是那一行或 if 中的内容。
I have been searching the net for any solutions, and most seem to be related to too many MySQL connections or that the user I am logging into MySQL as does not have permission. I have checked both. I can still perform my other queries elsewhere in the software, and I have verified that the account has the correct permissions.
我一直在网上寻找任何解决方案,大多数似乎与 MySQL 连接过多或我登录 MySQL 的用户没有权限有关。我已经检查了两个。我仍然可以在软件的其他地方执行我的其他查询,并且我已经验证该帐户具有正确的权限。
采纳答案by dragonmantank
And if it matters at all, apache@localhost is not the name of the user account that I use to get into the database. I don't have any user accounts with the name apache in them at all for that matter.
如果这很重要,apache@localhost 不是我用来进入数据库的用户帐户的名称。就此而言,我根本没有任何名称为 apache 的用户帐户。
If it is saying 'apache@localhost' the username is not getting passed correctly to the MySQL connection. 'apache' is normally the user that runs the httpd process (at least on Redhat-based systems) and if no username is passed during the connection MySQL uses whomever is calling for the connection.
如果它说 'apache@localhost' 用户名没有正确传递到 MySQL 连接。'apache' 通常是运行 httpd 进程的用户(至少在基于 Redhat 的系统上),如果在连接过程中没有传递用户名,MySQL 使用调用连接的任何人。
If you do the connection right in your script, not in a called file, do you get the same error?
如果您在脚本中进行连接,而不是在调用的文件中进行连接,您是否会遇到相同的错误?
回答by Piskvor left the building
Change the include() to require(). If the "connect.php" file can't be require()d, the script will failwith a fatal error, whereas include() only generates a warning. If the username you're passing to mysql_connect() isn't "apache", an incorrect path to the connect script is the most common way to get this type of error.
将 include() 更改为 require()。如果“connect.php”文件不能被 require()d,脚本将失败并出现致命错误,而include() 只会生成警告。如果您传递给 mysql_connect() 的用户名不是“apache”,则连接脚本的错误路径是导致此类错误的最常见方法。
回答by Piskvor left the building
Dude the answer is a big DUH! which unfortunately it took me a while to figure out as well. You probably have a function like dbconnect() and you are using variables from an include file to make the connection. $conn = mysql_connect($dbhost, $dbuser, $dbpass).
伙计,答案是一个很大的 DUH!不幸的是,我也花了一段时间才弄清楚。您可能有一个像 dbconnect() 这样的函数,并且您正在使用包含文件中的变量来建立连接。$conn = mysql_connect($dbhost, $dbuser, $dbpass)。
Well since this is inside a function the variables from the include file need to be passed to the function or else the function will not know what $dbhost, $dbuser and $dbpass is. A way to fix this is to make those variables global so your functions can pick them up. Another solution which is not very secure would be to write out you host, user and pass in the mysql_connect function.
因为这是在函数内部,所以需要将包含文件中的变量传递给函数,否则函数将不知道 $dbhost、$dbuser 和 $dbpass 是什么。解决此问题的一种方法是将这些变量设为全局变量,以便您的函数可以获取它们。另一个不太安全的解决方案是写出您的主机、用户并传入 mysql_connect 函数。
Hope this helps but I had the same problem.
希望这会有所帮助,但我遇到了同样的问题。
回答by Zak
Don't forget to check your database error logs. You should be able to see if you are even hitting the DB. If you aren't, you should check your firewall rules on the box. On a linux box you can run iptables -L to get the firewall list rules.
不要忘记检查您的数据库错误日志。你应该能够看到你是否甚至击中了数据库。如果不是,则应检查框上的防火墙规则。在 linux 机器上,您可以运行 iptables -L 来获取防火墙列表规则。
Otherwise it will be a pure access issue. Do a "select * from mysql.user" to see if the apache user is even set up in there. Further, I would recommend creating an account specifically for your app as opposed to using apache, since any other app you create will run as apache by default, and could get unauthorized access to your db.
否则,这将是一个纯粹的访问问题。执行“select * from mysql.user”以查看是否在其中设置了 apache 用户。此外,我建议您专门为您的应用程序创建一个帐户,而不是使用 apache,因为您创建的任何其他应用程序默认都将作为 apache 运行,并且可能会未经授权访问您的数据库。
Just look up "GRANT" in the documentation @ dev.mysql.com to get more info. If you have more specific questiosn regarding db, just edit your question, and i will take a look.
只需在文档@ dev.mysql.com 中查找“GRANT”即可获取更多信息。如果您对 db 有更具体的问题,只需编辑您的问题,我会看看。
回答by KernelM
Does the connect.php script actually make the connection or does it just define a function you need to call to create a connection? The error you're getting is symptomatic of not having a previously established connection at all.
connect.php 脚本是否真的建立了连接,还是只是定义了一个您需要调用以创建连接的函数?您遇到的错误是根本没有先前建立的连接的症状。
ETA: Also change the include to a require. I suspect it's not actually including the file at all. But include can fail silently.
ETA:还将包含更改为需要。我怀疑它实际上根本不包含该文件。但是包含可能会默默地失败。
回答by KernelM
You can do one of the following:
您可以执行以下操作之一:
- Add the user "apache" and setup its privileges from phpmyadmin or using mysql on a shell
- Tell php to run
mysql_connectas another user, someone who already has the privileges needed (but maybe not root), look for mysql.default_user in your php.ini file.
- 添加用户“apache”并从 phpmyadmin 或在 shell 上使用 mysql 设置其权限
- 告诉 php 以
mysql_connect另一个用户身份运行,该用户已经拥有所需的权限(但可能不是 root),在您的 php.ini 文件中查找 mysql.default_user。
回答by MoonJoose
Did you remember to do:
你是否记得这样做:
flush privileges;
If the user is not set up then it will give the 'apache'@'localhost' error.
如果用户未设置,则会出现 'apache'@'localhost' 错误。
回答by Teifion
Just to check, if you use justthis part you get an error?
只是为了检查一下,如果你只使用这部分,你会得到一个错误吗?
<?php
include("../includes/connect.php");
$query = "SELECT * from story";
$result = mysql_query($query) or die(mysql_error());
If so, do you still get an error if you copy and paste one of those Inserts into this page, I am trying to see if it's local to the page or that actual line.
如果是这样,如果您将这些插入内容之一复制并粘贴到此页面中,您是否仍然会收到错误消息,我正在尝试查看它是页面本地还是实际行。
Also, can you post a copy of the connection calls (minus passwords), unless the inserts use exactly the same syntax as this example.
此外,您能否发布连接调用的副本(减去密码),除非插入使用与此示例完全相同的语法。
回答by Justin Bennett
Does the apache user require a password to connect to the database? If so, then the fact that it says "using password: NO" would lead me to believe that the code is trying to connect without a password.
apache 用户是否需要密码才能连接到数据库?如果是这样,那么它说“使用密码:否”的事实会让我相信代码试图在没有密码的情况下进行连接。
If, however, the apache user doesn't require a password, a double-check of the permissions may be a good idea (which you mentioned you already checked). It may still be beneficial to try executing something like this at a mysql prompt:
但是,如果 apache 用户不需要密码,则仔细检查权限可能是一个好主意(您提到您已经检查过)。尝试在 mysql 提示符下执行这样的操作可能仍然有益:
GRANT ALL PRIVILEGES ON `*databasename*`.* to 'apache'@'localhost';
That syntax should be correct.
该语法应该是正确的。
Other than that, I'm just as stumped as you are.
除此之外,我和你一样难受。
回答by NilObject
If indeed you are able to insert using the same connection calls, your problem most likely lies in the user "apache" not having SELECT permissions on the database. If you have phpMyAdmin installed you can look at the permissions for the user in the Privileges pane. phpMyAdmin also makes it very easy to modify the permissions.
如果您确实能够使用相同的连接调用进行插入,那么您的问题很可能在于用户“apache”没有对数据库的 SELECT 权限。如果您安装了 phpMyAdmin,您可以在“权限”窗格中查看用户的权限。phpMyAdmin 还使修改权限变得非常容易。
If you only have access to the command line, you can check the permissions from the mysql database.
如果您只能访问命令行,则可以从 mysql 数据库检查权限。
You'll probably need to do something like:
您可能需要执行以下操作:
GRANT SELECT ON myDatabase.myTable TO 'apache'@'localhost';
GRANT SELECT ON myDatabase.myTable TO 'apache'@'localhost';

