2

I am trying to set up a PHP framework on my Mac 10.6 computer, and it keeps erroring during setup due to database connectivity issues. So I put together a small php script to see if I could connect to the server myself, and it couldn't either. The operation times out.

However, I am able to login to mysql from commandline perfectly fine. It's only PHP that is having these connection issues. Does anyone have any ideas?

Here is the script.

<?php
  $connection = mysql_connect("http://localhost", "root", "");
if( $connection ) {
  mysql_close( $connection );
  die('TRUE');
}else {
  die('Could not connect: ' . mysql_error());
}

?>

EDIT: I tried removing http:// but then I get a "No such file error."

PHP Warning:  mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/newuser/Downloads/t.php on line 2
PHP Warning:  mysql_connect(): No such file or directory in /Users/newuser/Downloads/t.php on line 2
Could not connect: No such file or directory
picardo
  • 24,530
  • 33
  • 104
  • 151

4 Answers4

3

You are using this :

mysql_connect("http://localhost", "root", "");

But your MySQL host is not http://localhost : it's probably localhost.


Try to remove that http://, using this :

mysql_connect("localhost", "root", "");


As a sidenote : working with the root account is not a good practice -- even on a development platform.


EDIT after the comment : In that case, try with 127.0.0.1 (the IP address of localhost)


And if you really want to use Unix sockets, you'll have to find which socket is used by your MySQL server (it should be indicated in its configuration) ; for instance, in my /etc/mysql/my.cnf, I have :

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock

Then, you'll want PHP to use the same one :

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I tried localhost but I get "No such file or directory error." Adding that to the post now. – picardo Mar 04 '11 at 17:48
  • @picardo : I'ev edited my answer, to give another hint, that should solve that problem -- using a TCP connection, instead of an Unix socket. – Pascal MARTIN Mar 04 '11 at 17:55
  • @pascal: the framework I'm installing uses http:// internally, though. So the script connects fine with 127.0.0.1, but the framework still fails...Do you know how I can make mysql_connect work with http://? – picardo Mar 04 '11 at 17:59
  • @picardo : re-edited to add something about sockets. Using `http` *(which is the protocol of the web)* feels very wrong, when it comes to connecting to a MySQL database, actually... – Pascal MARTIN Mar 04 '11 at 18:00
  • @pascal: I tried the my.cnf file method. Also edited my php.ini to point to `/tmp/mysql.socket` but none of them worked. (If you excuse me, I will proceed to bang my head on the wall now.) – picardo Mar 04 '11 at 19:27
  • Humph, sorry, I'm starting to get out of ideas :-( ; good luck with the wall, though ^^ – Pascal MARTIN Mar 04 '11 at 19:28
1

Take the "http://" out of the mysql_connect parameter. It wants a host, not an HTTP URL.

nobody
  • 19,814
  • 17
  • 56
  • 77
1

Remove http:// and use host name localhost only.

Barry Kaye
  • 7,682
  • 6
  • 42
  • 64
hfcorriez
  • 112
  • 8
1

Use "127.0.0.1" it will force using TCP4 insted of sockets.

If you want to use sockets, according to this document the default socket on Mac OS X is:

/tmp/mysql.sock

Fot this to be default you should edit your php confiuration or have an .htaccess file with:

php_value mysql.default_socket "/tmp/mysql.sock"
vbence
  • 20,084
  • 9
  • 69
  • 118
  • That worked. But do you know how to make it with sockets? The framework I'm installing -- Ushahidi -- connects through sockets, I think, and install keeps failing, which is why I was attempting this in the first place. – picardo Mar 04 '11 at 17:55