Warning: mysql_connect(): [2002] No such file or d

2018-12-31 14:40发布

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.

16条回答
何处买醉
2楼-- · 2018-12-31 15:05

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

查看更多
低头抚发
3楼-- · 2018-12-31 15:06

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.

查看更多
像晚风撩人
4楼-- · 2018-12-31 15:07

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.

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 15:12

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/

查看更多
听够珍惜
6楼-- · 2018-12-31 15:15

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 :-)

sudo su
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock
mkdir /var/mysql
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock
查看更多
听够珍惜
7楼-- · 2018-12-31 15:17

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/

查看更多
登录 后发表回答