MySQL
database connection issue
socket error
troubleshooting
server configuration

Can't connect to local MySQL server through socket '/tmp/mysql.sock

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error about '/tmp/mysql.sock' means the client tried to connect through a local Unix socket and could not find or use the socket file it expected. In practice, the cause is usually one of three things: the server is not running, the client is looking at the wrong socket path, or the client should have been using TCP instead of a local socket.

Why MySQL Uses a Socket

On Unix-like systems, a MySQL client often uses a Unix socket for local connections. This is common when you connect with no host or with localhost.

That behavior matters because localhost and 127.0.0.1 are not equivalent for MySQL clients:

  • 'localhost often means “use the local socket”'
  • '127.0.0.1 forces a TCP connection to the local machine'

So one of the fastest diagnostic steps is to decide whether you intended a socket connection or a TCP connection.

First Checks

Start with the simplest questions.

  1. Is the MySQL server running?
  2. Which socket path is the server actually configured to use?
  3. Is the client reading the same configuration?

These shell commands are a good starting point:

bash
mysqladmin ping
ps aux | grep mysqld
mysql --help | grep socket

If the server is down, fix that first. No socket troubleshooting matters until mysqld is running.

Check the Configured Socket Path

The server and client may disagree about the socket file location. Common locations include /tmp/mysql.sock, /var/run/mysqld/mysqld.sock, and platform-specific package paths.

Inspect your MySQL configuration files and compare the client and server settings.

ini
1[client]
2socket=/var/run/mysqld/mysqld.sock
3
4[mysqld]
5socket=/var/run/mysqld/mysqld.sock

If the server uses one path and the client looks at another, the error message will mention a missing socket even though the server is alive.

Force TCP When Appropriate

Sometimes the easiest fix is to stop using the socket path entirely. If the server is listening on port 3306 and you want a normal local network connection, specify 127.0.0.1 instead of localhost.

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

That bypasses the socket lookup and tests whether the server is reachable over TCP.

This is especially useful when:

  • the socket path is unclear
  • the client is inside a container or another environment where the host socket file is not visible
  • local TCP is acceptable for the workflow

Recreate the Socket by Restarting the Server

If the configured path is correct but the file does not exist, the server may not have started cleanly. In that case, inspect the server logs and restart the service.

bash
sudo systemctl restart mysql
sudo systemctl status mysql

On systems without systemd, use the service manager provided by the platform. The main point is to check startup logs rather than guessing.

A missing socket file often reflects an earlier failure such as:

  • permission issues on the run directory
  • a data directory problem
  • another process already using the port
  • a bad configuration change that prevents startup

Permissions and Environment Problems

The directory containing the socket file must exist and be writable by the MySQL server process. If /var/run/mysqld is missing or owned incorrectly, the server may fail before it ever creates the socket.

Be careful with quick fixes like creating random symlinks from /tmp/mysql.sock to another path. A symlink can mask the real configuration mismatch and create harder-to-debug behavior later.

Common Pitfalls

The most common pitfall is assuming localhost means TCP. With MySQL clients, it often means “try the Unix socket first”.

Another mistake is changing only the server socket path and forgetting the client section in the configuration. Both sides need to agree.

A third issue is debugging the socket path when the server is actually dead. Always confirm the daemon is running before changing client settings.

Summary

  • The error means the client expected a local Unix socket and could not use it.
  • First confirm that mysqld is running.
  • Check whether the client and server agree on the socket path.
  • Use 127.0.0.1 to force TCP when a socket is not required.
  • Avoid symlink hacks until you understand the real configuration mismatch.

Course illustration
Course illustration

All Rights Reserved.