77

I have a strange error when starting mysqld service:

Another MySQL daemon already running with the same unix socket.

I've tried to list running services and stopping them but the same error happens when starting mysqld service.

I can try to remove the mysqld and reinstall it but will this remove the database too?

рüффп
  • 5,172
  • 34
  • 67
  • 113
Mas
  • 1,267
  • 1
  • 14
  • 13

10 Answers10

228

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

GeckoSEO
  • 2,416
  • 1
  • 12
  • 6
  • 5
    The second recommendation above worked for me. Must have happened because I did not shutdown the server gracefully. Doing a `# reboot` did not solve the problem. Never tried the `shutdown -h now` – fred Jan 03 '14 at 20:54
  • 5
    "shutdown -h now" and boot did not solve it for me but the "CentOS" option worked like a charm. – PJ Brunet Jan 08 '14 at 12:18
  • 2
    Perform the `# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak` then `# shutdown -h now` and it should come up fine. – Michael Stramel Jan 10 '14 at 06:41
  • Moving the sock file worked for me as well. The big question I have is **how** this happened. I've never seen this problem before. – sstringer Feb 04 '14 at 15:00
  • Does anyone know how this problem could have occurred? – axiom82 Apr 21 '14 at 03:50
  • axiom82, it usually happens if the server is shut down forcefully, either by powering off suddenly, or by shutting down without first stopping the running services. – GeckoSEO Apr 25 '14 at 16:03
  • I always use `shutdown -r now` so it turns back on. Am I supposed to do: `shutdown -r -h now`? – User Aug 15 '14 at 01:43
  • You can run into this problem if you're doing a server migration using rsync. The solution above won't help in that case. I'm doing some test migrations using some VMs and I didn't find a solution for this problem yet. – Marco Marsala Oct 20 '15 at 15:45
  • Works by just moving the file, without shutting down. – Hafiz H May 23 '17 at 06:43
  • The first part of the answer looks nonsense to me. -h flag has nothing to do with "graceful shutdown" or shutting down the services before reboot, as this should be a default shutdown behaviour. Answer most likely gets upvoted because of it's second part. – IlliakaillI Dec 28 '18 at 14:45
  • @IlliakaillI I wrote this reply back in 2014 when the -h flag still meant something. It's no longer required. – GeckoSEO Mar 07 '19 at 20:50
19

TL;DR:

Run this as root and you'll be all set:

rm $(grep socket /etc/my.cnf | cut -d= -f2)  && service mysqld start

Longer version:

You can find the location of MySQL's socket file by manually poking around in /etc/my.conf, or just by using

grep socket /etc/my.cnf | cut -d= -f2

It is likely to be /var/lib/mysql/mysql.sock. Then (as root, of course, or with sudo prepended) remove that file:

rm /var/lib/mysql/mysql.sock

Then start the MySQL daemon:

service mysqld start

Removing mysqld will not address the problem at all. The problem is that CentOS & RedHat do not clean up the sock file after a crash, so you have to do it yourself. Avoiding powering off your system is (of course) also advised, but sometimes you can't avoid it, so this procedure will solve the problem.

Community
  • 1
  • 1
iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • The `cut -d= -f2` bit might appear wrong, because you'd expect a value to appear after the `=` sign, but the `=` *is* the value passed to the `-d` option: in other words, `=` is used as the delimiter for `cut`. – iconoclast Jul 17 '14 at 13:40
  • If you're on Ubuntu, try replacing `/etc/my.cnf` with `/etc/mysql/my.cnf`. – iconoclast Jul 17 '14 at 13:41
  • I'd suggest greping something more specific then any line containing the word _socket_, at least `grep '^socket[[:space:]]=' /etc/my.cnf`, or you may end up deleting something you didn't intend. – Beli Oct 20 '15 at 10:30
4

I have found a solution for anyone in this problem change the socket dir to a new location in my.cnf file

socket=/var/lib/mysql/mysql2.sock

and service mysqld start

or the fast way as GeckoSEO answered

# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak

# service mysqld start
Mas
  • 1,267
  • 1
  • 14
  • 13
3

My solution to this was a left over mysql.sock in the /var/lib/mysql/ directory from a hard shutdown. Mysql thought it was already running when it was not running.

  • yeah you are right I made shutdown to the system then this problem arises , so you have a solution when I start server not to show this error – Mas Dec 08 '13 at 03:25
2

Just open a bug report with your OS vendor asking them to put the socket in /var/run so it automagically gets removed at reboot. It's a bug to keep this socket after an unclean reboot, /var/run is the spot for these kinds of files.

Paul
  • 21
  • 1
1

in order to clean automatically .sock file, place these lines in file /etc/init.d/mysqld immediately after "start)" block of code

test -e /var/lib/mysql/mysql.sock
SOCKEXIST=$?

ps cax | grep mysqld_safe
NOPIDMYSQL=$?

echo NOPIDMYSQL $NOPIDMYSQL
echo SOCKEXIST $SOCKEXIST

if [ $NOPIDMYSQL -eq 1 ] && [ $SOCKEXIST -eq 0 ] ; then
    echo "NOT CLEAN"
    rm -f /var/lib/mysql/mysql.sock
    echo "FILE SOCK REMOVED"
else
    echo "CLEAN"
fi

it worked for me. I had to do this because I have not an UPS and often we have power supply failures.

regards.

ugo
  • 19
  • 1
  • The above is the way to go since it happens out of our control that power goes out and UPS dies after a while. So the server must recover. I have this problem on a mysql 5.7 (centos 6) installation but not on a 5.1 (centos 5). I don't have time to investigate what is wrong with the first one so I'm just gonna improve on this answer and use that. test -e $socketfile if [ $? -eq 0 ]; then – ciuly Sep 03 '17 at 06:21
  • looks like it took me more than 5 min to prepare the pastebin: here it is https://pastebin.com/DpiSGrmh – ciuly Sep 03 '17 at 06:27
1

It may sometime arises when MySQL service does not shut down properly during the OS reboot. The /var/lib/mysql/mysql.sock has been left. This prevents 'mysqld' from starting up.

These steps may help:

1: service mysqld start killall -9 mysqld_safe mysqld service mysqld start

2: rm /var/lib/mysql/mysql.sock service mysqld start

Vikash Kumar
  • 575
  • 5
  • 6
0

To start the MySQL service, you can remove '/var/lib/mysql/mysql.sock' and start the MySQL service again:

Remove the socket file:

[root@server ~]# rm /var/lib/mysql/mysql.sock
rm: remove socket `/var/lib/mysql/mysql.sock'? yes

Start the MySQL service:

[root@server~]# service mysqld start
Starting mysqld:                                           [  OK  ]

It will help you to resolve your problem.

Kamesh Jungi
  • 6,203
  • 6
  • 39
  • 50
0

It's just happen because of abnormal termination of mysql service. delete or take backup of /var/lib/mysql/mysql.sock file and restart mysql.

Please let me know if in case any issue..

Balkrushna Patil
  • 418
  • 6
  • 12
-1

I just went through this issue and none of the suggestions solved my problem. While I was unable to start MySQL on boot and found the same message in the logs ("Another MySQL daemon already running with the same unix socket"), I was able to start the service once I arrived at the console.

In my configuration file, I found the following line: bind-address=xx.x.x.x. I randomly decided to comment it out, and the error on boot disappeared. Because the bind address provides security, in a way, I decided to explore it further. I was using the machine's IP address, rather than the IPv4 loopback address - 127.0.0.1.

In short, by using 127.0.0.1 as the bind-address, I was able to fix this error. I hope this helps those who have this problem, but are unable to resolve it using the answers detailed above.

webjawns.com
  • 2,300
  • 2
  • 14
  • 34