I'm trying to set up WordPress. I have Apache and MySQL running, and the accounts and database are all set up. I tried to make a simple connection:
<?php
$conn = mysql_connect('localhost', 'USER', 'PASSWORD');
if(!$conn) {
echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();
}
?>
And I always get this:
Error: 2002 - No such file or directory
What file or directory could it be talking about?
I'm on a OS X Snow Leopard, using the built-in Apache. I installed MySQL using the x86_64 dmg.
UPDATE: I found that the socket is at /tmp/mysql.sock, so In php.ini, I replaced all occurrences of the wrong path with that.
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')
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".
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 sinceThe MySQL Server Instance is stopped
.Click the
Start MySQL Server
button, and magic will happen.This is for Mac OS X with the native installation of Apache HTTP and custom installation of MySQL.
The answer is based on @alec-gorge's excellent response, but since I had to google some specific changes to have it configured in my configuration, mostly Mac OS X-specific, I thought I'd add it here for the sake of completeness.
Enable PHP5 support for Apache HTTP
Make sure the PHP5 support is enabled in
/etc/apache2/httpd.conf
.Edit the file with
sudo vi /etc/apache2/httpd.conf
(enter the password when asked) and uncomment (remove;
from the beginning of) the line to load the php5_module module.Start Apache HTTP with
sudo apachectl start
(orrestart
if it's already started and needs to be restarted to re-read the configuration file).Make sure that
/var/log/apache2/error_log
contains a line that tells you the php5_module is enabled - you should seePHP/5.3.15
(or similar).Looking up Socket file's name
When MySQL is up and running (with
./bin/mysqld_safe
) there should be debug lines printed out to the console that tell you where you can find the log files. Note the hostname in the file name -localhost
in my case - that may be different for your configuration.The file that comes after
Logging to
is important. That's where MySQL logs its work.Open the
localhost.err
file (again, yours might be named differently), i.e.tail -1 /Users/jacek/apps/mysql/data/localhost.err
to find out the socket file's name - it should be the last line.Note the
socket:
part - that's the socket file you should use inphp.ini
.There's another way (some say an easier way) to determine the location of the socket's file name by logging in to MySQL and running:
Configuring PHP5 with MySQL support - /etc/php.ini
Speaking of php.ini...
In
/etc
directory there's /etc/php.ini.default file. Copy it to /etc/php.ini.Open
/etc/php.ini
and look for mysql.default_socket.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:Final verification
Restart Apache HTTP.
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!
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:
Then get the PHP path:
Using the output of these two commands, link them up:
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:Then try the sudo ln command again.
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 :
to :
thanks to @Alec Gorge