Programing

MySQL 연결이 작동하지 않음 : 2002 해당 파일 또는 디렉토리가 없습니다.

lottogame 2020. 8. 27. 08:08
반응형

MySQL 연결이 작동하지 않음 : 2002 해당 파일 또는 디렉토리가 없습니다.


WordPress를 설정하려고합니다. Apache와 MySQL이 실행 중이고 계정과 데이터베이스가 모두 설정되었습니다. 간단한 연결을 시도했습니다.

<?php
    $conn = mysql_connect('localhost', 'USER', 'PASSWORD');
    if(!$conn) {
        echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();
    }
?>

그리고 나는 항상 이것을 얻습니다.

오류 : 2002-해당 파일 또는 디렉터리가 없습니다.

어떤 파일이나 디렉토리에 대해 이야기 할 수 있습니까?

저는 내장 Apache를 사용하는 OS X Snow Leopard를 사용하고 있습니다. x86_64 dmg를 사용하여 MySQL을 설치했습니다.

업데이트 : 소켓이 /tmp/mysql.sock에 있다는 것을 알았으므로 php.ini에서 잘못된 경로의 모든 발생을 그로 대체했습니다.


Linux를 사용하는 경우 : mysql.sock 파일의 경로가 잘못되었습니다. 이것은 일반적으로 (LAMPP) XAMPP를 사용하고 있고 /tmp/mysql.sock에 없기 때문입니다.

php.ini 파일을 열고 다음 줄을 찾습니다.

mysql.default_socket

그리고 그것을 만드십시오

mysql.default_socket = /path/to/mysql.sock

비슷한 문제가 있었고 mysql 127.0.0.1대신 localhost.

이것은 아마도 호스트 설정에 문제가 있음을 의미하지만이 빠른 수정은 지금 당장 진행 중입니다.


이것은 Apache HTTP기본 설치MySQL의 사용자 정의 설치 가있는 Mac OS X 용 입니다 .

대답은 @ alec-gorge의 탁월한 응답을 기반으로하지만 내 구성 (대부분 Mac OS X 특정 구성)에 구성하기 위해 몇 가지 특정 변경 사항을 Google에 검색해야했기 때문에 완전성을 위해 여기에 추가 할 것이라고 생각했습니다.

Apache HTTP에 대한 PHP5 지원 활성화

에서 PHP5 지원이 활성화되어 있는지 확인하십시오 /etc/apache2/httpd.conf.

php5_module 모듈 을로드 하려면 파일을 편집 sudo vi /etc/apache2/httpd.conf(요청시 암호 입력 ;)하고 줄의 주석을 제거 ( 시작 부분부터 제거 )합니다 .

LoadModule php5_module libexec/apache2/libphp5.so

Apache HTTP를 시작합니다 sudo apachectl start(또는 restart이미 시작되어 구성 파일을 다시 읽기 위해 다시 시작해야하는 경우).

/var/log/apache2/error_logphp5_module이 활성화되었음을 알려주는 줄이 있는지 확인하십시오 PHP/5.3.15.

