Databases

Fix MySQL "Can't connect to local server through socket" (2002)

Fix ERROR 2002 and 2003 connection errors: MySQL not running, wrong socket path, localhost vs 127.0.0.1, bind-address, firewall and access denied errors explained.

IntermediateLinux VPS3 min read

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) means the client could not reach the server's Unix socket. Its cousin, ERROR 2003: Can't connect to MySQL server on '10.0.0.5' (111), is the TCP version. Both are about reaching the server at all, before any password check.

Before you start

  • Root access to the database server.
  • The exact error and the connection settings your app uses (host, port, socket).

Is MySQL running?

systemctl status mysql

The service may be called mysqld or mariadb. If it is stopped, start it and read why it stopped:

systemctl start mysql && journalctl -u mysql -n 50

Typical reasons: disk full, OOM killer, a bad config line after a change, or corrupted tables after a crash. The error log (/var/log/mysql/error.log or /var/log/mysqld.log) names the cause.

Socket path mismatch

The (2) at the end means "no such file". Find where the server creates its socket:

mysql -u root -p -h 127.0.0.1 -e "SHOW VARIABLES LIKE 'socket';"

or ss -xlp | grep mysql. Then make the client use the same path, in [client] of my.cnf or the app config (for PHP, pdo_mysql.default_socket / mysqli.default_socket).

localhost vs 127.0.0.1

For MySQL clients, localhost means "use the Unix socket"; 127.0.0.1 means TCP. If the socket is the problem, switching the app's DB host to 127.0.0.1 often works immediately. Note the database user must then be allowed from 127.0.0.1 (or localhost, which MySQL treats separately).

ERROR 2003: TCP connections

ERROR 2003 (HY000): Can't connect to MySQL server on '10.0.0.5:3306' (111)
  • (111) Connection refused: nothing listens on that IP/port. Check ss -ltnp | grep 3306 and the bind-address setting (default 127.0.0.1 only allows local connections).
  • (110) timed out: a firewall drops the traffic. Allow port 3306 from the app server's IP only.
  • To accept remote connections, set bind-address to the private IP, restart, and grant the user from the app server's address.

When it connects but says access denied

ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES)

The server is reachable; the credentials or host part do not match. Check grants:

SELECT user, host, plugin FROM mysql.user WHERE user='app';

Create the user for the right host, or reset its password with ALTER USER. Old client libraries may also fail with caching_sha2_password on MySQL 8.

Check the fix

mysql -u app -p -h 127.0.0.1 appdb -e 'SELECT 1;'

Then reload the application.

Troubleshooting

  • Socket directory missing after reboot: /var/run/mysqld was not recreated; reinstalling the package or a systemd tmpfiles rule restores it.
  • MySQL starts then stops: often InnoDB recovery failing or not enough memory; the error log explains which.
  • Works from SSH, not from the website: PHP uses a different socket path or the app config uses another host.

MySQL connection FAQ

Is socket or TCP faster?

The socket is slightly faster for local connections; the difference rarely matters.

Should I open MySQL to the internet?

No. Use private networking, an SSH tunnel or a VPN, and firewall rules limited to known IPs.

What host do I use on cPanel?

localhost. For remote access, add your IP under cPanel → Databases → Remote Database Access.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.