Related
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 and was able to solve it by addressing my mysql with 127.0.0.1 instead of localhost.
This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.
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
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.
LoadModule php5_module libexec/apache2/libphp5.so
Start Apache HTTP with sudo apachectl start (or restart 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 see 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
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.
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
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.
$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
Note the socket: part - that's the socket file you should use in php.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:
show variables like '%socket%';
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.
sudo cp /etc/php.ini.default /etc/php.ini
Open /etc/php.ini and look for mysql.default_socket.
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!
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.
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? (or /etc/msyql/my.cnf)
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
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.
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.
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.
May be I am late to answer this, but what solved my problem was to install the mysql-server
sudo apt-get install mysql-server
after spending more than 5 hours I found this solution which helped me to proceed.
I hope this would help someone if the top answers won't help them
by using 127.0.0.1 insteady of localhost solve the problem
Digital Ocean MySql 2002-no-such-file-or-directory
Add this end of file /etc/mysql/my.cnf
[mysqld]
innodb_force_recovery = 1
Restart MySql
service mysql restart
First check MySQL server is running or not. if running then check socket path by login to MySQL through command line.
mysql -uUSER -pPASSWORD
then
show variables like 'socket';
You'll find path of mysql socket which you can use further in connection string like below:
$conn = mysqli_connect('localhost', 'USER', 'PASSWORD', 'path of
socket file');
If MySQL is not running. Then Please share error logs which you are getting to troubleshoot further.
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
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 and was able to solve it by addressing my mysql with 127.0.0.1 instead of localhost.
This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.
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
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.
LoadModule php5_module libexec/apache2/libphp5.so
Start Apache HTTP with sudo apachectl start (or restart 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 see 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
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.
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
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.
$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
Note the socket: part - that's the socket file you should use in php.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:
show variables like '%socket%';
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.
sudo cp /etc/php.ini.default /etc/php.ini
Open /etc/php.ini and look for mysql.default_socket.
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!
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.
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? (or /etc/msyql/my.cnf)
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
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.
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.
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.
May be I am late to answer this, but what solved my problem was to install the mysql-server
sudo apt-get install mysql-server
after spending more than 5 hours I found this solution which helped me to proceed.
I hope this would help someone if the top answers won't help them
by using 127.0.0.1 insteady of localhost solve the problem
Digital Ocean MySql 2002-no-such-file-or-directory
Add this end of file /etc/mysql/my.cnf
[mysqld]
innodb_force_recovery = 1
Restart MySql
service mysql restart
First check MySQL server is running or not. if running then check socket path by login to MySQL through command line.
mysql -uUSER -pPASSWORD
then
show variables like 'socket';
You'll find path of mysql socket which you can use further in connection string like below:
$conn = mysqli_connect('localhost', 'USER', 'PASSWORD', 'path of
socket file');
If MySQL is not running. Then Please share error logs which you are getting to troubleshoot further.
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
I'm trying to install an opensource software on my local and I'm running zendserver on linux ubuntu.
I created the database and provided a correct user and password.
But as soon as I click on submit button I get a MySQL 2002 error saying:
(2002) No such file or directory
I tried to restart apache2 but still I get the same error after I have restarted apache2.
How can I resolve this Mysql error?
You don't have permissions to access the directory /var/lib/mysql/whatever.sock because mysql is the owner of the folder
or
/path/whatever.sock doesn't exist.
You can try this though [Linux specific, but what other operating systems are there?]
Go to /etc/my.cnf and change/add the lines:
[mysqld]
datadir=/var/lib/mysql
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
This way the client and server use the same socket and it's in a public directory.
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 and was able to solve it by addressing my mysql with 127.0.0.1 instead of localhost.
This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.
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
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.
LoadModule php5_module libexec/apache2/libphp5.so
Start Apache HTTP with sudo apachectl start (or restart 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 see 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
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.
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
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.
$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
Note the socket: part - that's the socket file you should use in php.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:
show variables like '%socket%';
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.
sudo cp /etc/php.ini.default /etc/php.ini
Open /etc/php.ini and look for mysql.default_socket.
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!
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.
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? (or /etc/msyql/my.cnf)
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
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.
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.
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.
May be I am late to answer this, but what solved my problem was to install the mysql-server
sudo apt-get install mysql-server
after spending more than 5 hours I found this solution which helped me to proceed.
I hope this would help someone if the top answers won't help them
by using 127.0.0.1 insteady of localhost solve the problem
Digital Ocean MySql 2002-no-such-file-or-directory
Add this end of file /etc/mysql/my.cnf
[mysqld]
innodb_force_recovery = 1
Restart MySql
service mysql restart
First check MySQL server is running or not. if running then check socket path by login to MySQL through command line.
mysql -uUSER -pPASSWORD
then
show variables like 'socket';
You'll find path of mysql socket which you can use further in connection string like below:
$conn = mysqli_connect('localhost', 'USER', 'PASSWORD', 'path of
socket file');
If MySQL is not running. Then Please share error logs which you are getting to troubleshoot further.
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
My code was working all fine yesterday and today it suddenly just don't want to connect to my database. I have changed no settings on it or on the code and I haven't updated any software either. All I do is this:
new PDO('mysql:host=localhost;port=3306;dbname=test', 'username', 'password');
And I get a nice exception message saying this:
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in ...
The thing is: I'm clearly not trying to connect using a unix socket but using TCP/IP. What am I doing wrong? Is there something I'm missing here?
Thanks for any help.
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_socketinstead 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.
From the PHP documentation about connection to MySQL using PDO: PDO_MYSQL DNS
The note at the very end says:
Unix only:
When the host name is set to "localhost", then the connection to the
server is made thru a domain socket. If PDO_MYSQL is compiled against
libmysql then the location of the socket file is at libmysql's
compiled in location. If PDO_MYSQL is compiled against mysqlnd a
default socket can be set thru the pdo_mysql.default_socket setting.
So in order to fix this you would have to properly configure in php.ini the location of your mysql.sock
Find your mysql.sock file. Common locations:
/tmp/mysql.sock
/tmp/mysql/mysql.sock
/var/mysql/mysql.sock
or if you use MAMP or LAMP look for inside the tmp folder for mysql
Edit your php.ini file and properly set the value for pdo_mysql.default_socket
Restart your Apache server to pickup the changes in the php.ini file
On Ubuntu, you can use this setting in php.ini
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
There's an update to the docs for Drush which is documented here.
I just added this line:
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
and all was well.