[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations

소켓 파일 이름 조회

MySQL이 실행 중일 때 (와 함께 ./bin/mysqld_safe) 로그 파일을 찾을 수있는 위치를 알려주는 디버그 행이 콘솔에 인쇄되어야합니다. 파일 이름의 호스트 이름을 기록해 두십시오. localhost제 경우에는 구성에 따라 다를 수 있습니다.

뒤에 오는 파일 Logging to이 중요합니다. MySQL이 작업을 기록하는 곳입니다.

130309 12:17:59 mysqld_safe Logging to '/Users/jacek/apps/mysql/data/localhost.err'.
130309 12:17:59 mysqld_safe Starting mysqld daemon with databases from /Users/jacek/apps/mysql/data

localhost.err파일을 엽니 다 (다시 말하지만, 이름이 다를 수 있음). 즉 tail -1 /Users/jacek/apps/mysql/data/localhost.err, 소켓 파일의 이름을 찾으려면 마지막 행이어야합니다.

$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

.NET에서 socket:사용해야하는 소켓 파일 인 부분에 유의하십시오 php.ini.

MySQL에 로그인하고 실행하여 소켓의 파일 이름 위치를 결정하는 또 다른 방법 (일부는 더 쉬운 방법이라고 말합니다)이 있습니다.

show variables like '%socket%';

MySQL 지원으로 PHP5 구성-/etc/php.ini

php.ini라고하면 ...

에서 /etc디렉토리있다 /etc/php.ini.default 파일. /etc/php.ini에 복사하십시오 .

sudo cp /etc/php.ini.default /etc/php.ini

mysql.default_socket을 열고 /etc/php.ini찾습니다 .

sudo vi /etc/php.ini

The default of mysql.default_socket is /var/mysql/mysql.sock. You should change it to the value you have noted earlier - it was /tmp/mysql.sock in my case.

Replace the /etc/php.ini file to reflect the socket file's name:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Final verification

Restart Apache HTTP.

sudo apachectl restart 

Check the logs if there are no error related to PHP5. No errors means you're done and PHP5 with MySQL should work fine. Congrats!


Replacing 'localhost' to '127.0.0.1' in config file (db connection) helped!


First, ensure MySQL is running. Command: mysqld start

If you still cannot connect then: What does your /etc/my.cnf look like?

The other 2 posts are correct in that you need to check your socket because 2002 is a socket error.

A great tutorial on setting up LAMP is: http://library.linode.com/lamp-guides/centos-5.3/index-print


Not that it helps you much, but in the recent versions (and even less recent) of MySQL, error code 2002 means “Can't connect to local MySQL server through socket [name-of-socket]”, so that might tell you a bit more.


Expanding on Matthias D's answer here I was able to resolve this 2002 error on both MySQL and MariaDB with exact paths using these commands:

First get the actual path to the MySQL socket:

netstat -ln | grep -o -m 1 '/.*mysql.sock'

Then get the PHP path:

php -r 'echo ini_get("mysql.default_socket") . "\n";'

Using the output of these two commands, link them up:

sudo ln -s /actualpath/mysql.sock /phppath/mysql.sock

If that returns No such file or directory you just need to create the path to the PHP mysql.sock, for example if your path was /var/mysql/mysql.sock you would run:

sudo mkdir -p /var/mysql

Then try the sudo ln command again.


Restarting the mysql server might help. In my case, restarting the server saved a lot of time.

service mysql restart

P.S.- use sudo service mysql restart for non-root user.


I'd check your php.ini file and verify the mysql.default_socket is set correctly and also verify that your mysqld is correctly configured with a socket file it can access. Typical default is "/tmp/mysql.sock".


I encountered this problem too, then i modified 'localhost' to '127.0.0.1',it works.


in my case I have problem with mysqli_connect.
when I want to connect
mysqli_connect('localhost', 'myuser','mypassword')
mysqli_connect_error() return me this error "No such file or directory"

this worked for me mysqli_connect('localhost:3306', 'myuser','mypassword')


The error 2002 means that MySQL can't connect to local database server through the socket file (e.g. /tmp/mysql.sock).

To find out where is your socket file, run:

mysql_config --socket

then double check that your application uses the right Unix socket file or connect through the TCP/IP port instead.

Then double check if your PHP has the right MySQL socket set-up:

php -i | grep mysql.default_socket

and make sure that file exists.

Test the socket:

mysql --socket=/var/mysql/mysql.sock

If the Unix socket is wrong or does not exist, you may symlink it, for example:

ln -vs /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

or correct your configuration file (e.g. php.ini).

To test the PDO connection directly from PHP, you may run:

php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8;dbname=dbname', 'root', 'root');"

Check also the configuration between Apache and CLI (command-line interface), as the configuration can be differ.

It might be that the server is running, but you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening. To correct that you need to invoke a client program (e.g. specifying --port option) to indicate the proper port number, or the proper named pipe or Unix socket file (e.g. --socket option).

See: Troubleshooting Problems Connecting to MySQL


Other utils/commands which can help to track the problem:

  • mysql --socket=$(php -r 'echo ini_get("mysql.default_socket");')
  • netstat -ln | grep mysql
  • php -r "phpinfo();" | grep mysql
  • php -i | grep mysql
  • Use XDebug with xdebug.show_exception_trace=1 in your xdebug.ini
  • On OS X try sudo dtruss -fn mysqld, on Linux debug with strace
  • Check permissions on Unix socket: stat $(mysql_config --socket) and if you've enough free space (df -h).
  • Restart your MySQL.
  • Check net.core.somaxconn.

Make sure your local server (MAMP, XAMPP, WAMP, etc..) is running.


I had a similar problem.
Basically here the problem is there are probably two instances of mysql running.
A) One running at /etc/init.d
B) Lamp being installed at /opt/lamp
Solution :
Step 1 :- Find all mysql running instances using commnad "find / | grep mysqld"
Step 2 :- Shutdown the services running at /etc/init.d using service mysql stop
Step 3 :- Restart your Lamp services using /opt/lamp/lamp restart

You should be good to go :)


On a Mac, before doing all the hard work, simply check your settings in System Preferences > MySQL. More often than not, I've experienced the team running into this problem since The MySQL Server Instance is stopped.

Click the Start MySQL Server button, and magic will happen.


Im using PHP-FPM or multiple php version in my server. On my case i update mysqli value since there is not mysql default socket parameter :

mysqli.default_socket

to :

mysql.default_socket = /path/to/mysql.sock

thanks to @Alec Gorge


I had the same problem. My socket was eventually found in /tmp/mysql.sock. Then I added that path to php.ini. I found the socket there from checking the page "Server Status" in MySQL Workbench. If your socket isn't in /tmp/mysql.sock then maybe MySQL Workbench could tell you where it is? (Granted you use MySQL Workbench...)


I've installed MySQL using installer. In fact, there was no data directory alongside 'bin' directory.

So, I manually created the 'data' directory under "C:\Program Files\MySQL\MySQL Server 8.0". And it worked (changing the root password following steps suggested on https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html.


enable and start mariadb service

sudo systemctl enable mariadb.service

sudo systemctl start mariadb.service

참고URL : https://stackoverflow.com/questions/1676688/mysql-connection-not-working-2002-no-such-file-or-directory

반응형