Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (38)
That certainly explains the error in trying to connect to it. How, as @Romain mentioned, is there anything in MySQL’s logs (try /var/log/mysql or somewhere around there) which indicates why it isn’t running? Do you get an error when you try to start it?
I was getting the same error, but in my case, I found out mysql wouldn’t start because the disk was 100% full. /var/log/mysqld.log was helpful.
the reason they are asking if it is running, i presume, is because the socket is made when the service starts. i installed mysql, but never started the service, so the .sock file doesn’t exist. type service mysqld start if you just installed. hth
44 Answers 44
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/
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)
Afterwards set the MySQL root password:
mysqladmin -u root password 'new-password' (with the quotes)
I had already installed the client, the command I needed was sudo apt-get install mysql-server then life was good
Isn’t the output Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (38) from the client? It’s the client trying and failing to connect, right? (I think the original question needs editing to clarify that).
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.
It worked. but why? Doesn’t MySQL resolve localhost and get 127.0.0.1 anyway before even trying to connect?
nope. when using localhost you aren’t using an Internet Socket. You are using a IPC Socket. en.wikipedia.org/wiki/Unix_domain_socket . 127.0.0.1 is local loopback which means the request won’t exit your machine but it will use TCP/IP thus being slower.
Ok. it works, but there is no need to use this trick — TCP/IP can be simply forced by passing additional parameter for the connection: —protocol=tcp .
The same, similar issue is when connecting via command line client to a port forwarded from virtualbox or docker, when you use it without a host parameter, it will fail on socket, when you use it with an -H host parameter, and with 127.0.0.1 instead of localhost, it will succeed, mysql -h 127.0.0.1 instead of mysql -h localhost
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
sudo /etc/init.d/mysql restart
I had a similar issue; moved from ethernet (192.168.220.11) to wifi (192.168.220.12) but had bind-address hard-coded to ethernet IP. Changing to localhost fixed it.
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.
Remember that -h means host, -P means port and -p means password.
Forcing a connection over TCP/IP instead of using a socket is inefficient (and requires that you turn on localhost TCP/IP support in the server, as per this earlier answer). The accepted answer from 2011 is better: Configure the server so you can use a socket properly.
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.
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.
+1 For taking a step back and pointing out something that many of the other answers fail to even consider — that actually seeing what the application might have reported as a problem is a much better approach than to blindly rush in and make changes which may not even be applicable.
Ah, so it’s using the wrong .cnf . That explains it. Now I can stop trying random things and address the actual issue. Thanks.
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
In my case, mysql wasn’t running. I ran sudo service mysql start after running sudo service mysql status to verify it wasn’t running.
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:
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.
This should serve you just fine. There could be a possibility that you changed some commands that affected the mysql configurations.
or systemctl start mariadb.service in Fedora 22 or RedHat 7. After that it is possible to set root password.
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:
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.
You might want to chek if the hard disk is full ( df on the console), that’s what ultimately triggered this error for me.
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
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.
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 had the same problem on the AWS server «micro» instance and I can confirm that making the swap file DID fix the «ERROR 2002 (HY000): Can’t connect to local MySQL server through socket /var/run/mysqld/mysqld.sock’ (111)» problem. Thank you @jth_92 !
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).
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/*
chown mysql:root /var/lib/mysql/mysql chown mysql:root /var/run/mysqld
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 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:
check if my.cnf is present
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.
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)
Unfortunately this did not work. This basically just says that only the local machine can access mysql. No remote connections.
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.