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
.
You can do it by simply aliasing the MAMP php on Apple terminal:
Example:
> phpmamp - v
Now you can run something like:
> phpmamp scriptname.php
Note: This will be applied only for the current terminal session.
When you face the following issue:
Set "mysql.default_socket" value in your
/etc/php.ini
toThen restart web service in server admin
Since your might use MAMP, either change your Port to the default 3306 or use 127.0.0.1 in the database.php
Or with the default settings:
For some reason mysql on OS X gets the locations of the required socket file a bit wrong, but thankfully the solution is as simple as setting up a symbolic link.
You may have a socket (appearing as a zero length file) as
/tmp/mysql.sock
or/var/mysql/mysql.sock
, but one or more apps is looking in the other location for it. Find out with this command:Rather than move the socket, edit config files, and have to remember to keep edited files local and away from servers where the paths are correct, simply create a symbolic link so your Mac finds the required socket, even when it's looking in the wrong place!
If you have
/tmp/mysql.sock
but no/var/mysql/mysql.sock
then...If you have
/var/mysql/mysql.sock
but no/tmp/mysql.sock
then...You will need permissions to create the directory and link, so just prefix the commands above with sudo if necessary.