PHP & MySQL on Mac OS X: Access denied for GUI user
- by Eirik Lillebo
Hey!
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;
}
}