4

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:

  1. Collect all the data from article form (title, content, dates, etc..)
  2. Validate collected data
  3. Connect to database
  4. Dynamically build SQL query based on validated article data
  5. 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:

  1. The connection error happens at a call to mysql_query() in function X in class_1
  2. class_1 uses class_2 to connect to database
  3. class_2 reads a config file with the database connection variables (host, user, pass, db)
  4. 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;
}
}

2
  • Could you post the exact line of code you are using to connect to the DB?
    – Bart B
    Jul 30, 2009 at 16:13
  • Done. And as I've mentioned; I've already double checked the $SYSTEM variables right before they are passed to mysql_connect .. and they are correct (without mention of the username "_securityagent").
    – o01
    Jul 30, 2009 at 16:39

5 Answers 5

1

How are you calling mysql_query()? I notice that you don't save the connection handle from the mysql_connect() call. It looks to me like the failing mysql_query() call is not using the connection you're making.

I recommend that you revise your code to do something like this:

function connect_db() {
    // Reads the config file
    include('system_config.php');

    if ($SYSTEM_DB_HOST == "") {
        return false;
    }

    $dbhandle = mysql_connect($SYSTEM_DB_HOST, $SYSTEM_DB_USER, $SYSTEM_DB_PASS);
    if ($dbhandle) {
        @mysql_select_db($SYSTEM_DB, $dbhandle);

        return $dbhandle;
    } else {
        return false;
    }
}

// ... invoke like this:
$dbhanle = connect_db();
if (!$dbhandle) {
    // handle failed DB connection
}

// later, whenever you need to run queries:
$result = mysql_query($querysql, $dbhandle);

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.

0

You should be passing your credentials on mysql_connect.

6
  • As I'm describing in the last paragraph, I'm certain that on all of my calls to mysql_connect() I'm passing the correct credentials with host address, username, password and database schema.
    – o01
    Jul 30, 2009 at 15:49
  • 3
    Since you are not storing the db link returned by mysql_connect, mysql_query could be acting on a different connection. Do a mysql query with "select user(),current_user();" just after mysql connect, and another before the problematic mysql_query and see if they differ. If that's the case, finding the rogue mysql_connect can be another story, but you can always store the link in a variable Jul 30, 2009 at 16:51
  • I did like you said and put a "select user(), current_user();" query right after mysql_connect and echoed the results. Then I put another right before the crashing query in question. The one after mysql_connect() returned the values I was expecting with correct username and address. The other one didn't go through and failed all together with no results.
    – o01
    Jul 30, 2009 at 19:39
  • Here too the mysql_error() output on the second query that fails: "Access denied for user '_securityagent'@'localhost' (using password: NO)". Go figure...
    – o01
    Jul 30, 2009 at 19:41
  • Have you find any other mysql_connect statements? (hint: grep -ri mysql_conect /path/to/your/code) Jul 30, 2009 at 19:53
0

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 the mysql_connect() and explicitly pass it to mysql_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 a mysql_connect() and mysql_query() with no credentials and see if you get the same error.

0

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:

  • localhost
  • ip address of box
  • loopback

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.

0

Try something like:

var $SYSTEM_DB_HOST = "";

function connect_db() {

    // Reads the config file
    include('system_config.php');

    if (!($SYSTEM_DB_HOST == "")) {
        $handle = mysql_connect($SYSTEM_DB_HOST, $SYSTEM_DB_USER, $SYSTEM_DB_PASS);
        @mysql_select_db($SYSTEM_DB,$handle);

        return true;
    } else {
        return false;
    }
}
$handle = connect_db();
mysql_query("QUERY",$handle);
.....
mysql_close($handle);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .