Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
You are using a Unix socket. When reading "localhost" MySQL client libraries don't interpret it as TCP host "localhost" and resolve that name but use the default Socket location. For using TCP on the local machine you have to use 127.0.0.1
as hostname.
To specify the past use unix_socket
instead of host
in the DSN. The location of the socket used for localhost
can be defined at compile time or in some versions of PHP using pdo_mysql.default_socket
in the php.ini
.
MySQL connection not working: 2002 No such file or directory
If you use Linux: the path to the mysql.sock file is wrong. This is usually because you are using (LAMPP) XAMPP and it isn't in /tmp/mysql.sock
Open the php.ini file and find this line:
mysql.default_socket
And make it
mysql.default_socket = /path/to/mysql.sock
PDOException SQLSTATE[HY000] [2002] No such file or directory
The error message indicates that a MySQL connection via socket is tried (which is not supported).
In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production
(or whatever environment). See here.
Linked my file to nothing and overwrote mysql.sock
Just found a forum that says the following should work on fedora
touch /var/lib/mysql/mysql.sock
cd /tmp
ln -s /var/lib/mysql/mysql.sock .
Please note the .
in the end.
service mysql restart
Thats it.
Warning: mysqli_connect(): (HY000/2002): No such file or directory
Alright, I just found the solution. The problem turned out to be that the host shouldn't have been localhost
, but 127.0.0.1
. I always thought localhost
and 127.0.0.1
was the same, but it turned out to be different.
So maybe as a tip for future users, always use the ip when in doubt.
Related Topics
How to Read Gzip-Ed Response from Stackoverflow API in PHP
Like Query Using Multiple Keywords from Search Field Using Pdo Prepared Statement
Implicit Class Variable Declaration in PHP
What Does This Mean in PHP: -> or =>
Why Do I Have to Run "Composer Dump-Autoload" Command to Make Migrations Work in Laravel
How to Write a Stored Procedure in PHPmyadmin
Type-Juggling and (Strict) Greater/Lesser-Than Comparisons in PHP
How to Add a Line Break Within Echo in PHP
New Csrf Token Per Request or Not
Simple PHP SQL Login Troubleshooting
Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()
How to Send 500 Internal Server Error Error from a PHP Script