Mysql. Bind on unix socket: Permission denied

Solution 1:

1) First make sure you delete the existing socket file using the rm command.

rm /var/lib/mysql/mysql.sock

2) Kill all existing MySQL processes via the following command.

service mysql stop

3) Very important: Make sure the permission for the MySQL database directory under /var/lib/ folder should be mysql:root

chown -R mysql:root /var/lib/mysql

4) With the above information you should be able to start MySQL successfully and the socket file should be created successfully under the database directory.

service mysql start

Solution 2:

Thanks to @Abhishek's answer combined with mysqld output(Can't use /var/run/mysqld/mysqld.sock) I managed to solve this with one command:

chown -R mysql:root /var/run/mysqld

Then it starts properly:

root@***:~# service mysql start
df: Warning: cannot read table of mounted file systems: No such file or directory
[ ok ] Starting MySQL database server: mysqld ..
[info] Checking for tables which need an upgrade, are corrupt or were 
not closed cleanly..

Solution 3:

Your problem can be caused by any condition that prevents the MySQL daemon from writing a socket file to the path /var/lib/mysql. The other answers, such as a read-only filesystem or incorrect directory ownership, address various reasons that MySQL may not be able to write to the socket.

Another possible reason is that /var/lib/mysql may have the incorrect SELinux context. You can diagnose this condition by checking for recent SELinux AVC denials after attempting to start the MySQL daemon:

ausearch -m avc --start recent

If SELinux is denying access to create the socket file, you will see an error message like this:

time->Thu Feb  2 03:46:27 2017
type=SYSCALL msg=audit(1566106187.784:152): arch=c000004e syscall=49 success=no exit=-13 a0=4f a1=7ff3fd652e80 a2=6e a3=7fcd2d6523d40 items=0 ppid=1 pid=1820 auid=4294967295 uid=996 gid=994 euid=996 suid=996 fsuid=996 egid=994 sgid=994 fsgid=994 tty=(none) ses=4294967295 comm="mysqld" exe="/usr/sbin/mysqld" subj=system_u:system_r:mysqld_t:s0 key=(null)
type=AVC msg=audit(1566106187.784:152): avc:  denied  { create } for  pid=1820 comm="mysqld" name="mysql.sock" scontext=system_u:system_r:mysqld_t:s0 tcontext=system_u:object_r:var_log_t:s0 tclass=sock_file

To resolve an SELinux denial, try to restore the default context of the directory (your distribution should set the correct context of mysqld_db_t when you installed MySQL or MariaDB):

restorecon -R -v -f /var/lib/mysql

If you have customized the contexts on your system, fixing the problem is beyond the scope of this post.

Tags:

Mysql

Centos