I'm trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).
Yesterday it worked fine, and now I suddenly get the error in the title.
The script works when I use my browser to run it (I have XAMPP installed), but Terminal refuses to connect to the DB.
Here is the file that I include to connect (the script works when I don't include this, but then it doesn't connect to the DB):
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("FNB1C_data") or die(mysql_error());
?>
That should work, since it works with my browser.
The command I use at the Terminal is php scriptname.php
.
The reason is that php cannot find the correct path of
mysql.sock
.Please make sure that your mysql is running first.
Then, please confirm that which path is the
mysql.sock
located, for example/tmp/mysql.sock
then add this path string to php.ini:
Finally, restart Apache.
I also had this error, but could only fix it through the suggestion here.
To summarize, use:
Instead of:
The reason is that "localhost" is a special name for the MySQL driver making it use the UNIX socket to connect to MySQL instead of the a TCP socket.
Another solution is to fix the socket location in the php.ini configuration file like this:
Of course, the symlink works too, so its a matter of preference which one you change.
MySQL socket is located, in general, in
/tmp/mysql.sock
or/var/mysql/mysql.sock
, but probably PHP looks in the wrong place.Check where is your socket with:
When the updatedb is terminated:
Then locate your php.ini:
this will output something like:
Edit your php.ini
Change the lines:
where /tmp/mysql.sock is the path to your socket.
Save your modifications and exit ESC + SHIFT: x
Restart Apache
Mac OS X EL Capitan + MAMP Pro Do this
Then do this
Hope this saves you some time.
When you install php53-mysql using port it returns the following message which is the solution to this problem: