41

Last night, my MySQL server went down unexpectedly. On attempting to restart (with service mysql restart - I'm root) it merely hangs. With the mysql -u root -p command, I get:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).

I then tried to start the daemon manually (mysqld). The prompt would hang for about 2 seconds, and then return. On closer inspection of the error logs, I got:

2016-01-22T19:18:32.399584Z 0 [ERROR] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.

2016-01-22T19:18:32.399622Z 0 [ERROR] Unable to setup unix socket lock file.

2016-01-22T19:18:32.399646Z 0 [ERROR] Aborting

I then tried chown mysql /var/run/mysqld, chmod -R 775 /var/run/mysqld and apt-get install mysql-community-server --reinstall. No luck.

I have looked around, and couldn't find a solution. Any help here?

Note: I am Running Debian 8 (Jessie) with MySQL community Server 5.7.10

johannchopin
  • 13,720
  • 10
  • 55
  • 101
SamboyCoding
  • 583
  • 1
  • 4
  • 10
  • Run this: `touch /var/run/mysqld/mysqld.sock.lock` and later `service mysql start`.... – Hackerman Jan 22 '16 at 20:06
  • 1
    Sorry, that didn't work - I get the same error in the log file (`[ERROR] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.`) – SamboyCoding Jan 23 '16 at 10:53
  • Try running: `chmod -R 755 /var/run/mysqld/` – Hackerman Feb 03 '16 at 14:51
  • 1
    Did you read the post? I said I did that. It's fixed now anyway: http://serverfault.com/questions/751277/mysql-daemon-lock-issue. I guess I should update it on here. – SamboyCoding Feb 03 '16 at 17:01
  • I skip that part...sorry, you don't need to be that rude...where are trying to help...anyway, your fix is more like a patch than an actual solution... – Hackerman Feb 03 '16 at 17:34
  • Sorry if I came across as rude. If anyone has a better solution I'd **love** to hear it, but it doesn't really matter if you're not locking the directory, does it? – SamboyCoding Feb 06 '16 at 17:16
  • Note to future travelers: It depends on your specific configuration but this is *very* likely an issue with apparmor. If you don't want to disable locking take a look at syslog and see if you're getting apparmor denies on that file. – anq Feb 13 '16 at 02:40
  • Except... I didn't have AppArmor. Also, yay for necroposting! – SamboyCoding Jul 08 '16 at 18:59
  • 2
    For anybody who gets this issue only when running mysqld by hand to reset the root password, use `mysqld_safe --skip-grant-tables` instead. – berezovskyi Mar 11 '17 at 23:28
  • I got the same issue with mysql-server. I killed all the processes related to mysql(command `ps aux | grep mysql`). Still couldn't solve the issue. Then i went through this answer - https://serverfault.com/a/752681. This almost solved the issue. But without adding `[mysqld]` in my.cnf it won't work. Refer https://stackoverflow.com/q/44298071/8175015. Note: Restart the server using `service mysql start` after updating my.cnf – Shetty_Akash Feb 06 '20 at 18:10

12 Answers12

81

I just ran into this. The mysql install was a little weird and was missing /var/run/mysqld/

MySQLd was attempting to create the socket and lock file but the parent directory was missing. I simply created the directory

sudo mkdir /var/run/mysqld

chowned it to mysql

sudo chown mysql:mysql /var/run/mysqld

I was then able to start mysqld normally.

CivFan
  • 13,560
  • 9
  • 41
  • 58
jorfus
  • 2,804
  • 27
  • 23
  • 3
    This solved my issue. I had run mysql using supervisorctl. It was working fine, until somehow the directory went missing. Ubuntu 16.04. – Nj Subedi Aug 01 '18 at 11:22
  • As I faced this issue on a seemingly fresh Ubuntu 20.04 VPS. First i checked that /run/mysqld exist, and that made me change every occurence of `/var/run/mysqld` to `/run/mysqld` inside **/etc/mysql**. After that the apt-get -f install went well. Anyway i assume the VPS had maybe some unusual defaults, that could lead to the split brain of /run and /var/run – Greg May 23 '20 at 02:31
6

I experienced the same issue today on my Ubuntu server. But I think the solution is NOT to disable locking. In my case, a look at the system log (dmesg | tail and journalctl -xe) revealed that it's an apparmor issue and the solution can be found in this question.

Community
  • 1
  • 1
affitz
  • 171
  • 1
  • 5
  • 1
    Yes, but as I stated in the comments section, I did not have any AppArmor or anything like that. It was just being plain weird. – SamboyCoding Jun 27 '16 at 19:32
5

I had following error

[ERROR] Unix socket lock file is empty /var/lib/mysql/mysql.sock.lock.

This error came up as a result that my server was lacking space 100% use (see below)

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/xvda1      41153856 28531384  12187364  100% /

Make sure the folder exists with the proper permissions

chown mysql:mysql /var/lib/mysql/

And make sure to delete the file so the mysql server could generate a new one

rm /var/lib/mysql/mysql.sock.lock
David Raleche
  • 436
  • 5
  • 11
4

Posting this answer, in case it helps someone with the same config as me. I had similar issue with this configuration:

Host OS: Ubuntu 20.04, MySql server: 8.0

The following error was seen in /var/log/mysql/error.log during run of systemctl start mysql

[ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.

Fix was similar:

mkdir -p /var/run/mysqld/
chmod 777 /var/run/mysqld/

And of course, I had enough space wherever mysql server was being installed.

Binita Bharati
  • 5,239
  • 1
  • 43
  • 24
  • 1
    Yeah, I solved my problem in the same way. It's very wired that Ubuntu 20 didn't create this folder after `apt install mysql-server`. – Siwei Jan 05 '21 at 00:32
  • 1
    For those coming to this a bit later, `chmod 777 /var/run/mysqld` is not so good. Much better is `chown mysql:mysql /var/run/mysqld` – glowkeeper Sep 09 '21 at 12:51
  • Thanks a lot :) I restarted my system and this error literally surprised me. – Ravi Hirani Feb 03 '22 at 04:46
2

Below commands worked for me.

sudo mkdir /var/run/mysqld

sudo chmod 777 /var/run/mysqld -R
  • 1
    For those coming to this a bit later, `chmod 777 /var/run/mysqld` is not so good. Much better is `chown mysql:mysql /var/run/mysqld` – glowkeeper Sep 09 '21 at 12:51
1

Also may be out of disk space.

4n70wa
  • 337
  • 4
  • 19
  • For those coming to this a bit later, `chmod 777 /var/run/mysqld` is not so good. Much better is `chown mysql:mysql /var/run/mysqld` – glowkeeper Sep 09 '21 at 12:50
1

For those running docker. If you're already running mysql in docker, you will also get such an error. Removing the mysql container solved the problem for me.

0

It's best to look at the log files (/usr/local/var/mysql/*.local.err) before attempting any of the more generic answers.


In my case first it complained of not being able to open the socket file ([Server] Could not open unix socket lock file /tmp/mysql.sock.lock.) and once I changed permissions, it complained of not being able to remove it! ([Server] Could not remove unix socket lock file /tmp/mysql.sock.lock errno 13.)

Removed it and run mysql.server start and DONE.

Ali Bakhshandeh
  • 433
  • 6
  • 10
0

Adding this since my solution was just a bit different. After moving the Mysql data directory to a different partition I ran into socket lock file creation issues that were resistant to permission setting and file touching. I think with some hunting I might have found the config updates necessary to make moving /etc/var/mysql work, but since time was a factor I just created a new empty /etc/var/mysql folder and made mysql.mysql the owner with chmod. Mysql started right up.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 09 '23 at 10:27
-1

Rename /var/lib/mysql/mysql.sock.lock & /var/lib/mysql/mysql.sock to /var/lib/mysql/mysql.sock.lock.bck & /var/lib/mysql/mysql.sock.bck respectively and start mysqld. It works

-1

If you are using docker to run MySQL, please check if there is enough allocated disk space in docker preferences.

Navaneeth Pk
  • 602
  • 7
  • 14
-3

Fixed. Add skip-external-locking to my.cnf under the [mysqld] section, and then reboot the entire system. This should fix it, for anyone who finds this. Also if you backed up your data, then attempted to restore and were told to rm ib* to make it work, you need the ibdata file.

SamboyCoding
  • 583
  • 1
  • 4
  • 10
  • How can this be the accepted answer. Are things totally differen in verison 5.7? – Wolfgang Fahl Aug 26 '17 at 11:30
  • 1
    This is the accepted answer because it's the only solution that worked for me that was suggested here. Also it's my question and my answer and I accepted it. And i can't speak for 5.7. – SamboyCoding Sep 04 '17 at 18:12
  • My research shows that things changed around 5.7.14 :-( – Wolfgang Fahl Sep 05 '17 at 09:48
  • See the man pages for the mysql server. skip-external-locking has nothing to do with the sock.lock file. –  Oct 23 '17 at 16:17
  • Works for me on 5.7.23 running (now) on MacOS X Mojave. – David Apr 21 '19 at 14:34
  • As SamboyCoding (the same who ask the question) said in the question comments: "t's fixed now anyway: serverfault.com/questions/751277/mysql-daemon-lock-issue. " This is the right answer so I'm updating this. I tried to increase RAM, SWAP, on a new ubuntu 20 install and this was the only thing that stopped the problem. File in Ubuntu is at /etc/mysql/my.cnf and it didn't have the [mysqld] line so i added the 2 lines. – gallo2000sv Jan 20 '22 at 09:07