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.
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 :)
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...)
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.
Make sure your local server (MAMP, XAMPP, WAMP, etc..) is running.
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:
And make it
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:
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:
and make sure that file exists.
Test the socket:
If the Unix socket is wrong or does not exist, you may symlink it, for example:
or correct your configuration file (e.g.
php.ini
).To test the PDO connection directly from PHP, you may run:
Check also the configuration between Apache and CLI (command-line interface), as the configuration can be differ.
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
xdebug.show_exception_trace=1
in yourxdebug.ini
sudo dtruss -fn mysqld
, on Linux debug withstrace
stat $(mysql_config --socket)
and if you've enough free space (df -h
).net.core.somaxconn
.