MySQL error 2002 means the client cannot reach mysqld through the Unix socket it selected. Check that the server is running, read the socket path printed in the error, test that path explicitly, and make the client and server use the same socket. If 127.0.0.1 works while localhost fails, the server is reachable and the fault is almost certainly socket selection, the socket path, or filesystem access rather than the database engine itself.
Antihost offers Linux VPS plans with full root access, unlimited traffic on a 1 Gbps port and a 14-day free trial.
What MySQL socket error 2002 actually means
A Unix socket is a special filesystem endpoint used for local inter-process communication. When a MySQL client connects without a host, or names the host as localhost, MySQL programs normally attempt a Unix-socket connection. The client does not send traffic through the network stack in that mode; it opens a socket file on the same machine and asks the local mysqld process to accept the connection.
As an Amazon Associate I earn from qualifying purchases.
Error 2002 is therefore a connection-path error, not a diagnosis of corrupted tables or bad SQL. The client may be looking in the wrong place, the server may be stopped, the socket may have been removed, or the operating-system permissions may prevent access. A client and server can also be healthy while disagreeing about the socket filename. MySQL documents /tmp/mysql.sock as a default, but packages and administrators frequently configure another directory, including locations under /var/lib/mysql.
The complete error normally includes the path the client tried. Preserve that path; it is the most useful clue in the message. A line such as Can't connect to local MySQL server through socket '/some/path/mysql.sock' (2) tells you which endpoint to verify. Do not assume that the path is correct merely because it appears in the error.
#1 Best Overall
A five-minute triage sequence
- Copy the entire error. Note the socket filename, the numeric error code, and whether the command used
localhost, no host, or an explicit IP address. - Check the daemon before changing configuration. On a systemd installation, inspect the unit used by your distribution. Common names are
mysql.serviceon Debian-family packages andmysqld.serviceon RPM-family packages.sudo systemctl status mysql sudo systemctl status mysqldOnly one of those units may exist. A failed status should be followed by the MySQL error log, because the log usually explains why startup stopped.
- Run one explicit socket test. Substitute the path shown in your error or the path from the active server configuration:
mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock versionThis removes ambiguity about host-name interpretation. If it succeeds, the server and socket are usable and the ordinary client command is selecting a different path or protocol.
- Run a TCP comparison. Use the loopback address and the configured port:
mysql --protocol=TCP -h 127.0.0.1 -P 33063306is MySQL’s default TCP port, not a guarantee that your installation uses it. Replace it when the server is configured for another port. - Choose the branch indicated by the tests. A working TCP test with a failing socket test points to socket selection, path, lifecycle, or permissions. Failure of both tests sends you back to service state, startup logs, port settings, networking options, and firewall rules.
Socket and TCP connections are different paths
The distinction explains why two apparently equivalent commands can produce different results. The following behavior is the basis for the troubleshooting branches.
| Client form | Protocol selected | Endpoint used | What a result tells you |
|---|---|---|---|
| No host supplied | Unix socket on Unix systems | Client’s configured socket file | A failure can be caused by a stopped server, wrong path, missing file, or permissions. |
-h localhost |
Unix socket on Unix systems | Client’s configured socket file | Name resolution is not the key test; the local socket setting is. |
-h 127.0.0.1 |
TCP/IP | Loopback address and configured TCP port | A success isolates the problem to the socket path or socket access. A failure requires server, port, bind, firewall, or networking checks. |
| A remote host name or address | TCP/IP | Remote address and configured TCP port | A Unix socket cannot cross machines; remote access requires TCP and an accepting server configuration. |
Changing localhost to 127.0.0.1 can be a useful diagnostic or a temporary workaround, but it does not repair a missing socket. If local applications are expected to use a socket, correct the socket configuration instead of silently changing every application to TCP.
Recommended Free Tools
Step-by-step repair procedure
1. Confirm which MySQL service is installed and whether it is running
The service manager is the first authority for the daemon’s state. A unit that is inactive, failed, or missing explains why no process can answer on either a socket or TCP port. Query the likely unit names separately rather than assuming that one label applies to every operating-system package:
sudo systemctl status mysql
sudo systemctl status mysqld
If the status shows a startup failure, read the MySQL error log named by your installation’s configuration or service output. Look for the first startup error, not only the final “failed” line. Configuration syntax errors, an unusable data directory, an occupied port, and permission problems can all prevent the daemon from creating its socket. Do not delete database files or reinstall while the cause is still unknown.
After correcting a clearly identified startup issue, restart the unit that actually exists:
sudo systemctl restart mysql
sudo systemctl restart mysqld
Use one command, not both. Immediately check status again and then repeat the explicit socket or TCP test. A restart is a verification step after a change; it is not a substitute for reading the failure log.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
2. Compare the path in the error with the server’s active socket setting
Read the path printed by the client character for character. Compare it with the socket setting used by mysqld and with the client-side setting used by the mysql and mysqladmin programs. The documented default /tmp/mysql.sock is only a starting point. Distribution packages may select a directory under the MySQL data area, and a custom configuration can choose any suitable local path.
There are two separate values to reconcile:
- Server value: where
mysqldcreates and listens on its socket. - Client value: where a local client looks when it chooses socket protocol.
If those values differ, the server can be fully operational while the client reports error 2002. Correct the configuration used by the component you control, then restart mysqld so that the server creates its endpoint at the intended location. Keep the client and server settings identical, including directory and filename.
3. Test the suspected socket without relying on defaults
Use the explicit protocol and path form while diagnosing:
mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock version
Replace /path/to/mysql.sock with the actual configured path. The version operation is a lightweight reachability check; it does not require you to select a database. You can use the same path with the interactive client:
Free tools Windows power users keep installed
One-click scans. No signup required.
mysql --protocol=SOCKET --socket=/path/to/mysql.sock
Interpret the result narrowly. Success proves that a server is answering through that path and that the invoking user can open it. It does not prove that every application has the same client configuration. Failure means you should inspect service state, the existence of the file, the containing directory, and permissions before trying another path at random.
4. Check whether the socket file exists and is being recreated
A socket file is created when the server starts and is removed when the server stops. It can also disappear if an automated cleanup task removes files from a temporary directory. If the server is running but the expected file is absent, compare the actual server setting with the path in the error and inspect the directory named by that setting.
/tmp is convenient but may be subject to temporary-file cleanup. If a scheduled cleanup process is deleting the endpoint, protect the socket from that policy or configure MySQL to use a more suitable persistent runtime directory. The replacement directory must already exist with ownership and permissions that allow the daemon to create the socket and authorized clients to open it. Moving the socket requires changing both server and client configuration; changing only one side creates the same error under a new filename.
5. Correct filesystem permissions
Unix access is checked twice: the client must be able to traverse the parent directories, and it must be allowed to open the socket file. The server also needs permission to create the file in its configured directory. A path can exist and still be unusable to an application running as another user.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →- Check the owner and group of the socket and its parent directory.
- Check execute permission on every parent directory in the path.
- Compare the failing client’s user and group memberships with the permissions shown.
- Check whether a security policy or service sandbox restricts access, if your operating system uses one.
Fix the narrowest permission problem that explains the failure. Avoid making the socket or its parent world-writable as a quick workaround; broad permissions can expose a local database interface to unintended users. After a permission change, rerun the explicit mysqladmin test as the same account that failed originally.
6. Use the TCP test to separate socket faults from server-wide faults
Try loopback TCP with the port that the server is configured to use:
mysql --protocol=TCP -h 127.0.0.1 -P 3306
If this connects while the socket command fails, the daemon is accepting at least one local protocol and the investigation should remain focused on socket path selection, file lifecycle, or filesystem access. If the TCP attempt reports connection refusal or timeout as well, a socket-only explanation is insufficient. Check whether the server is running, whether it is listening on the expected port, and whether configuration disables networking or binds the daemon to an address that excludes loopback.
MySQL documents skip_networking and bind_address as settings that affect TCP availability. A firewall can also block the configured port, even for some network paths. Verify the actual port rather than assuming 3306, and inspect firewall rules only after confirming the daemon is configured to listen there.
Which service should you restart?
The correct unit name follows the package family, not the error message. Use the unit that returns a real status on your system.
| Installation family | Common systemd unit | Commands to inspect and restart |
|---|---|---|
| Debian-family packages | mysql.service |
sudo systemctl status mysqlsudo systemctl restart mysql |
| RPM-family packages | mysqld.service |
sudo systemctl status mysqldsudo systemctl restart mysqld |
These are common names, not a promise for every vendor build or manually installed server. If neither unit exists, identify how your installation was started and use that service definition. Do not launch a second mysqld process simply because the expected unit name is absent; two daemons can compete for the same data directory, port, or socket.
When localhost fails but 127.0.0.1 works
This is the most informative pattern in socket troubleshooting. localhost normally selects a Unix socket on Unix systems, whereas 127.0.0.1 selects TCP/IP. A successful loopback connection proves that the server is reachable through the configured TCP port. It does not validate the socket filename used by local clients.
Work through these checks in order:
- Copy the socket path from the failing
localhosterror. - Run
mysqladmin --protocol=SOCKET --socket=...with that exact path. - Compare the path with the server’s
socketsetting. - Check the socket file and parent-directory permissions for the failing user.
- Check whether a cleanup process removed a socket located in
/tmp.
Keeping TCP as a deliberate configuration can be reasonable for an application that already specifies a port, but it changes authentication, firewall, and bind-address considerations. Treat it as a design choice, not evidence that the socket error was harmless.
When both socket and TCP connections fail
Two failed protocols widen the search. Start with the daemon and its startup log, because a stopped server cannot create a socket or accept a port connection. If the daemon is running, verify each endpoint independently:
- Confirm the active socket path and whether its file exists.
- Confirm the configured TCP port;
3306is only the default. - Check whether
skip_networkingdisables TCP. - Check
bind_addressto ensure the intended local or remote interface is included. - Review firewall rules for the configured port.
- Check permissions for the socket directory and file.
A remote connection adds another layer: Unix sockets are local to one machine, so a remote client must use TCP. A local loopback failure and a remote failure may share a server-side cause, but a remote-only failure can also be explained by bind or firewall settings.
Best Value
Common symptoms and the right interpretation
“No such file or directory” for the socket
This usually means the client path does not contain a socket at the time of the attempt. Verify that mysqld is running, then compare client and server paths. If the configured directory is temporary, investigate automated cleanup. Do not create an ordinary empty file with the socket’s name; only the server can create a usable Unix socket endpoint.
“Connection refused” after switching to TCP
The address and port were reached but no process accepted the connection, or a network policy rejected it. Check service state, the configured port, bind_address, and whether networking is disabled. A successful socket test alongside a refused TCP test is consistent with a server configured for local socket use without an available TCP listener.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →“Permission denied” on an existing socket
The endpoint exists, but the calling process cannot traverse its directory or open the file. Inspect ownership, group membership, mode bits, and any operating-system security restrictions. Test again under the same account that runs the application, not only as an administrator.
The socket works immediately after a reboot, then disappears
Check whether the server later stops, whether a service restart creates the socket elsewhere, or whether a scheduled temporary-file cleanup removes it. Compare timestamps and service logs with the time of failure. A persistent mismatch between startup configuration and client defaults must be corrected in configuration rather than by repeatedly recreating a symlink or empty file.
A safe recovery checklist
- Save the exact error, including its socket path and numeric code.
- Determine whether the installed service is
mysql.serviceormysqld.service, then inspect its status. - Read the MySQL error log if startup failed or the process stopped.
- Identify the socket path configured for
mysqldand for the client. - Run
mysqladmin --protocol=SOCKET --socket=... versionwith the intended path. - Check the socket file, parent directories, ownership, and permissions.
- Test
mysql --protocol=TCP -h 127.0.0.1 -P ...using the actual configured port. - If TCP fails, inspect
skip_networking,bind_address, port settings, and firewall rules. - Restart only the service that exists, after correcting the identified cause, and repeat both protocol tests.
Preventing the same socket error later
Document the socket path and TCP port used by your installation instead of relying on package defaults. Keep server and client configuration under the same change-management process so an upgrade or manual edit cannot silently split their values. If the socket lives in a temporary directory, verify that cleanup policies preserve it or move it to a directory intended for runtime database sockets. Ensure the daemon can create the directory and the application users can traverse it without granting unnecessary access.
For operational checks, monitor both the service state and the endpoint your applications actually use. A green service status does not guarantee that every client is using the correct socket, and a successful TCP probe does not prove socket-based applications are configured correctly. Testing both paths after a package upgrade, configuration change, or service migration catches those mismatches before an application reports error 2002.
Bottom line
Fix MySQL socket errors by tracing the connection path rather than reinstalling the server: verify the correct service is running, use the exact socket path from the error, align client and server settings, check file and directory permissions, and investigate temporary-file cleanup. Then compare with an explicit 127.0.0.1 TCP test on the configured port. That comparison tells you whether the remaining fault is local socket handling or a broader server, networking, or firewall problem.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




