MySQL connection error
June 15, 2009 1:09 PM   Subscribe

MySQL connection error.

This is at my work. IT will create and setup a MySQL db then hand of backups and maintenance to you. They give you the dbname, username, password, and server it is on.

I went through this 5 years ago. Accessing it from command line is no problem, but a php file will give access denied to apache@localhost in browser. The IT fix back then was to add user apache@localhost, and then web pages could access the db.

I just had a new db setup, and I'm back to web pages being denied access. I explained this had happened before and how it was handled. The IT folks I dealt with before are no longer here, and the replacement is asking me why the web pages are using apache@localhost instead of what I was given. IT has not dealt with this before. I have asked for apache@localhost to be added as a user, but I do not know how to explain that the user/passw I was given is also being used.

How do I tell IT what is going on with web page access vs commandline access? Why do web pages show as apache@localhost? Is adding user apache@localhost the correct fix?
posted by sailormouth to Computers & Internet (12 answers total) 1 user marked this as a favorite
'Access denied for apache@localhost' is an error which tells us you're not feeding the correct arguments to mysql_connect() in PHP.

They've given you a username and password and hostname for use with MySQL.

For what possible reason would you decide you don't want to use them?
posted by genghis at 1:31 PM on June 15, 2009

To second genghis on preview:

You should be able to use the username and password provided by IT without any problem.

If you've been seeing errors that mention apache@localhost, the most likely cause is that you're not providing a username when you try to establish the connection. apache@localhost is simply the user who owns the apache service; mysql will default to this user if no other user is specified when you make a connection, since the request is coming from the web server.

So the best solution might be to solve the problem of why your MySQL server isn't getting the correct login details rather than fudging things so that the default user has permissions, because that route will compromise application security.
posted by le morte de bea arthur at 1:33 PM on June 15, 2009

Your PHP file is using the use apache is running as to MySQL as the MySQL account name. You need to specify the username of the MySQL account somehow. Who created this script and can you change it or change its configuration somehow?
posted by mkb at 1:38 PM on June 15, 2009

I have used all the connection details IT provided me.
I can connect command line and I have cronjobs to do backups that also work.

My old db, which they added apache@localhost as a user, will be denied access if I purposely remove host, user, or passw.

This is the simple file I put together:
$query="SELECT name FROM test_table";
$db = mysql_connect("host", "user","passw");
$qry_result = mysql_query($query);
while($row = mysql_fetch_array($qry_result)){

posted by sailormouth at 1:56 PM on June 15, 2009

Your PHP file is using the use apache is running as to MySQL as the MySQL account name.
What? After reading this sentence about 5 times, I think it's trying to say something like "Your PHP file is passing the apache user to MySQL as the MySQL account name."
I have used all the connection details IT provided me.
I can connect command line and I have cronjobs to do backups that also work.
Is your apache webserver running on the same machine as the mysql database? MySQL differentiates accounts based on where you're connecting from, so if you have server A running MySQL and server B running apache+mod_php, you need to have your IT people grant connect to user@B (not user@locahost).
posted by axiom at 2:19 PM on June 15, 2009

The apache@localhost error is deceptive but seems to be generated a lot following some other connection error - I'm not sure if maybe when the connection fails PHP tries to default to 'apache' with no password, but a google search seems to indicate that it shows up all the time. You might check to see if there is any more informative error in the logs.
posted by dreadpiratesully at 2:40 PM on June 15, 2009

Axiom - yes 3 different servers, newMySQL db, oldMySQL db, and web pages. These are on our intranet if that adds to the complexity at all.
posted by sailormouth at 2:44 PM on June 15, 2009

OK, so you have 3 machines. I'm going to assume that they have these made-up intranet IPs; you'll have to replace them with your actual values:


You need to have someone with the appropriate admin access issue the following two commands:

grant [privileges] on yourdb.* to user@webpages;
grant [privileges] on yourdb.* to user@'';

where [privileges] is a list of your privileges (e.g., select, create, insert, update, index). Of course, they need to replace yourdb, user, '' with the appropriate values.
posted by axiom at 3:27 PM on June 15, 2009

Addendum: Your IT people should know what the privileges you need are, and should also know what the IP of the webserver is.
posted by axiom at 3:29 PM on June 15, 2009

Sorry, I typed too fast :(
posted by mkb at 3:48 PM on June 15, 2009

Thank you axiom for the very clear explanation. Now time to wait to hear back from IT.
posted by sailormouth at 4:12 PM on June 15, 2009

Just a follow up. It took two more weeks to resolve the issues. I was told by another IT guy that the working db and webfiles were on the same server. He said that server was running such old versions of php and mysql that that was the connection issue to the new db from the webfiles. They migrated the old db to the server with the new db and upgraded it. They also set me up with new storage space and url on another server for my webfiles. Now everything plays nicely together.
Thanks to everyone for your input.
posted by sailormouth at 8:44 AM on July 8, 2009

« Older I just found out my foot pain is being caused by...   |   What is the best eye protection for tennis players... Newer »
This thread is closed to new comments.