views:

255

answers:

2

Hey!

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 an 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".

So as suggested by Frank in the comments I 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().

+1  A: 

If username is not specified explicitly, MySQL tries to guess it by using name of current system user.

You don't have to accept that, you just need to specify desired username explicitly.

How – that depends how you're connecting. In case of phpMyAdmin it's config.inc.php, add line like:

$cfg['Servers'][0]['user'] = 'Eirik';

(see manual)

porneL
The 'Access denied' error isn't triggered by phpMyAdmin. It happens within my own PHP files, on a simple mysql_query() call. And right before that query, I'm successfully connecting to the MySQL server explicitly specifying the username.
Orolin
That's pretty strange then. Did you try to use connection handle explicitly? Maybe there's another invalid mysql_connect() call between your connect and query (that would break default connection)
porneL
I know there is no other mysql_connect() or mysql_close() call between my call to mysql_query() and connecting to the database. But I haven't saved the connection link earlier .. so I'm working on a rewrite where I store the database link until I want it to close.
Orolin
+1  A: 

Did you set up your local AMP server using a pre-made package, or did you install MySQL, PHP, etc. through the respective OS-specific download packages? Setting up Apache, MySQL, and PHP4/5 can be a real PITA.

If you're having problems with your setup I'd recommend MAMP. It's a nifty all-in-one package that really does the trick. You can still access all the config files you want, and everything is contained in the MAMP folder instead of spread all over the system. If Apple upgrades the pre-installed version of Apache/PHP, your machine-specific config wouldn't be overridden as it would in the case of using pre-installed Apache/PHP.

Mike Meyer
I'm afraid I did it all myself as I wanted to learn how it's done. Asspain was definitely a big part of the process:) If this doesn't resolve itself soon enough, I might just retry with that MAMP you're referring to.
Orolin
Man, I'm sorry you had to go through that! I tried doing a custom local server setup twice, once when I got my MBP, and once when I got my Mac Mini. Both times I gave up and used MAMP. It's a great tool, works right out of the box.Are you accessing the database with a DB abstraction class, or straight-up function calls? Using an abstraction layer might simplify life somewhat. I use EzSQL for database handling. Works great for me (at least for the few times I'm dabbling in PHP anymore).http://www.woyano.com/jv/ezsqlHope you get this problem figured out!
Mike Meyer