Can’t Connect To Local Mysql Server Through Socket Var Run Mysqld Mysqld Sock – Local Mysql Socket Connection

MySQL cannot find the socket file at the specified path, indicating the database server may not be running. If you’ve ever seen the error “can’t connect to local mysql server through socket var run mysqld mysqld sock”, you know how frustrating it can be. This common issue stops your applications from connecting to the database, but it’s usually fixable with a few simple steps.

In this guide, we’ll walk you through exactly what causes this error and how to resolve it. You’ll learn practical solutions that work on Linux servers, especially Ubuntu and CentOS. Let’s get your MySQL connection back up and running.

What Does This Error Mean?

The error message tells you that MySQL client cannot locate the socket file at /var/run/mysqld/mysqld.sock. A socket file is like a communication pipe between MySQL client and server on the same machine. When it’s missing, the client can’t talk to the server.

Think of it as trying to call someone but their phone is off. The server might be stopped, crashed, or the socket path might be wrong. Either way, you get the same error.

Can’t Connect To Local Mysql Server Through Socket Var Run Mysqld Mysqld Sock

This exact error appears when your MySQL client tries to connect locally but finds no socket file. Let’s break down the most common causes and solutions.

Common Causes Of This Error

  • MySQL service is not running
  • Socket file is in a different location
  • Permissions issues on the socket directory
  • MySQL configuration file points to wrong path
  • System reboot without restarting MySQL
  • Disk space full preventing socket creation

Step 1: Check If MySQL Service Is Running

First, verify the MySQL service status. Open your terminal and run:

sudo systemctl status mysql

Or for older systems:

sudo service mysql status

If the service is inactive or failed, you’ll see “Active: inactive (dead)” or “Active: failed”. This confirms MySQL isn’t running, which explains the missing socket.

What To Do If MySQL Is Not Running

Start the service with:

sudo systemctl start mysql

Then enable it to start on boot:

sudo systemctl enable mysql

Check the status again. If it starts successfully, your socket file should appear at /var/run/mysqld/mysqld.sock.

Step 2: Verify The Socket File Exists

Even if MySQL is running, the socket might be in a different location. Check for the file:

ls -la /var/run/mysqld/mysqld.sock

If you get “No such file or directory”, the socket isn’t there. Try searching for it:

find / -name "mysqld.sock" 2>/dev/null

This command searches your entire system for the socket file. Common alternative locations include:

  • /tmp/mysql.sock
  • /var/lib/mysql/mysql.sock
  • /var/run/mysqld/mysqld.sock
  • /var/run/mysql/mysql.sock

Step 3: Check MySQL Configuration

MySQL configuration files tell the client where to find the socket. Check these files:

sudo nano /etc/mysql/my.cnf

Or:

sudo nano /etc/my.cnf

Look for the [client] section and a line like:

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

Also check the [mysqld] section for the same socket path. If the paths don’t match, update them to point to the actual socket location you found in Step 2.

Example Configuration Fix

If your socket is at /tmp/mysql.sock, edit the config file:

[client]
socket = /tmp/mysql.sock

[mysqld]
socket = /tmp/mysql.sock

Save the file and restart MySQL:

sudo systemctl restart mysql

Step 4: Check Directory Permissions

The MySQL user must have write permission to the directory containing the socket. Check permissions:

ls -la /var/run/mysqld/

You should see something like:

drwxr-xr-x 2 mysql mysql 60 Jan 15 10:30 .

If the owner isn’t mysql, fix it:

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

Also ensure the directory has correct permissions:

sudo chmod 755 /var/run/mysqld/

Step 5: Check Disk Space

A full disk can prevent MySQL from creating the socket file. Check disk usage:

df -h

If any partition is at 100%, free up space by removing old logs or temporary files. Then restart MySQL.

Step 6: Use TCP/IP Connection Instead

As a temporary workaround, you can connect via TCP/IP instead of socket. Use the -h flag with localhost:

mysql -h 127.0.0.1 -u root -p

This bypasses the socket file entirely. If this works, your MySQL server is running but the socket configuration is wrong.

Step 7: Check MySQL Error Logs

Error logs often reveal why MySQL failed to start. Check them:

sudo tail -100 /var/log/mysql/error.log

Or:

sudo journalctl -u mysql -n 50

