Error 2002 means the MySQL client cannot reach a local server through the socket path it tried. The usual fixes are to start the MySQL daemon, use the daemon’s actual socket path, repair access to the socket directory, or test the server over TCP/IP. A missing mysqld.sock is a connection-path symptom, not a password error by 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 Error 2002 means
The full message commonly looks like ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'. MySQL documents two normal explanations: no local server is running, or the client is using the wrong Unix-socket filename or TCP/IP port. Other documented causes include a socket removed from its directory, a server started with a different --socket value, crashed server threads, and insufficient permissions on the socket directory or file. See the MySQL 8.0 connection-error reference.
As an Amazon Associate I earn from qualifying purchases.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
On Unix systems, omitting a host or specifying localhost normally selects a Unix socket. An explicit TCP host and port use a different transport. The socket named in the error is therefore a clue about what the client attempted; it does not prove that the server is configured to create its socket there.
Error 2002 occurs before MySQL can authenticate your account. Changing a password, recreating a user, or granting privileges will not repair a stopped daemon, a wrong socket filename, or a directory that the client cannot enter. First establish that the server is alive and determine which endpoint it is actually listening on.
#1 Best Overall
| Message clue | What it commonly indicates | Best next check |
|---|---|---|
Path followed by error number (2) |
No socket exists at the path the client searched. | Check whether the daemon is running and find its configured socket path. |
Connection refusal such as (111) |
A server is not accepting connections through that endpoint. | Verify daemon state, socket selection, and then test TCP/IP. |
| Socket exists but access fails | The client account cannot traverse the directory or open the socket. | Inspect ownership, group membership, and mode on the directory and socket. |
| Failure returns after the socket is recreated | A crash, startup failure, or cleanup process may be removing or invalidating it. | Read the MySQL error log and check scheduled cleanup jobs. |
Fix Error 2002 in the right order
Work from the server outward. This avoids changing client settings when the daemon is simply stopped, and it gives every later test a known endpoint. Record the complete error, including the quoted path and any operating-system error number, before making changes.
1. Check whether mysqld is running
Use your operating system’s service manager to inspect the MySQL unit. Distribution and package names differ, so try the unit that exists on your machine rather than assuming one universal name:
sudo systemctl status mysql
sudo systemctl status mysqld
If the status output shows the correct unit is inactive, start that unit:
sudo systemctl start mysql
Substitute mysqld when that is the installed unit name. A unit can be present but repeatedly exit, so check that it remains active after the start command rather than assuming the command succeeded permanently. If startup fails, do not keep retrying the client. Read the MySQL error log and the service manager’s failure details first; a syntax error in an option file, an unavailable data directory, an in-use port, or a crash can prevent the socket from ever being created. MySQL’s documented troubleshooting sequence begins with server availability in Problems Connecting to MySQL.
Service status answers only whether the package believes the unit is active. It does not, by itself, prove that the server created the socket path shown in your error or that your application account can use it. Continue with the path and permission checks even after a successful start.
2. Read the exact socket path in the error
Copy the complete path between the quotes in the error. Check common candidates without creating anything:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsls -l /tmp/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null
A packaged Linux installation may use /var/run/mysqld/mysqld.sock, while the MySQL reference documents /tmp/mysql.sock as a default Unix-socket location. The package, operating system, and option files determine the actual value. If neither candidate exists, the daemon may be stopped, may be configured elsewhere, or may be unable to create the file. If a socket exists at a different location, use that location for the client test instead of editing files blindly.
Do not mistake a directory listing for proof that the endpoint is usable. A pathname can be left behind after a crash, and a file with the expected name is not necessarily a live Unix socket. The server process that owns the endpoint and the permissions on its parent directories matter.
3. Make the server and client socket settings identical
The server can receive a --socket setting on its command line or in an option file. The client can receive its value from its own --socket option or from the [client] section of an option file. Both values must name the same socket file. MySQL explains this relationship in Problems with the MySQL socket file.
For a one-time test, explicitly select the path you found:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock version
Replace /path/to/mysql.sock with the real path and use an account permitted to connect. A successful version response proves that the daemon and that socket agree; it does not change the persistent configuration.
For a lasting fix, inspect the option files used by the server and clients and remove conflicting socket declarations. A typical aligned arrangement has the same value under the server’s [mysqld] options and the clients’ [client] options. Keep the socket directory stable and ensure it is created during boot before mysqld starts. After changing the server option, restart the correct service and repeat the explicit mysqladmin test.
Multiple MySQL installations or manually started instances can make this comparison confusing. The client may read a configuration intended for one instance while the service starts another with a different socket. Identify the service definition and the option files it actually invokes, then compare those values directly. Do not change several files at once, because you will lose track of which setting resolved or introduced the mismatch.
4. Check directory and socket permissions
The account running mysqld must be able to create the socket in its directory. The account running the client must be able to traverse every parent directory and open the socket. Inspect both the directory and file, not just the filename:
Free tools Windows power users keep installed
One-click scans. No signup required.
ls -ld /path/to
ls -l /path/to/mysql.sock
Use the ownership, group, and mode expected by your package. Add the client account to the appropriate group or adjust the configured socket directory when that is the intended access model. Do not create an empty file with the socket’s name: a Unix socket is a live endpoint created by the server, and an empty placeholder cannot accept MySQL connections. Avoid broad permission changes such as making the entire directory world-writable; correct the service account, group, or directory policy instead.
If the directory does not exist after a reboot, find out whether the service package is supposed to create it. Creating a persistent, protected directory and configuring both sides to use it is safer than relying on a temporary location that another process can clean. Verify parent-directory traversal as the actual application user; a test run as root can hide a permissions defect affecting an unprivileged service.
5. Test TCP/IP to separate a socket problem from a server problem
Use an explicit TCP protocol, host, and port on the same machine:
mysql --protocol=TCP --host=127.0.0.1 --port=3306 -u USER -p
3306 is MySQL’s default port, but verify the configured port before treating it as correct. Replace USER and the port as necessary. If TCP succeeds while the socket test fails, the server is reachable and the defect is confined to the socket path, permissions, or client selection. If both fail, return to daemon status, the error log, and the server’s configured bind address and port.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchThis comparison also prevents a common mistake: changing localhost to an IP address changes the transport on Unix. localhost or an omitted host normally selects a local socket; 127.0.0.1 with --protocol=TCP selects a TCP connection. The two tests can therefore produce different results even with the same credentials. A successful TCP test does not repair the socket; it only demonstrates that another endpoint works.
6. Investigate a socket that keeps disappearing
MySQL notes that a cron job or other cleanup process can remove a socket stored in /tmp. If the daemon remains healthy but the file vanishes, inspect scheduled cleanup rules, temporary-file policies, and security software. Move the socket to a protected directory and configure both server and clients to use it, or change the cleanup policy so it does not delete a live MySQL socket.
If the socket disappears because mysqld crashes or exits, the path is only a symptom. Read the MySQL error log for the crash or startup cause, fix that cause, and then verify that the service stays active across a new connection attempt and a reboot. Recreating the pathname manually is not a durable repair and can obscure the event that removed it.
Unix socket versus TCP/IP: choose the diagnostic that fits
MySQL clients on Unix can connect in two ways: through a Unix socket file in the local filesystem (documented default /tmp/mysql.sock) or through TCP/IP and a port. The distinction matters because a socket is local-only and has no meaning on a remote host.
Recommended Free Tools
| Connection method | How the client selects it | What it can diagnose |
|---|---|---|
| Unix socket | Omitted host or localhost; optionally --protocol=SOCKET --socket=... |
Local daemon availability, socket path, directory access, and socket-file permissions. |
| TCP/IP | --protocol=TCP --host=... with a configured port |
Server listener, port selection, local firewall rules, and network reachability. |
For a remote MySQL server, do not copy a local mysqld.sock path into the command. Sockets are local to the machine where mysqld runs. Use the remote host’s TCP address and configured port, and investigate firewall or port blocking if the connection is refused or times out. A remote client that reports a socket error is usually being directed to a local socket because its host or protocol settings were omitted or overridden.
Reading configuration without guessing
Find the server’s authoritative value
Start with the service definition and the MySQL option files used by that installation. Look for a server-side socket setting or a command-line --socket argument. The value may differ between distributions, multiple MySQL instances, or an installation that was started manually rather than by the package service.
Do not infer the server’s value from a stale client configuration. A client can continue searching /var/run/mysqld/mysqld.sock even when the active daemon creates /tmp/mysql.sock, or the reverse. The explicit mysqladmin test against each plausible configured path is safer than changing several files at once.
When startup fails after an option-file edit, treat that failure as evidence that the daemon did not accept the new configuration. Return to the last known-good value, read the error log, and make one controlled change. A client-side change cannot make a daemon start when the server-side option is invalid.
Align every client that needs local access
Command-line tools, application connectors, and administrative scripts may each read different option files. Set the intended path in the client configuration they actually use, or pass --socket explicitly while diagnosing. After alignment, test both an administrative utility and the application account so a fix for one client does not hide a second stale setting.
Document which applications intentionally use TCP and which use the local socket. That prevents a later change from replacing a working TCP endpoint with an obsolete pathname, or from assuming that a socket fix automatically updates a connector with its own configuration.
Common scenarios and the appropriate remedy
The service is stopped
Start the existing MySQL service unit, then verify that its status remains active. If it exits immediately, use the error log to correct the startup failure before testing the socket again.
The service is running, but the named socket is absent
Compare the server’s configured socket value with the path in the error. Test the actual path explicitly, then align the client option. Check that the socket directory exists and is writable by mysqld.
The socket exists, but only one account receives Error 2002
Inspect directory traversal permissions, socket ownership, and group membership for that account. A root test can succeed while an unprivileged application still lacks access, so test as the account that normally runs the client.
TCP works but local applications fail
Keep the TCP result as proof that mysqld is listening, then correct the applications’ socket setting or switch them deliberately to TCP with the configured host and port. Do not assume that changing the server port will repair a socket mismatch.
The daemon repeatedly crashes
Treat every new missing socket as an effect of the crash. Follow the error log to the underlying startup, data-directory, resource, or configuration problem, and confirm stable service status before changing client paths.
A cleanup job removes the socket
Confirm that the daemon is still running when the file disappears, then inspect cron and temporary-file cleanup rules. Relocate the socket to a protected directory or adjust the cleanup policy. Do not repeatedly touch or recreate the pathname while the cleanup rule remains active.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Pitfalls that make troubleshooting slower
- Assuming the error path is authoritative: it is the path the client tried, not necessarily the path mysqld uses.
- Using one service name everywhere: package and distribution conventions differ; inspect the unit that actually exists.
- Testing only as root: root may bypass the directory permissions that block the application account.
- Creating a placeholder socket: only mysqld can create a functioning Unix socket endpoint.
- Changing the port to fix a socket mismatch: a Unix socket and a TCP listener are separate transports.
- Ignoring repeated crashes: each missing socket may be a consequence of an unstable daemon, not a new client misconfiguration.
- Editing every option file at once: make one documented change and retest so the active setting remains clear.
A compact recovery checklist
- Record the exact socket path and any errno shown in the Error 2002 message.
- Check the installed service unit with the system service manager.
- Start the correct unit if it is inactive; if startup fails, read the MySQL error log.
- List the reported path and other paths configured by the installation.
- Find the server’s active
--socketvalue and make the client use that same value. - Verify directory traversal, socket ownership, group, and mode for the real client account.
- Run
mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock versionagainst the selected path. - Run an explicit TCP test with
--protocol=TCP, the verified host, and the verified port. - If the socket vanishes again, inspect cleanup jobs and the daemon’s error log rather than recreating the file manually.
- Repeat the successful test after a service restart or reboot to confirm that the directory and configuration persist.
These steps distinguish a stopped server, a path mismatch, a permissions defect, a transport choice, and a recurring crash without making unrelated changes to authentication or application data.
Frequently Asked Questions
How do I fix MySQL Error 2002?
Check that the MySQL service is running, read the exact socket path in the error, compare it with the server’s configured --socket value, and make the client use that same path. Then verify directory and socket permissions. An explicit TCP test can show whether the server itself is healthy.
Best Value
Why is mysqld.sock missing?
The daemon may be stopped or crashing, the client may be looking in the wrong directory, the server may use a different socket setting, or a cleanup process may have removed the file. Check service status, the error log, configured paths, and cleanup jobs.
What does “Can’t connect through /var/run/mysqld/mysqld.sock” mean?
It means the client attempted a Unix-socket connection at that pathname and could not reach a usable endpoint there. The active server might use another path, might not be running, or might be unable to create or access the directory.
Should I use localhost or 127.0.0.1?
On Unix, localhost or an omitted host normally selects a Unix socket. 127.0.0.1 with --protocol=TCP selects TCP/IP. Comparing both is useful because one can work while the other has a path, permission, port, or listener problem.
Can I create an empty mysqld.sock file myself?
No. A working socket is a live endpoint created by mysqld. Creating an empty file will not accept connections and can make diagnosis harder. Correct the daemon, path, directory, or permissions instead.
Why does TCP work while the socket connection fails?
The server is reachable through its TCP listener, while the local socket path, socket permissions, or client configuration is wrong. Keep the TCP result as a diagnostic and align the local clients with the server’s actual socket or deliberately configure them to use TCP.
How do I fix a socket that disappears after reboot?
Check whether the socket directory is created before mysqld starts and whether the service recreates it with the expected ownership and mode. A protected, persistent directory is safer than a temporary location that cleanup processes can remove.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Can a remote MySQL server use my local mysqld.sock?
No. Unix sockets are local to the machine running mysqld. For a remote server, use its TCP host and configured port and check firewall or port blocking if the connection is refused or times out.
The Bottom Line
Fix Error 2002 by proving the daemon is running, identifying its real socket, aligning server and client settings, checking permissions, and comparing with an explicit TCP connection. Never create a fake socket file; investigate startup failures, crashes, and cleanup jobs that remove the real endpoint.
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.




