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
.
I got the same errors. Mysql was running as a standalone application before I started phpMyAdmin.
I just stopped mysql Then sudo /Applications/XAMPP/xamppfiles/xampp stop sudo /Applications/XAMPP/xamppfiles/xampp start
It worked fine
I just had this problem, but it only appeared when loading certain pages (other pages worked fine). It turned out that I was making calls to MySQL after I closed the connection with
mysql_close()
. So, as @brucenan said: make sure that MySQL is running when you call it.I was having the same problem and this is how I fixed it:
I had this and it didn't work:
I did this and it worked:
Instead of using the mysql server, I connected directly to the Unix Socket. Worked for me.
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/
I am on XAMPP on Mac OS X, and Brian Lowe's solution above worked with a slight modification.
The mysql.sock file is actually in "/Applications/xampp/xamppfiles/var/mysql/" folder. So had to link it up both in /tmp and /var/mysql. I haven't checked which one is used by PHP command line, but this did the fix, so I am happy :-)
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.
and then
source: http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/