The MySQL error “Can’t connect to local MySQL server through socket” (commonly error 2002) means the client could not open the Unix-socket connection it selected. It does not, by itself, indicate a bad password. Work through these checks in order: make sure mysqld is running, use the socket pathname shown in the error, make the client and server use the same pathname, correct filesystem access, and then test TCP with 127.0.0.1 if the server is configured to accept it.
Antihost offers Linux VPS plans with full root access, unlimited traffic on a 1 Gbps port and a 14-day free trial.
What this MySQL error actually means
On Unix-like systems, a MySQL client normally has two local connection methods:
As an Amazon Associate I earn from qualifying purchases.
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 →- Unix socket: a special file in the filesystem, selected when no host is specified or when the host is
localhost. - TCP/IP: a network connection to an address and port, selected by an address such as
127.0.0.1.
Oracle’s MySQL 8.4 Reference Manual describes the distinction this way: “A MySQL client on Unix can connect to the mysqld server in two different ways: By using a Unix socket file to connect through a file in the file system (default /tmp/mysql.sock), or by using TCP/IP, which connects through a port number.” That distinction remains the key to diagnosing the message.
The socket message is raised before MySQL can authenticate your account. If the server is stopped, the pathname is wrong, the socket has been removed, or a directory is inaccessible, changing the password will not solve the problem. An Access denied response is different: it proves that a connection reached MySQL and that account privileges now need attention.
#1 Best Overall
Use this diagnostic sequence first
- Confirm that the server is running. Use the service manager supplied by your operating system or package, or inspect the process list for
mysqld. If startup failed, read the MySQL error log before trying more client options. No socket or TCP connection can succeed while the server is stopped. - Copy the socket pathname from the error literally. The text after
socketis the path the client attempted. Do not substitute a familiar path without checking it. For example, one installation may use/tmp/mysql.sock, while a distribution package may place the socket elsewhere, including under a MySQL data or runtime directory. - Check whether that file exists and is accessible. Inspect the socket and every parent directory with the normal filesystem tools for your operating system. A missing file usually means the server did not create it, created it at another path, or a cleanup process removed it.
- Identify the server’s configured socket. Check the active MySQL configuration and the startup options used for this instance. Package defaults are not universal; the client and server must agree on one exact pathname.
- Pass the verified pathname explicitly. This removes ambiguity while you repair the permanent configuration:
mysql --socket=/path/to/mysql.sock -u USER -p
mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version
Replace both the example path and USER with values from your installation. The second command is a useful low-impact check: a successful version response confirms that the socket transport works, while an error still points to server state, pathname, or filesystem access.
- Check access permissions. The connecting client must be able to traverse every directory in the socket path and open the socket. The server account must also be able to create and use it. Correct ownership, group membership, directory mode, or the configured socket directory as appropriate for your installation. Prefer a targeted fix over making a whole temporary directory broadly writable.
- Try TCP deliberately. If a socket is unavailable, test the loopback address and the instance’s configured port:
mysql -h 127.0.0.1 -P 3306 -u USER -p
Port 3306 is MySQL’s documented default, not a guarantee that your instance uses it. Substitute the configured port. This test bypasses Unix-socket selection only when the server is listening for TCP, the bind address includes the loopback address, networking has not been disabled, and a firewall permits the connection.
Recommended Free Tools
Why localhost and 127.0.0.1 behave differently
| Client host value | Typical transport on Unix-like systems | What must be true |
|---|---|---|
No host, or localhost |
Unix socket | The server is running, the client knows the same socket pathname, and the client can access its directories and file. |
127.0.0.1 |
TCP/IP loopback | MySQL is listening on the selected address and port; skip_networking, binding restrictions, and firewalls do not block it. |
Therefore, changing localhost to 127.0.0.1 is a valid diagnostic and sometimes a practical choice, but it is not a universal fix. If the instance was started with skip_networking, TCP is intentionally disabled. If bind_address excludes loopback, or the port is filtered, the TCP attempt will fail even though the server process is healthy.
These two values can also reach different MySQL account-matching rules because one attempt is a socket connection and the other is a TCP connection. Do not use a successful TCP test as proof that every socket-based application is configured correctly, or vice versa. Test with the same host value, socket option, port, and runtime identity that the failing program uses.
Fix a wrong or missing socket path
Read the error as a pathname clue
A message naming /tmp/mysql.sock tells you exactly where that client looked. If the server is configured for another location, point the client to the server’s location rather than creating an arbitrary empty file at the path in the error. A socket is created by the running server; an ordinary file with the same name cannot substitute for it.
Make temporary testing explicit
Use the --socket option on each client while diagnosing:
mysql --socket=/path/to/mysql.sock -u USER -p
mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock version
These options affect only the command being run. Other programs, scripts, and administration tools may still use their own defaults, so a successful manual login does not automatically repair application configuration. If a script invokes a different client binary or runs under a service account, repeat the test in that same context.
Make the setting persistent
Set the same socket value in the active server configuration and in the client configuration used by your applications, or ensure every application passes the explicit socket option. Restart the server after changing its startup configuration, then verify the resulting socket path and permissions. If more than one MySQL instance runs on the host, confirm that you are editing the configuration for the intended instance; each instance may have a different socket.
Keep a single authoritative record of the socket pathname for each instance. A package upgrade, a manually started server, and a service-managed server can load different configuration files. When the error suddenly returns after maintenance, compare the startup configuration actually used by the running process with the client defaults rather than assuming the old path is still active.
When the socket file disappears
Socket files placed in temporary directories can be removed by automatic cleanup jobs even while an administrator expects them to remain available. MySQL’s socket-protection guidance recommends preventing cleanup from deleting the live socket or moving the socket to a safer, dedicated location. It also discusses protecting temporary storage with the sticky bit.
- First establish whether
mysqldis still running. If it is not, a missing socket is a symptom of the stopped service, not a standalone file problem. - If the server is running but the socket vanished, inspect temporary-directory cleanup policy and the server log.
- Choose a directory whose lifecycle is appropriate for a service, make sure both the server and clients can traverse it, and then configure all participants to use the new pathname.
Do not blindly delete a socket that appears stale. Verify the owning server process and instance first; removing a live socket can interrupt clients while the server continues running. If the server recreates a socket after a restart, verify that the recreated pathname, owner, group, and mode are the values your applications expect.
Correct filesystem permissions without weakening the host
Socket access is a two-sided filesystem requirement. The MySQL service account needs permission to create the socket and traverse its parent directories. The user or service connecting to MySQL needs permission to traverse those directories and open the socket. A directory can block access even when the socket’s own mode looks permissive.
- Check the owner and group of the socket and each directory in its path.
- Check whether the client runs as your login account, a web-service account, a container user, or another restricted identity.
- Grant the narrow directory or group access required by that identity instead of applying broad world-write permissions.
- If a security policy or service sandbox restricts filesystem access, adjust the policy or move the socket to an approved directory rather than bypassing the protection.
After changing access, rerun the explicit mysql --socket=... test as the same identity that the failing application uses. A test as an administrator can succeed while the application remains unable to traverse the path. Remember that access to the socket is not enough if the client cannot traverse one parent directory; inspect the complete path from the filesystem root.
Use the TCP fallback correctly
Run the loopback test with the port configured for your instance:
mysql -h 127.0.0.1 -P 3306 -u USER -p
If this succeeds while the socket command fails, the MySQL server and credentials are probably usable and the remaining problem is the Unix-socket path or its permissions. Configure the application to use the same host and port only if TCP is acceptable for your deployment.
If TCP fails too, check the three server-side controls that commonly explain the result:
skip_networking: disables TCP/IP entirely. In that configuration, use the Unix socket or remove the setting only if network access is intended.bind_address: limits which local or remote addresses accept connections. A binding that does not include loopback will reject a127.0.0.1test.- Firewall rules: can block the configured port even when MySQL is listening. Check host and network firewalls using your organization’s normal procedures.
Do not assume that port 3306 is correct merely because it is the default. Use the port configured for this server instance, and keep the port qualification with any connection string or deployment documentation you create. A TCP success proves only that the selected address and port accepted that attempt; it does not prove that an application using localhost is using TCP.
Interpret the next error instead of repeating the wrong fix
| Observed result | What it establishes | Next action |
|---|---|---|
| “Can’t connect … through socket” and the named file is absent | The attempted Unix-socket endpoint is not available at that path. | Check server startup, the configured pathname, and temporary-directory cleanup. |
| The socket exists but the client reports permission or access failure | The endpoint may be present, but the client or a parent directory is inaccessible. | Correct ownership, group, mode, or service-policy access for the actual client identity. |
| Explicit socket command succeeds; an application still fails | MySQL and that socket work for the test identity. | Update the application’s host/socket settings and its runtime user. |
127.0.0.1 returns Access denied |
TCP reached MySQL; transport troubleshooting is complete for that attempt. | Investigate account name, authentication method, host grants, or password. |
127.0.0.1 cannot connect |
TCP is not reaching a listening MySQL endpoint. | Check the configured port, skip_networking, bind_address, firewall, and server logs. |
Use the wording of the new error to decide whether to continue transport troubleshooting. “Can’t connect” still indicates that the endpoint was not reached. “Access denied” means the endpoint answered and the next work belongs to account authentication or authorization. Separating those stages prevents repeated password resets when the real issue is a path or listener.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallPackage defaults and path differences
/tmp/mysql.sock is a commonly documented default, but it is not a universal location. Distribution packages, vendor installers, custom builds, containers, and multiple-instance setups can choose different directories. A path under /var/lib/mysql or another runtime directory may be correct for a particular package. Treat the path printed by your error and the active instance configuration as authoritative for that host.
When documenting a deployment, record the socket pathname, TCP port, bind address, service account, and the client configuration that uses them. This prevents a later package upgrade or second instance from silently reintroducing the mismatch. In containerized or restricted environments, also record which filesystem namespace contains the socket and which identity the client uses; a path visible to one environment may not be visible to another.
A compact decision checklist
- Is the intended
mysqldprocess running? - What exact socket path does the error name?
- What exact socket path does the server configuration specify?
- Does the socket exist at that path?
- Can the server account create it, and can the client identity traverse every parent directory and open it?
- Could temporary-directory cleanup have removed it?
- Does an explicit
--sockettest succeed? - If using TCP, are the configured port,
bind_address,skip_networking, and firewall consistent with that choice? - Has the application been configured separately from the command-line test?
Following those questions in order separates a stopped service, a pathname mismatch, a filesystem problem, and a deliberately disabled TCP listener—the four situations most often hidden behind the same socket wording.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Frequently asked questions
Is this always MySQL error 2002?
The wording is commonly associated with error 2002, but the exact numeric code and surrounding text can vary by client version and package. Diagnose the transport named in the message: a socket path means the client attempted a Unix socket, while a host-and-port failure indicates a TCP attempt.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #4
Where is mysql.sock?
There is no single universal location. /tmp/mysql.sock is a documented common default, but packages and custom installations may use a path under /var/lib/mysql or another runtime directory. The path printed in the error tells you where that client looked; compare it with the active server configuration and the file present on disk.
Can I fix the problem just by changing localhost to 127.0.0.1?
Sometimes. That change forces a TCP/IP attempt instead of the usual Unix-socket choice on Unix-like systems. It works only if MySQL is listening on the selected loopback address and port, TCP has not been disabled with skip_networking, and a firewall is not blocking the port. Use the instance’s configured port rather than assuming 3306.
Why does a password reset not help?
The socket error occurs before authentication. Until the client reaches a running server through a valid socket or TCP listener, MySQL cannot evaluate the password. Reset credentials only after a connection reaches MySQL and returns an authentication or authorization error such as Access denied.
Should I create an empty file named mysql.sock?
No. The server creates and manages a Unix socket; an ordinary file with that name cannot accept MySQL connections. Find the server’s real socket, start or repair the service, or explicitly point the client at the correct pathname.
What if the socket exists but the client still cannot connect?
Check every parent directory as well as the socket’s owner, group, and mode. The client identity must be able to traverse the complete path and open the socket, and the server account must be able to create and use it. Repeat the test as the same user that runs the failing application.
Can cleanup jobs really remove a live socket?
Yes. Sockets in temporary directories can be deleted by automated cleanup. Confirm that mysqld is still running, inspect the cleanup policy and server log, then protect the socket from removal or move it to a dedicated, persistent directory accessible to both sides.
What does a successful mysqladmin test prove?
A command such as mysqladmin --protocol=SOCKET --socket=/path/to/mysql.sock version confirms that the specified socket transport works for that command’s user and configuration. It does not automatically fix an application using another path, another client default, or another runtime identity.
Best Value
Is TCP safer or better than a Unix socket?
Neither is a universal answer. A Unix socket avoids network listener and firewall settings but requires matching filesystem paths and permissions. TCP can simplify applications that expect a host and port, but it depends on a listening address, an enabled network protocol, the correct port, and firewall policy. Choose the transport that fits the deployment and configure it consistently.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsFrequently Asked Questions
What is the fastest first check for this error?
Confirm that the intended mysqld service is running, then compare the socket path in the error with the server’s configured path. A stopped service or path mismatch accounts for many failures.
Does localhost use TCP?
On Unix-like systems, MySQL normally treats localhost (or an omitted host) as a Unix-socket connection. Use 127.0.0.1 and the configured -P value to request TCP/IP.
Why can an administrator connect while my application cannot?
The administrator may use a different socket path or have permissions that the application’s runtime identity lacks. Test with the same socket option and user account as the application.
What should I do if both socket and TCP tests fail?
Read the MySQL error log and verify that the intended server instance started. Then check the socket path, filesystem access, configured TCP port, skip_networking, bind_address, and firewall rules.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
The Bottom Line
Fix this error by treating it as a transport and endpoint problem first: start the correct mysqld instance, match the client and server socket pathname, verify access to every directory, protect the socket from temporary cleanup, and test 127.0.0.1 only with the configured port and listener settings. Once a connection reaches MySQL and returns Access denied, switch from socket troubleshooting to account and privilege troubleshooting.
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.




