PHP/mysql requiring 127.0.0.1 on OS X Mavericks
-
21-12-2019 - |
Question
On Mac OS X 10.6, I am able to connect to mysql in php with
$mysql = mysql_connect(localhost,user,password)
However, if I use the same in Mac OS X 10.9 (Mavericks), I get the error message:
Warning: mysql_connect(): No such file or directory
if I use $mysql = mysql_connect("127.0.0.1",user,password)
, it works OK, but I'd rather not make the change everywhere.
I've read on this site that there is a socket issue, but I already have this in php.ini.default: pdo_mysql.default_socket=/tmp/mysql.sock
Any idea on how to make mysql_connect(localhost,user,password) work on 10.9
?
OTHER TIPS
what you need is to edit the hostnames file, but I am a PC user, so I can't walk you with that on mac. Anyway here is what I found and I suppose it is the same as your OS version
Step 1 – Open the Terminal.app
Either by start typing Terminal on the Spotlight, or by going into Applications -> Utilities -> Terminal.
Step 2 – Open the hosts file
Open the hosts by typing on the Terminal that you have just opened:
1$ sudo nano /private/etc/hosts Type your user password when prompted.
Step 3 – Edit the hosts file
The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost). source: http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/
what you need is to write an entry for 127.0.0.1 - localhost if it is not already there.
If you call mysql_connect()
with localhost
as a first argument PHP tries to connect using a UNIX domain socket instead of TCP.
Probably it looks for wrong socket name and/or you mysql only listens on TCP.
You should check your mysql settings (/etc/mysql ? search for socket) and the php mysql.default_socket
config variable. (The pdo_mysql.default_socket
you mentions is used when you connect using pdo.)
For example on my debian:
$ grep socket /etc/mysql/my.cnf
socket = /var/run/mysqld/mysqld.sock
(Otherwise your life would be easier if you use any kind of db abstraction layer, and storing the connection details only once.)