This question was first posted to Stack Overflow, but as it is perhaps just as much a server issue I though it might be just as well to post it here also.
I have just installed and configured Apache, MySQL, PHP and phpMyAdmin on my Macbook in order to have a local development environment. But after I moved one of my projects over to the local server I get a weird MySQL error from one of my calls to mysql_query():
Access denied for user '_securityagent'@'localhost' (using password: NO)
First of all, the query I'm sending to MySQL is all valid, and I've even testet it through phpMyAdmin with perfect result. Secondly, the error message only happens here while I have at least 4 other mysql connections and queries per page. This call to mysql_query() happens at the end of a really long function that handles data for newly created or modified articles. This basically what it does:
- Collect all the data from article form (title, content, dates, etc..)
- Validate collected data
- Connect to database
- Dynamically build SQL query based on validated article data
- Send query to database before closing the connection
Pretty basic, I know. I did not recognize the username "_securityagent" so after a quick search I came across this from and article at Apple's Developer Connection talking about some random bug:
Mac OS X's security infrastructure gets around this problem by running its GUI code as a special user, "_securityagent".
Then I tried put a var_dump() on all variables used in the mysql_connect() call, and every time it returns the correct values (where username is not "_securityagent" of course). Thus I'm wondering if anyone has any idea why 'securityagent' is trying to connect to my database - and how I can keep this error from occurring when I call mysql_query().
Update: Here is the exact code I'm using to connect to the database. But a little explanation must follow:
- The connection error happens at a call to mysql_query() in function X in class_1
- class_1 uses class_2 to connect to database
- class_2 reads a config file with the database connection variables (host, user, pass, db)
- class_2 connect to the database through the following function:
var $SYSTEM_DB_HOST = "";
function connect_db() {
// Reads the config file
include('system_config.php');
if (!($SYSTEM_DB_HOST == "")) {
mysql_connect($SYSTEM_DB_HOST, $SYSTEM_DB_USER, $SYSTEM_DB_PASS);
@mysql_select_db($SYSTEM_DB);
return true;
} else {
return false;
}
}
How are you calling
mysql_query()
? I notice that you don't save the connection handle from themysql_connect()
call. It looks to me like the failingmysql_query()
call is not using the connection you're making.I recommend that you revise your code to do something like this:
This will ensure that the connection you make with
mysql_connect
is in fact the one that is used for the query. See if that changes anything; even if it doesn't, personally I think it's good practice to explicitly tell MySQL which DB connection you want to use.You should be passing your credentials on mysql_connect.
The call to
mysql_query()
might be picking up the wrong connection, or trying to open a new connection; try saving the connection resource from themysql_connect()
and explicitly pass it tomysql_query()
. Better yet, consider updating to a more modern driver, such as mysqli or PDO. As a separate test (and in another script), try issuing amysql_connect()
andmysql_query()
with no credentials and see if you get the same error.Have you verified that that user is able to perform queries? If you can log into phpmyadmin as a root user then verify that user _sercurityagent has the rights to perform queries on:
Then make sure that on each of those listed give the user rights to what you think they will need.
You should then be able to perform a query then.
Try something like: