When MySQL fails to start and reports a missing socket file (/var/run/mysqld/mysqld.sock
), it typically indicates one of several fundamental issues:
# Common error you'll see in logs
$ tail -n 20 /var/log/mysql/error.log
[ERROR] Can't start server: Bind on Unix socket: No such file or directory
[ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld.sock?
Before attempting fixes, verify these critical points:
# Check if MySQL process is running
$ ps aux | grep mysqld
# Verify socket directory exists
$ ls -la /var/run/mysqld/
# Check permissions (mysql user should own the directory)
$ ls -ld /var/run/mysqld
1. Create Missing Directory Structure
Modern systems using systemd may require manual directory creation:
# Create directory with proper permissions
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld
$ sudo chmod 755 /var/run/mysqld
2. Configure Alternative Socket Path
If filesystem restrictions exist, modify my.cnf
:
[mysqld]
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
3. Systemd-Specific Fixes
For systems using systemd, create proper runtime directory:
# Create systemd service override
$ sudo mkdir -p /etc/systemd/system/mysql.service.d
$ sudo nano /etc/systemd/system/mysql.service.d/socket.conf
[Service]
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
Checking File Descriptors and Process Limits
# Check system limits
$ ulimit -n
# Verify in-use sockets
$ sudo lsof -U | grep mysql
Debugging with MySQL Safe Mode
$ mysqld_safe --skip-grant-tables --skip-networking &
$ mysql -u root
Add these to your server maintenance checklist:
- Configure log rotation for MySQL error logs
- Implement monitoring for socket file existence
- Create startup pre-checks in init scripts
#!/bin/bash
# Sample pre-start check
if [ ! -d "/var/run/mysqld" ]; then
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
fi
When your MySQL server refuses to start after a system reboot and complains about a missing socket file, you're facing a common yet frustrating issue. The error message clearly indicates the system cannot locate /var/run/mysqld/mysqld.sock
, which is crucial for local connections.
Before diving into solutions, verify the current state with these commands:
sudo systemctl status mysql
ls -la /var/run/mysqld/
ps aux | grep mysql
- Permissions issues in
/var/run/mysqld/
- Incorrect my.cnf configuration
- MySQL not having write access to the directory
- AppArmor/SELinux restrictions
- Filesystem corruption or full disk
First, ensure the directory exists with proper permissions:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
Then check your MySQL configuration (typically in /etc/mysql/my.cnf
or /etc/my.cnf
):
[mysqld]
socket=/var/run/mysqld/mysqld.sock
[client]
socket=/var/run/mysqld/mysqld.sock
If the issue persists, try forcing a clean start:
sudo systemctl stop mysql
sudo rm -f /var/lib/mysql/ib_logfile*
sudo mysqld --initialize
sudo systemctl start mysql
For systems with AppArmor/SELinux:
# For AppArmor
sudo aa-status | grep mysql
sudo aa-complain /usr/sbin/mysqld
# For SELinux
sudo restorecon -v /var/run/mysqld/mysqld.sock
While fixing the socket issue, you can temporarily connect via TCP:
mysql -h 127.0.0.1 -u root -p
Update your applications to use TCP connection if necessary:
$db = new mysqli('127.0.0.1', 'user', 'password', 'database', 3306);
Add these to your server maintenance checklist:
- Regularly verify MySQL's systemd unit file
- Monitor disk space in /var
- Implement proper log rotation
- Test service recovery after reboots