Skip to content
Databases3 min read

Fix MySQL error 2002: Can't connect to local MySQL server

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Short answer

Error 2002 means the application couldn't reach the MySQL server at all, so the problem happens before any username or password check. The usual causes are a wrong DB_HOST, a socket file that isn't where PHP expects it, or a stopped MySQL service.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. What each variant means
  2. Causes
  3. How to fix it
  4. Prevention
  5. On ArabyHost

Unlike error 1045, where the server answers and refuses the login, 2002 means your app never found the server. In PHP it appears in one of these forms, and each points somewhere different:

SQLSTATE[HY000] [2002] No such file or directory
SQLSTATE[HY000] [2002] Connection refused
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known
SQLSTATE[HY000] [2002] Connection timed out

What each variant means

  • No such file or directory: the host is localhost, so PHP tried the socket file and didn’t find it at the expected path.
  • Connection refused: a TCP connection reached the address but nothing is listening on the port; MySQL is down or the port is wrong.
  • getaddrinfo failed: DB_HOST is a name that doesn’t resolve, such as mysql or db from a Docker setup.
  • Connection timed out: the remote server doesn’t answer, usually because a firewall blocks port 3306.

Causes

  • A wrong DB_HOST copied from your development environment.
  • Uploading your local .env to the server unchanged.
  • A socket path in PHP’s settings that doesn’t match MySQL’s real one.
  • The MySQL service is stopped or restarting.
  • Connecting from your own computer to a hosting database that doesn’t allow your IP.

How to fix it

  1. Set the host. On cPanel shared hosting the database lives on the same server:
DB_HOST=localhost
DB_PORT=3306
  1. Switch from socket to TCP if you see No such file or directory:
DB_HOST=127.0.0.1

Or give the correct socket path. Find it in phpMyAdmin’s SQL tab:

SHOW VARIABLES LIKE 'socket';

Then set it as DB_SOCKET in Laravel, or in a PDO DSN:

$pdo = new PDO('mysql:unix_socket=/var/lib/mysql/mysql.sock;dbname=cpuser_shop;charset=utf8mb4', $user, $pass);
  1. Clear Laravel’s config cache after any change:
php artisan config:clear
  1. Check the service is up. If phpMyAdmin opens your database normally, MySQL is running and the problem is in your app’s settings. If phpMyAdmin fails too, the service itself is down: on shared hosting contact support; on your own server check:
systemctl status mariadb
journalctl -u mariadb --since "1 hour ago"
  1. Connecting from your computer: reaching a hosting database from outside requires the provider to allow your IP and port 3306 to be open, and many hosts restrict it. It’s usually simpler to work on a local copy of the database or manage data through phpMyAdmin.

Prevention

  • Keep a separate .env per environment and never upload your local one.
  • Add .env to .gitignore and keep defaults in .env.example.
  • If the error appears only now and then, note the times and send them to support; it may line up with service restarts.

On ArabyHost

Databases run on MariaDB/MySQL with phpMyAdmin on the same server as your site, so the host in your settings is localhost. If phpMyAdmin won’t open either, contact support. If it works and only your site fails, ask the Araby AI assistant in the panel; it reads your error log and points to the setting at fault. The .env steps for Laravel are in the Laravel deployment guide.

FAQ

What's the difference between localhost and 127.0.0.1 for MySQL?

In PHP and the mysql client, localhost means connecting through a local socket file, while 127.0.0.1 means a TCP connection on port 3306. If one fails with 2002, try the other.

Why does it appear after moving a Laravel project out of Docker?

Your .env probably says DB_HOST=mysql, which is a Docker service name that doesn't exist on the server. Change it to localhost and clear the config cache.

How is error 2002 different from 2003?

In the mysql client, 2002 is a failed local socket connection and 2003 a failed TCP connection to a server. PHP usually reports 2002 for both. Either way the connection never got as far as the password.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.