No such file or directory [duplicate] - php

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

Related

EPSO CRM 500 error, Log Says file not found [duplicate]

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

Can't connect to my online database with PDO and PHP [duplicate]

I am getting the following error when I try to connect to mysql:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Is there a solution for this error? What might be the reason behind it?
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
Ensure that your mysql service is running
service mysqld start
Then, try the one of the following following:
(if you have not set password for mysql)
mysql -u root
if you have set password already
mysql -u root -p
If your file my.cnf (usually in the etc folder) is correctly configured with
socket=/var/lib/mysql/mysql.sock
you can check if mysql is running with the following command:
mysqladmin -u root -p status
try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 777 /var/lib/mysql/
that solved it for me
The MySQL server is not running, or that is not the location of its socket file (check my.cnf).
Most likely mysql.sock does not exist in /var/lib/mysql/.
If you find the same file in another location then symlink it:
For ex: I have it in /data/mysql_datadir/mysql.sock
Switch user to mysql and execute as mentioned below:
su mysql
ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock
That solved my problem
If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db:
yum remove mysql
yum -y install mariadb-server mariadb
service mariadb start
You should then be able to access mysql in the usual fashion:
mysql -u root -p
Just edit /etc/my.cnf
Add following lines to my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again
mysql -u user -p password database -h host;
In my case I have moved socket file to another location inside /etc/my.cnf
from /var/lib/mysql/mysql.sock to /tmp/mysql.sock
Even after restarting the mysqld service, I still see the error message when I try to connect.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld
Works:
mysql -uroot -p -h127.0.0.1
mysql -uroot -p --socket=/tmp/mysql.sock
Does not Work:
mysql -uroot -p
mysql -uroot -p -hlocalhost
You can fix this problem by adding the same socket line under [client] section inside mysql config.
Check if your mysqld service is running or not, if not run, start the service.
If your problem isn't solved, look for /etc/my.cnf and modify as following, where you see a line starting with socket. Take a backup of that file before doing this update.
socket=/var/lib/mysql/mysql.sock
Change to
socket=/opt/lampp/var/mysql/mysql.sock -u root
MariaDB, a community developed fork of MySQL, has become the default implementation of MySQL in many distributions.
So first you should start,
$ sudo systemctl start mariadb
If this fails rather try,
$ sudo systemctl start mysqld
Then to start mysql,
$ mysql -u root -p
As of today, in Fedora the package is named mariadb
And in Ubuntu it is called mariadb-server.
So you may have to install it if its not already installed in your system.
Make sure you have enough space left in /var. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Consider using
expire_logs_days = 10
max_binlog_size = 100M
This will help you keep disk usage down.
Please check whether another mysql service is running.
Make sure you started the server:
mysql.server start
Then connect with root user:
mysql -uroot
Here's what worked for me:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart
One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one:
eric#dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (2)
eric#dev ~ $
So you have to specify the host like this:
eric#dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| mysql |
| performance_schema |
+-------------------------+
3 rows in set (0.00 sec)
mysql> exit
Bye
eric#dev ~ $
If your mysql was previously working and has stopped suddenly just "reboot" the server.
Was facing this issue on my CentOS VPS.->
Was constantly getting
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)
Tried all techniques, finally restarting the server fixed the issues ->
shutdown -r now
Hope this helps !!
try
echo 0 > /selinux/enforce
if you change files in /var/lib/mysql [ like copy or replace that ], you must set owner of files to mysql this is so important if mariadb.service restart has been faild
chown -R mysql:mysql /var/lib/mysql/*
chmod -R 700 /var/lib/mysql/*
First enter "service mysqld start" and login
It worked for me with the following changes
Whatever path for socket is mentioned in [mysqld] and same in [client] in my.cnf and restart mysql
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Please ensure you have installed MySQL server correctly, I met this error many times and I think it's complicated to debug from the socket, I mean it might be easier to reinstall it.
If you are using CentOS 7, here is the correct way to install it:
First of all, add the mysql community source
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Then you can install it by yum install mysql-community-server
Start it with systemctl: systemctl start mysqld
My problem was that I installed mysql successfully and it worked fine.
But one day, the same error occurred.
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
And no mysql.sock file existed.
This sollution solved my problem and mysql was up and running again:
Log in as root:
sudo su -
Run:
systemctl stop mysqld.service
systemctl start mysqld.service
systemctl enable mysqld.service
Test as root:
mysql -u root -p
mysql should now be up and running.
I hope this can help someone else as well.
Note that while mysql reads the info of the location of the socketfile from the my.cnf file, the mysql_secure_installation program seems to not do that correctly at times.
So if you are like me and shuffle things around at installationtime you might get into the situation where you can connect to the database with mysql just fine, but the thing can not be secured (not using that script anyway).
To fix this the suggestion from sreddy works well: make a softlink from where the script would expect the socket to where it actually is. Example:
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
(I use /tmp/ as a default location for sockets)
This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!
In my case, I was importing a new database, and I wasnt able to connect again after that. Finally I realized that was a space problem.
So you can delete the last database and expand you hard drive or what I did, restored a snapshot of my virtual machine.
Just in case someone thinks that is useful
I came to this issue when i reinstall mariadb with yum, which rename my /etc/my.cnf.d/client.cnf to /etc/my.cnf.d/client.cnf.rpmsave but leave /etc/my.cnf unchanged.
For I has configed mysqld's socket in /etc/my.cnf, and mysql's socket in /etc/my.cnf.d/client.cnf with customized path.
So after the installation, mysql client cannot find the mysql's socket conf, so it try to use the default socket path to connect the msyqld, which will cause this issue.
Here are some steps to locate this isue.
check if mysqld is running with ps -aef | grep mysqld
$ps -aef | grep mysqld | grep -v grep
mysql 19946 1 0 09:54 ? 00:00:03 /usr/sbin/mysqld
if mysqld is running, show what socket it use with netstat -ln | grep mysql
$netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 560340807 /data/mysql/mysql.sock
check if the socket is mysql client trying to connect.
if not, edit /etc/my.conf.d/client.cnf or my.conf to make the socket same with it in mysqld
[client]
socket=/data/mysql/mysql.sock
You also can edit the mysqld's socket, but you need to restart or reload mysqld.
Just rain into the same problem -- and here's how I addressed it.
Assuming mysqld is running, then the problem might just be the mysql client not knowing where to look for the socket file.
The most straightforward way to address this consists in adding the following line to your user's profile .my.cnf file (on linux that's usually under /home/myusername):
socket=<path to the mysql socket file>
If you don't have a .my.cnf file there, then create one containing the following:
[mysql]
socket=<path to the mysql socket file>
In my case, since I moved the mysql default data folder (/var/lib/mysql) in a different location (/data/mysql), I added to .my.cnf the following:
[mysql]
socket=/data/mysql/mysql.sock
Hope this helps.
ran into this issue while trying to connect mysql in SSH client, found adding the socket path to the command helpful when switching between sockets is necessary.
> mysql -u user -p --socket=/path/to/mysql5143.sock
This is a problem if you are running out of disk space.
Solution is to free some space from the HDD.
Please read more to have the explanation :
If you are running MySQL at LINUX check the free space of HDD with the command disk free :
df
if you are getting something like that :
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 5162828 4902260 0 100% /
udev 156676 84 156592 1% /dev
/dev/sda3 3107124 70844 2878444 3% /home
Then this is the problem and now you have the solution!
Since mysql.sock wants to be created at the mysql folder which is almost always under the root folder could not achieve it because lack of space.
If you are periodicaly give the ls command under the mysql directory (at openSUSE 11.1 is at /var/lib/mysql) you will get something like :
hostname:/var/lib/mysql #
.protected IT files ibdata1 mysqld.log systemtemp
.tmp NEWS greekDB mysql mysqld.pid test
ARXEIO TEMP1 ib_logfile0 mysql.sock polis
DATING deisi ib_logfile1 mysql_upgrade_info restore
The mysql.sock file appearing and disappearing often (you must to try allot with the ls to hit a instance with the mysql.sock file on folder).
This caused by not enough disk space.
I hope that i will help some people!!!!
Thanks!
I had to disable explicit_defaults_for_timestamp from my.cnf.

What's the cause of this "PHP/MySQL Can't Connect Through Socket" error? [duplicate]

I am having a big problem trying to connect to mysql. When I run:
/usr/local/mysql/bin/mysql start
I have the following error :
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
I do have mysql.sock under the /var/mysql directory.
In /etc/my.cnf I have:
[client]
port=3306
socket=/var/mysql/mysql.sock
[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
and in /etc/php.ini I have :
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock
I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart
But I still have the error.
Otherwise, I don't know if that's relevant but when I do mysql_config --sockets I get
--socket [/tmp/mysql.sock]
If your file my.cnf (usually in the /etc/mysql/ folder) is correctly configured with:
socket=/var/lib/mysql/mysql.sock
You can check if mysql is running with the following command:
mysqladmin -u root -p status
Try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/
That solved it for me.
Are you sure you installed MySQL as well as MySQL server?
For example to install MySQL server I'll use yum or apt to install both MySQL command line tool and the server:
yum -y install mysql mysql-server (or apt-get install mysql mysql-server)
Enable the MySQL service:
/sbin/chkconfig mysqld on
Start the MySQL server:
/sbin/service mysqld start
Afterwards set the MySQL root password:
mysqladmin -u root password 'new-password' (with the quotes)
A quick workaround that worked for me: try using the local ip address (127.0.0.1) instead of 'localhost' in mysql_connect(). This "forces" php to connect through TCP/IP instead of a unix socket.
I got the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
Tried several ways and finally solved it through the following way:
sudo gksu gedit /etc/mysql/my.cnf
Modified:
#bind-address = 127.0.0.1
to:
bind-address = localhost
and restarted:
sudo /etc/init.d/mysql restart
It worked.
Make sure you are running mysqld : /etc/init.d/mysql start
I got this error when I set cron job for my file. I changed the permissions of file to 777 but it still not worked for me. Finally I got the solution. May be it will be helpful for others.
Try with this command:
mysql -h 127.0.0.1 -P 3306 -u root -p
Remember that -h means host, -P means port and -p means password.
To prevent the problem from occurring, you must perform a graceful shutdown of the server from the command line rather than powering off the server.
shutdown -h now
This will stop the running services before powering down the machine.
Based on Centos, an additional method for getting it back up again when you run into this problem is to move mysql.sock:
mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak
service mysqld start
Restarting the service creates a new entry called mqsql.sock
As can be seen by the many answers here, there are lots of problems that can result in this error message when you start the MySQL service. The thing is, MySQL will generally tell you exactly what's wrong, if you just look in the appropriate log file.
For example, on Ubuntu, you should check /var/log/syslog. Since lots of other things might also be logging to this file, you probably want to use grep to look at mysql messages, and tail to look at only the most recent. All together, that might look like:
grep mysql /var/log/syslog | tail -50
Don't blindly make changes to your configuration because someone else said 'This worked for my system.' Figure out what is actually wrong with your system and you'll get a better result much faster.
Another workaround is to edit /etc/my.cnf and include host in the section [client]
[client]
#password = your_password
host = 127.0.0.1
port = 3306
socket = /var/run/mysql/mysql.sock
And then restarting the mysql service.
This workaround was tested in: Server version: 5.5.25a-log Source distribution
Try with -h (host) and -P(port):
mysql -h 127.0.0.1 -P 3306 -u root -p
I had the same problem and it has been caused by an update of mysql drivers when mysql server was running.
I fixed it just restarting both mysql and apache2:
sudo service mysql stop
sudo service mysql start
sudo service apache2 stop
sudo service apache2 start
sudo service mysql start
This should serve you just fine. There could be a possibility that you changed some commands that affected the mysql configurations.
In my case, I was using Centos 5.5. I found that the problem was because the mysql service was stopped some how.
So I started mysql service with the command:
/etc/init.d/mysqld start
So.. silly mistake.
If everything worked just fine and you just started seeing this error, before you do anything else, make sure you're not out of disk space:
df -h
If the volume where the mysql.sock is being created is at 100% use, MySql won't be able to create it and this will be the cause of this error. All you need to do is delete something that's not needed, like old log files.
There are many solutions to this problem but for my situation, I just needed to correct the DATE on the machine/server (Ubuntu 16.04 Server).
i) Check the date of your server and correct it.
ii) Run sudo /etc/init.d/mysql restart
That should get it started.
I was getting the error because I was running MAMP and my .sock file was in a different location. I just added a symbolic link where the app thought it should be that pointed to where it actually was and it worked like a charm.
If you are using AWS (Amazon Web Services) Micro version, then it is a memory issue. When I ran
mysql
from the terminal it would say
ERROR 2002 (HY000): Can't connect to local MySQL server through socket /var/run/mysqld/mysqld.sock' (111)
So I tried the following and it would just fail.
service mysqld restart
After much searching, I found out that you have to create a swap file for MySQL to have enough memory. Instructions are listed: http://www.prowebdev.us/2012/05/amazon-ec2-linux-micro-swap-space.html.
Then, I was able to restart mysqld.
I also found that this was a permissions problem. I compared the MySQL files to a working install (both on Debian 6 squeeze) and had to make the following ownership changes (where mydatabase is any database(s) you have).
Ownership mysql:mysql:
chown mysql:mysql /var/lib/mysql
chown mysql:mysql /var/lib/mysql/ib*
chown mysql:mysql /var/lib/mysql/mydatabase
chown mysql:mysql /var/lib/mysql/mydatabase/*
chown mysql:mysql /var/lib/mysql/mysql/*
Ownership mysql:root:
chown mysql:root /var/lib/mysql/mysql
chown mysql:root /var/run/mysqld
Ownership mysql:adm:
chown mysql:adm /var/log/mysql
chown mysql:adm /var/log/mysql.err
chown mysql:adm /var/log/mysql.log*
For me - this was simply a case of MySQL taking a long time to load. I have over 100,000 tables in one of my databases and it did eventually start but obviously has to take a long time in this instance.
You might want to chek if the hard disk is full (df on the console), that's what ultimately triggered this error for me.
you can always start mysql server by specifying the location of the mysql.sock file using the --socket option like
mysql --socket=/var/mysql/mysql.sock
This will work even if the location of socket file in specified in a different location in the my.cnf file.
For those whose any solution did not work, try:
cd /etc/mysql
check if my.cnf is present
nano my.cnf
and make sure you have only one bind-address as follows:
bind-address = 127.0.0.1
If not, that might be the problem, just exit nano and save the file.
and service mysql start
note that if you don't have nano (its a text editor) just install it with apt-get install nano and once in just press Ctrl+X to exit, dont forget to say Y to save and use the same file)
sudo service mysqld start
Worked for me, I'm using Centos
I had this problem too when trying to start the server, so many of the answers here that just say to start the server didn't work. The first thing you can do is execute the following to see if there are any config errors:
/usr/sbin/mysqld --verbose --help 1>/dev/null
I did have one error that showed up:
160816 19:24:33 [Note] /usr/sbin/mysqld (mysqld 5.5.50-0ubuntu0.14.04.1-log) starting as process 9461 ...
160816 19:24:33 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
160816 19:24:33 [Note] Plugin 'FEDERATED' is disabled.
160816 19:24:33 [ERROR] /usr/sbin/mysqld: unknown variable 'innodb-online-alter-log-max-size=4294967296'
160816 19:24:33 [ERROR] Aborting
A simple grep -HR "innodb-online-alter-log-max-size" /etc/mysql/ showed me exactly what file contained the offending line, so I removed that line from the file.
Then, checking my /var/log/mysql/error.log file I had:
InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 671088640 bytes!
160816 22:46:46 [ERROR] Plugin 'InnoDB' init function returned error.
160816 22:46:46 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160816 22:46:46 [ERROR] Unknown/unsupported storage engine: InnoDB
160816 22:46:46 [ERROR] Aborting
Based on this question the accepted solution wouldn't work because I couldn't even get the server started, so I followed what some of the comments said and deleted my /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1 files.
This allowed the server to start and I was able to connect and execute queries, however checking my error log file it was quickly getting filled up with several tens of thousands of lines like this:
160816 22:52:15 InnoDB: Error: page 1415 log sequence number 82039318708
InnoDB: is in the future! Current system log sequence number 81640793100.
InnoDB: Your database may be corrupt or you may have copied the InnoDB
InnoDB: tablespace but not the InnoDB log files. See
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: for more information.
Based on a suggestion from here, to fix this I did a mysqldump and restore of all databases (see the link for several other solutions).
$ mysqldump -u root -p --allow-keywords --add-drop-database --comments --hex-blob --opt --quote-names --databases db_1 db_2 db_3 db_etc > backup-all-databases.sql
$ mysql -u root -p < backup-all-databases.sql
Everything appears to be working as expected now.
When you use localhost to connect to MySQL, the operating system uses the socket connector. However, if you use the 127.0.0.1 IP address, the operating system will use the TCP/IP connector. So, a possible solution when you’re having issues with the socket connector is trying to establish the connection using TCP/IP by specifying the 127.0.0.1 IP address instead of localhost.
Adding
--protocol=tcp
to the list of pramaters in your connection worked for me.
This was good enough for me
sudo /etc/init.d/mysql restart
I ran into this issue today. None of these answers provided the fix. I needed to do the following commands (found here https://stackoverflow.com/a/20141146/633107) for my mysql service to start:
sudo /etc/init.d/mysql stop
cd /var/lib/mysql/
ls ib_logfile*
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
... etc ...
/etc/init.d/mysql restart
This was partly indicated by the following errors in /var/log/mysql/error.log:
140319 11:58:21 InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes
InnoDB: than specified in the .cnf file 0 5242880 bytes!
140319 11:58:21 [ERROR] Plugin 'InnoDB' init function returned error.
140319 11:58:21 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140319 11:58:21 [ERROR] Unknown/unsupported storage engine: InnoDB
140319 11:58:21 [ERROR] Aborting
I also saw the disk full error, but only when running commands without sudo. If the permissions check fails, it reports disk full (even when your partition is not even close to full).
CentOS 7, 64 bit. Fresh installation.
In my case, the error was because I didn't have the right MySQL server and MySQL client installed.
Using yum, I removed mariadb and mysql-community edition. I downloaded the rpm's for the client and server from the official MySQL website and installed the server and client.
On installing the server, I was shown a message that the password to the root account for MySQL was stored in a file which I could view with sudo cat /root/.mysql_secret.
So after installing the client and server, I checked if MySQL was working (I think I rebooted before doing so) with the command sudo service mysql status and I got the result.
MySQL running (2601) [ OK ]
I logged into MySQL using the password from the .mysql_secret file:
mysql -uroot -pdxM01Xfg3DXEPabpf. Note that dxM01Xfg3DXEPabpf is the password mentioned in the .mysql_secret file.
and then typed entered the following command at the mysql prompt to change the password of root:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('somePassword');
Everything worked fine from then on.
This doesn't directly answer your question but a subset of it, namely using PythonAnywhere. I kept stumbling upon this question when looking for a fix so I'm adding it here in the hope that it will help others in my situation.
PythonAnywhere decided to change the database connection hostnames in order to improve efficiency and reliability, as detailed here:
The official host name you should use for connecting to your account's
MySQL database instance has changed from mysql.server to
yourusername.mysql.pythonanywhere-services.com. This bypasses a part of our infrastructure that has started showing problems in recent
weeks, and it should be much more efficient and reliable than the old
way.
Hence, you will need to update your hostname to the value highlighted above.

Mac OS X 10.8 MySQL connection error: localhost does not work, 127.0.0.1 all OK

I have now a new MabBook with 10.8 and I am trying to set up all the Apache & MySQL etc.
Apache successfully runs including vhosts.
In my /etc/hosts I have all the local hosts set including 127.0.0.1 localhost etc.
When I try to coennct to one of my local vhost, e.g. alpensonne, I can connect to MySQL without any troubles. If I try to have the MySQL host to be localhost, I get the follwoing error:
Database connection error (2): Could not connect to MySQL.
I know the error (2) means username or password incorrect, but they are fine.
If I connect with
mysql -u root
SELECT password,host FROM user where user='root';
Also I have just done to be double sure:
SET PASSWORD FOR 'root'#'localhost' = '';
Is it possible that localhost uses a different MySQL socket? Maybe it then tries to connect to a different MySQL?
Ok I found the mistake...
The problam was, that I have forgot to change the php.ini file to point to the mysql.default_socket...
My default_socket was set to /var/mysql/mysql.sock
so I just created a symlink to /tmp/mysql.sock and now all works fine! :)
#shadyyx
Thanks for the proposal of MAMP, I know this of course but not such a big fan of MAMP, XAMPP, LAMP etc. for windows user its good so you can use apache instead of IIS, but for Linux, Unix and also Mac user you can use the build in apache server... so no need to use an other third party software. also you are depended then on the php version etc. I like it more to be able to configure it myself.
To elaborate on the answer from #schurtertom...
The problem is that mysql.default_socket setting in PHP.INI defaults to a different location than where MySQL actually puts that file.
Instead of editing the config files, create an alias in the location that PHP is looking that connects to the real mysql.sock by running these two commands (no restart needed):
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
With MySQL from MacPorts it works with this path:
mysql.default_socket = /opt/local/var/run/mysql56/mysqld.sock
In case MySQL still does not connected, run:
brew switch openssl 1.0.2s

MySQL connection not working: 2002 No such file or 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

Categories