MySQL Socket File Missing: Troubleshooting /var/run/mysqld/mysqld.sock Connection Issues


2 views

When MySQL fails to create its socket file at /var/run/mysqld/mysqld.sock, you'll typically encounter the dreaded error:

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

The mysqld.sock file is a Unix domain socket that enables local processes to communicate with the MySQL server without using network protocols. Unlike TCP/IP connections, socket connections are:

  • Faster (no network stack overhead)
  • More secure (local system only)
  • Required for certain administrative operations

Since systemd became prevalent, several factors can cause this issue:

  1. Service not running: Check with systemctl status mysql
  2. Custom socket path: Configured in /etc/mysql/my.cnf
  3. Permission issues: The /var/run/mysqld/ directory needs proper ownership

Before considering a OS reinstall, try these diagnostic commands:

# Check if MySQL is actually running
ps aux | grep mysqld

# Find alternative socket locations
sudo find / -name "*.sock" 2>/dev/null

# Check the configured socket path
mysql --help | grep "socket"

If the socket is truly missing, here's how to properly recreate it:

# Stop MySQL service
sudo systemctl stop mysql

# Ensure the directory exists with correct permissions
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

# Start MySQL with explicit socket parameter
sudo mysqld --socket=/var/run/mysqld/mysqld.sock &

Add these lines to your /etc/mysql/my.cnf under the [mysqld] section:

[mysqld]
socket=/var/run/mysqld/mysqld.sock

[client]
socket=/var/run/mysqld/mysqld.sock

While fixing the socket issue, you can temporarily connect via TCP/IP:

mysql -h 127.0.0.1 -u root -p

Simply creating an empty file won't work because:

  • MySQL needs to bind to the socket itself
  • The file must have special socket attributes (not regular file)
  • Proper permissions must be maintained throughout service restarts

KDE may be using a different connection method if your desktop works:

# Check active MySQL connections
sudo lsof -U | grep mysql

When MySQL fails to create its socket file during installation or upgrade, you'll encounter connection errors that can derail your development workflow. The socket file (/var/run/mysqld/mysqld.sock) acts as a local communication channel between MySQL clients and the server.

Several factors could prevent socket file creation:

1. Permission issues in /var/run/mysqld
2. MySQL service failing to start properly
3. Custom configuration overriding default paths
4. Conflict with AppArmor/SELinux policies
5. Upgrade artifacts from previous installations

First verify if MySQL is actually running:

sudo service mysql status
ps aux | grep mysqld

Check configured socket locations:

mysql --help | grep sock
# Or examine my.cnf files:
sudo grep -r "socket" /etc/mysql/

Method 1: Manual Socket Creation

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo service mysql restart

Method 2: Alternative Connection via TCP

mysql -h 127.0.0.1 -P 3306 -u root -p

Method 3: Forcing Socket Recreation

sudo pkill mysqld
sudo rm -f /var/run/mysqld/mysqld.sock
sudo service mysql start

Examine AppArmor denials:

sudo tail -f /var/log/syslog | grep -i denied

Create custom AppArmor profile if needed:

/var/run/mysqld/mysqld.sock rw,

Complete removal and fresh install:

sudo apt-get purge mysql-server mysql-client mysql-common
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get install mysql-server

Remember to backup your databases before any destructive operations.

Verify socket communication works:

mysqladmin -u root -p ping
# Should return: mysqld is alive