Look for lines mentioning “socket”, “permission denied”, or “can’t create”. Common errors include:

  • InnoDB corruption
  • Out of memory
  • Port already in use
  • Missing data directory

Step 8: Reinstall Or Repair MySQL

If nothing works, you might need to reinstall MySQL. First, backup your databases:

mysqldump --all-databases > backup.sql

Then remove and reinstall:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get install mysql-server mysql-client

After reinstallation, restore your backup:

mysql -u root -p < backup.sql

Advanced Troubleshooting Techniques

Sometimes basic steps don't work. Here are more advanced methods.

Using Mysqladmin To Test Connection

The mysqladmin tool can test connectivity:

mysqladmin -u root -p ping

If MySQL is running, you'll see "mysqld is alive". If not, you'll get an error.

Creating A Symbolic Link

If the socket is in a non-standard location, create a symlink:

sudo ln -s /actual/path/mysqld.sock /var/run/mysqld/mysqld.sock

Replace /actual/path with the real socket location. This tricks the client into finding the socket.

Checking AppArmor Or SELinux

Security modules like AppArmor or SELinux can block socket creation. Check AppArmor status:

sudo aa-status | grep mysql

If MySQL is confined, you may need to adjust the profile. For SELinux:

sudo getenforce

If it's enforcing, try temporarily disabling it:

sudo setenforce 0

Then test MySQL. If it works, create an SELinux policy for MySQL.

Checking For Multiple MySQL Instances

Multiple MySQL installations can conflict. Check for other instances:

ps aux | grep mysql

If you see multiple processes, stop all but one. Also check for conflicting ports:

sudo netstat -tlnp | grep 3306

Only one process should listen on port 3306.

Preventing This Error In The Future

Once you fix the issue, take steps to prevent it from happening again.

Enable MySQL To Start On Boot

Ensure MySQL starts automatically after system reboots:

sudo systemctl enable mysql

This prevents the socket error after server restarts.

Monitor MySQL Service

Set up monitoring to alert you if MySQL stops. Tools like Monit or Nagios can check the service and restart it automatically.

Regular Backups Of Configuration

Backup your MySQL configuration files:

sudo cp /etc/mysql/my.cnf ~/my.cnf.backup

This helps restore settings quickly if something changes.

Keep System Updated

Regular updates fix bugs that cause MySQL crashes:

sudo apt-get update && sudo apt-get upgrade

Always backup before major updates.

Frequently Asked Questions

Why Do I Get "Can't Connect To Local Mysql Server Through Socket Var Run Mysqld Mysqld Sock" After Reboot?

After a system reboot, MySQL might not start automatically. Check if the service is enabled to start on boot using systemctl enable mysql. Also verify the socket directory exists after reboot, as some systems clear /var/run on restart.

Can I Change The MySQL Socket Location?

Yes, edit the [mysqld] and [client] sections in your MySQL configuration file. Change the socket parameter to your desired path, then restart MySQL. Ensure the directory exists and has correct permissions.

What If The Socket File Exists But I Still Get The Error?

This usually means permission issues. Check that the socket file is readable by your user. Also verify the [client] section in your config file points to the exact same path. Run mysql -S /path/to/socket to test directly.

How Do I Connect Without Using A Socket?

Use TCP/IP connection by specifying -h 127.0.0.1 instead of -h localhost. This bypasses the socket and uses the network protocol. You can also set protocol=TCP in your connection string.

Is This Error Specific To Linux?

Yes, Unix socket files are a Linux/Unix feature. On Windows, MySQL uses named pipes or TCP/IP instead. The Windows equivalent error would be "Can't connect to MySQL server on 'localhost' (10061)".

Final Thoughts

The "can't connect to local mysql server through socket var run mysqld mysqld sock" error is frustrating but fixable. Most of the time, starting MySQL service or fixing the socket path resolves it. Always check the basics first: is MySQL running, does the socket file exist, and are permissions correct?

Remember to check your error logs for clues. They often tell you exactly what went wrong. With the steps in this guide, you should be able to get your MySQL connection working again quickly.

If you're still stuck after trying everything, consider seeking help from your hosting provider or a MySQL specialist. Sometimes the issue is deeper, like corrupted system files or hardware problems. But for most users, these solutions will do the trick.

Keep your MySQL configuration backed up and monitor your service regularly. This prevents future headaches and keeps your applications running smoothly. Good luck, and happy databasing!