Problem
I’m attempting to install WordPress. I’ve set up Apache and MySQL, as well as the accounts and database. I attempted to create a straightforward connection:
<?php
$conn = mysql_connect('localhost', 'USER', 'PASSWORD');
if(!$conn) {
echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();
}
?>
And this is something I always get:
What file or directory is it referring to?
I’m using OS X Snow Leopard with the built-in Apache web server. I used the x86 64 dmg to install MySQL.
UPDATE: I discovered that the socket is located at /tmp/mysql.sock, therefore I updated all instances of the incorrect path with that in php.ini.
Asked by mk12
Solution #1
I had a similar issue and was able to overcome it by using 127.0.0.1 instead of localhost to address my mysql server.
This indicates that something is amiss with my hosts setup, but for the time being, this fast fix will suffice.
Answered by bryan kennedy
Solution #2
If you’re using Linux, the path to mysql.sock is incorrect. This is frequently due to the fact that you’re using (LAMPP) XAMPP and the MySQL.sock file 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
Answered by Alec Gorge
Solution #3
This is for Mac OS X with a native Apache HTTP installation and a custom MySQL installation.
The answer is based on @alec-fantastic gorge’s response, but since I had to google some particular adjustments to get it configured in my configuration, most of which were Mac OS X-specific, I felt I’d include it here for completeness’ sake.
In /etc/apache2/httpd.conf, make sure PHP5 support is enabled.
Uncomment (remove ; from the beginning of) the line to load the php5 module module with sudo vi /etc/apache2/httpd.conf (enter the password when prompted).
LoadModule php5_module libexec/apache2/libphp5.so
sudo apachectl start (or restart if it’s currently running and needs to be restarted to re-read the configuration file) starts Apache HTTP.
Make sure there’s a statement in /var/log/apache2/error log that says the php5 module is enabled – it should say PHP/5.3.15 (or similar)
[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations
When MySQL is up and running (using./bin/mysqld safe), debug lines should be sent to the terminal, indicating where the log files may be found. Note the hostname in the file name – in my example, localhost – since it may differ depending on your configuration.
The file that follows Logging to is crucial. That’s where MySQL keeps track of its activities.
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
To find the name of the socket file, open the localhost.err file (yours may be named differently), i.e. tail -1 /Users/jacek/apps/mysql/data/localhost.err (it should be the final line).
$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
The socket: section is the socket file you should use in your php.ini file.
Logging into MySQL and running: There’s another (some say easier) way to find the location of the socket’s file name by logging in and running:
show variables like '%socket%';
Speaking of php.ini…
/etc/php.ini is located in the /etc directory. the default file It should be placed in /etc/php.ini.
sudo cp /etc/php.ini.default /etc/php.ini
Look for mysql.default socket in /etc/php.ini.
sudo vi /etc/php.ini
/var/mysql/mysql.sock is the default socket for mysql.default socket. You need modify it to the value you specified before – in my instance, it was /tmp/mysql.sock.
Replace the name of the socket file in the /etc/php.ini file:
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
Restart Apache HTTP.
sudo apachectl restart
If there are no errors linked to PHP5, check the logs. If there are no errors, you’re done, and PHP5 should operate perfectly with MySQL. Congrats!
Answered by Jacek Laskowski
Solution #4
It’s possible that restarting the MySQL server will help. Restarting the server saved me a lot of time in my circumstance.
service mysql restart
For non-root users, use sudo service mysql restart.
Answered by Avani Khabiya
Solution #5
In the config file (database connection), changing ‘localhost’ to ‘127.0.0.1’ helps!
Answered by Oleg
Post is based on https://stackoverflow.com/questions/1676688/mysql-connection-not-working-2002-no-such-file-or-directory