When MySQL fails to start with the classic socket error message, it typically indicates one of several underlying issues. The error you're seeing suggests the MySQL server isn't creating its socket file during startup.
Before attempting fixes, let's verify the current state:
sudo systemctl status mysql
sudo journalctl -xe
ls -la /var/run/mysqld/
The missing socket file could be caused by:
- Incorrect permissions on /var/run/mysqld
- MySQL configuration pointing to wrong socket location
- Insufficient disk space
- Corrupted InnoDB tables
Here's how to systematically address this issue:
1. Check and Repair Directory Structure
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo chmod 755 /var/run/mysqld
2. Verify MySQL Configuration
Check your my.cnf or mysqld.cnf file:
sudo nano /etc/mysql/my.cnf
# Ensure these lines exist under [mysqld]:
# socket = /var/run/mysqld/mysqld.sock
# pid-file = /var/run/mysqld/mysqld.pid
3. Manual Startup Attempt
Try starting MySQL manually to see detailed errors:
sudo mysqld --console
# or for debugging:
sudo mysqld --skip-grant-tables --skip-networking --console
4. Check for Disk Space Issues
df -h
sudo du -sh /var/lib/mysql/
5. Repair MySQL System Tables
If corruption is suspected:
sudo mysql_upgrade -u root -p
sudo mysqlcheck --all-databases --repair --optimize -u root -p
If the issue persists, you can try connecting through TCP instead of socket:
mysql -h 127.0.0.1 -u root -p
Or temporarily change the socket location in your configuration.
After applying fixes:
sudo systemctl start mysql
sudo systemctl status mysql
mysqladmin -u root -p ping
For persistent issues, check your MySQL error logs (typically at /var/log/mysql/error.log) for detailed startup messages.
When you encounter the error message indicating MySQL can't connect through the socket file, it typically means one of these scenarios:
- The MySQL service isn't running
- The socket file was deleted or never created
- Permissions prevent MySQL from accessing the socket
- The MySQL configuration points to a non-existent socket path
First, verify if MySQL is actually running:
sudo systemctl status mysql
ps aux | grep mysqld
Check if the socket directory exists:
ls -la /var/run/mysqld/
Try starting MySQL manually with more verbose output:
sudo mysqld_safe --skip-grant-tables --skip-networking &
This bypasses some authentication checks and lets you examine the error output directly.
If the directory exists but the socket is missing, you can try:
sudo touch /var/run/mysqld/mysqld.sock
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
sudo chmod 777 /var/run/mysqld/mysqld.sock
Then restart MySQL:
sudo systemctl restart mysql
Examine your MySQL configuration for socket path settings:
sudo grep -R "socket" /etc/mysql/
Common locations include:
/etc/mysql/my.cnf
/etc/mysql/mysql.conf.d/mysqld.cnf
/etc/mysql/mariadb.conf.d/50-server.cnf
While fixing the socket issue, you can connect via TCP:
mysql -h 127.0.0.1 -u root -p
Always check MySQL error logs for detailed information:
sudo tail -n 50 /var/log/mysql/error.log
As a last resort, completely purge and reinstall MySQL:
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 back up your databases before attempting this!
To prevent future occurrences:
- Configure proper log rotation for MySQL
- Set up monitoring for the MySQL service
- Regularly verify socket file permissions
- Consider using TCP connections instead of sockets for critical applications