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
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.
The mySQL client by default attempts to connect through a local file called a socket instead of connecting to the loopback address (127.0.0.1) for localhost.
The default location of this socket file, at least on OSX, is /tmp/mysql.sock
.
QUICK, LESS ELEGANT SOLUTION
Create a symlink to fool the OS into finding the correct socket.
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp
PROPER SOLUTION
Change the socket path defined in the startMysql.sh
file in /Applications/MAMP/bin
.
I was having the same problem and this is how I fixed it:
I had this and it didn't work:
$con = mysql_connect('localhost', 'root', '1234');
I did this and it worked:
$con = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'root', '1234');
Instead of using the mysql server, I connected directly to the Unix Socket. Worked for me.
Mac OS X EL Capitan + MAMP Pro Do this
cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
Then do this
cd /tmp
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
Hope this saves you some time.
i was having the same issue
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
[ErrorException] Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in …htdocs/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
So the solution is to make a symlink to the sock file thus resolving the issue. Do the following to resolve it:
$ sudo mkdir /private/var/mysql/
$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/var/mysql/mysql.sock
source:http://www.reecefowell.com/2012/07/21/symfony2-cli-does-not-connect-to-mysql-while-browser-works-fine/
Fix the looming 2002 socket error – which is linking where MySQL places the socket and where OSX thinks it should be, MySQL puts it in /tmp and OSX looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.
sudo mkdir /var/mysql
and then
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
source: http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/