Jump to content
xisto Community
dzimney

Php Unable To Connect To Mysql Using Localhost

Recommended Posts

I'm running local sandbox on OS X 10.6. Been doing so for quite sometime. Ran an software update and now everything seems to be jacked up.

Here's the basic problem: I'm unable to connect to MySQL using PHP when defining the host as "localhost", which I need in order to keep in sync with the rest of the team.

First, the relavant code.

Here's my test php script:

<?php$usr = "root";$pass = "";$host = $_SERVER['HTTP_HOST'];//connection to the database$dbhandle = mysql_connect($host, $usr, $pass)   or die("Unable to connect to MySQL");  echo "Connected to MySQL";?>

And here's my httpd-vhosts.conf file:
NameVirtualHost *:80<VirtualHost *:80>	ServerName	localhost	DocumentRoot	/Users/admin/Development</VirtualHost><VirtualHost *:80>	ServerName	local	DocumentRoot	/Users/admin/Development</VirtualHost><VirtualHost *:80>	ServerName	test	DocumentRoot	/Users/admin/Development</VirtualHost>

So, with this config, navigating to "127.0.0.1", "local" or "test" into my browser, I'm able to connect to MySQL. The test script returns: "Connected to MySQL".
But, if I enter in "localhost" the script dies giving me the output: "Unable to connect to MySQL".

If I set the hostname to 127.0.0.1 or any VirtualHost ServerName as defined in my httpd-vhosts.conf file, excluding 'localhost', it works. That is to say, in my example PHP script I'm using $_SERVER['HTTP_HOST'] but can also statically set $host to '127.0.0.1', 'local' or 'test' and the script will execute (without error) on all four tests sites (127.0.0.1, localhost, local and test).

And of course statically setting $host to 'localhost' fails across all four.

Any ideas?

Again, I recently ran an update on my machine, which very well could have caused all of these problems. My next step will be to reinstall MySQL. Unfortunately I'm not exactly sure what the updates where. I do know that the issue came about sometime within the past four days as last friday my sandbox was working fine.

Thanks in advance!
Edited by dzimney (see edit history)

Share this post


Link to post
Share on other sites
$host = $_SERVER['HTTP_HOST'];

Do you have any other reason to setup your host to a dynamic one? That was the part where the error was being triggered. When you mention 'after an update', which files got updated and what is this update about?

That area should be 'localhost' or the IP of your MySQL server and have root granted access to localhost which is odd since this is always setup as true on default.

Share this post


Link to post
Share on other sites

I was just using

$host = $_SERVER['HTTP_HOST'];
for testing.

root still has access to localhost in MySQL (according to PhpMyAdmin).

Unfortunately I have no idea what the updates where. And I'm really not even sure if they had anything to do with it. Just the only possible thing I can think of that would have changed.

At this point I've given up on it. Still curious as to what's going on those. Seems like a pretty odd thing to be happening.

Share this post


Link to post
Share on other sites

I would tend to agree there ^^ Try just "localhost" Secondly with the "Or die" statement replace your custom error message with:or die(mysql_error());(i think that's right it's been a while but a syntax highlighted editor will flag it up.)That way you will get the MYSQL server error message out. Might help, might not but i always use it in the testing/development phase (ideally change it back to a custom, non-informative error message for live release)

Share this post


Link to post
Share on other sites

This may be a late reply (kinda busy the past weeks), the only logical thing I can think are:1. you have 2 users defined as root, one of them have an access of localhost and the other of % (all connections). this 2 instance of the account root have different passwords and using the host as localhost will select root with access localhost and using any other value will trigger root instance 2 which is allowed to login to any host (with a different password)this case is possible with MySQL.2. second possibility, for some reason you have blocked root from accessing the server using localhost connection, odd but possible. even if 127.0.0.1 and localhost points to the same machine, to the database system they are 2 unique connections and treated differently.3. the user/privilege table got corrupted.

Share this post


Link to post
Share on other sites

Thank you for the reply. It's an ongoing issue, so it's definitely not too late.

 

At this point, I'm opting for option 3. It's not a big enough issue at this point to do a fresh install of MySQL though. Even still, it is an awfully curious issue and I would like to solve it just for the sack of solving it. In the meantime it works well enough to use 127.0.0.1 for sites in my sandbox.

 

I don't think it's either of the first 2 options you've posted, though I could be wrong.

 

Here's a screenshot of my permissions as provided my phpMyAdmin.

 

Posted Image

 

Thanks again for the efforts team!

Share this post


Link to post
Share on other sites

Posted Image

 


did you consider resetting all the password for all root accounts?

some versions of MySQL does not allow blank passwords for root but on your case, it seems it was accepting a blank password.

 

4. It is also possible base on the screenshot that your server was having conflicts on who will take over for local connection (case between connection strictly for 127.0.0.1 and localhost)

 

I only got the problem related to item #4 on a Fedora OS with high security and no other UNIX/Linux or even windows system have the same issue ever. This is only base on my experience making it non 100% accurate statement.

Share this post


Link to post
Share on other sites

Yeah, the problem with all of that though, is that it all used to work fine. There was a point when everything was cool, root with no password, localhost and 127.0.0.1 as the hostname, and everything was gravy. Also, I'm on OS X, and I'm pretty sure that root defaults to a blank password. I think I changed it at one point, and then back to blank (all of this long before the issue arose).

Share this post


Link to post
Share on other sites

Strange, the only possible solution is to check the changelog on of OSX and MySQL version installed for any issues known. I used to have the problem and I just created a new account with full root access. :DMy problem is not related to a change in the system but on the rule I inserted on the server which I can't take down. The server only recognize logging to all root accounts using the local IP space in the range of 192.168.xxx.xxx excluding 192.168.1.0 - 192.168.199.999. The problem started when my work computer was put behind VPN to connect to Japan and Thailand servers. The fix I have done is to create a new user, give it full privilege and locks that user to be allowed to login with my own IP address.Kind of a cheat, I was reluctant to read changelogs and bug issues to figure out why my IP change regardless if the VPN was initiated or not. NOTE: When the VPN was turned on, I get a new IP and when it was turned off, the IP resets to what is was before the VPN started (any IP as long as it is not on the range of 192.168.xxx.xxx). The other machines maintain the same IP in the local area network range regardless if they have VPN turned on or off.

Edited by vhortex (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.