Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

This error message is a classic sign of environment mismatch between your web server and your command-line PHP execution. You are running two different instances of PHP that talk to the database using different connection parameters. When you invoke php scriptname.php directly from the Terminal, it does not behave exactly like the version embedded within your XAMPP installation.

The specific error [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) reveals that the PHP CLI process is attempting a Unix socket connection because localhost in the context of a local server usually resolves to the socket path /tmp/mysql.sock. However, your PHP script cannot find this specific socket file at that location.

The fundamental issue here is that the MySQL server running inside XAMPP is likely configured to use a different socket directory than what the CLI is looking for. By default, many modern distributions of MySQL (including those bundled with newer versions of XAMPP) often set the socket path in the my.ini configuration file to something like /tmp/mysql.sock. However, if you have manually moved the socket file or if your system permissions are strict, the file might exist physically but be inaccessible, or it might be located entirely elsewhere, such as /var/tmp/mysql.sock.

When you run the script via a browser (using XAMPP's Apache), the mysql_connect() function often defaults to looking for the socket defined in the server's configuration or falls back to TCP/IP if the socket is missing. The CLI environment, conversely, is more rigid and strictly adheres to the default socket path unless explicitly told otherwise via the --socket flag or a hardcoded path in your code.

To resolve this without relying on deprecated functions like mysql_connect(), you should inspect your MySQL configuration file. Open the my.ini file located within your XAMPP installation directory (typically xampp/mysql/my.ini). Look for the [mysqld] section and find the socket directive.

[mysqld]
socket = /tmp/mysql.sock

If this line is commented out or set to a different path, you have two primary options. First, you can update your PHP CLI environment to point to the correct socket. You can do this by adding the following line to your php.ini file (usually located in /xampp/php/ or found via the php -i | grep ini_file_name command):

default_socket_path = /tmp/mysql.sock

Alternatively, and often more robustly, you can use the --socket flag directly when invoking PHP from the terminal. This bypasses the need to modify global configuration files:

php --socket=/path/to/your/mysql.sock scriptname.php

However, the most reliable long-term solution is to stop relying on the deprecated mysql_* extension entirely. The mysql_connect() function and its family have been removed in PHP 7.0 and later versions due to security vulnerabilities and lack of maintenance. If your project still uses these functions, you are likely running an outdated version of PHP or code that hasn't been migrated to PDO or MySQLi.

Migrating to PDO is the industry standard for a reason. It allows you to define the connection host, port, username, and password in a single array, handling both Unix sockets and TCP/IP connections seamlessly depending on how your application logic requires it.

$pdo = new PDO(
    'mysql:host=localhost;dbname=FNB1C_data', 
    'root', 
    '', 
    [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]
);

When using PDO, the distinction between a socket connection and a TCP connection often becomes abstracted away by the driver. Furthermore, if you are transitioning to Laravel, this specific error pattern is frequently resolved by updating your configuration. You would simply update the database.php file within your application root to use the environment variables or hardcoded values for the database host, ensuring consistency between your CLI and web environments.

If you have recently updated your system or XAMPP, it is possible that the MySQL socket file was moved to a new location, such as /var/run/mysqld/mysql.sock or simply removed if switching to TCP-only communication. In such cases, explicitly defining 127.0.0.1 in your connection string forces a TCP/IP handshake rather than relying on the existence of a Unix socket file.

// Using MySQLi instead of deprecated mysql_connect()
$mysqli = new mysqli("127.0.0.1", "root", "", "FNB1C_data");

By switching to IP addresses or updating your socket paths, you ensure that your development environment remains stable regardless of where the server places its socket files. For production environments, relying on localhost sockets is often unnecessary; using TCP/IP connections over 127.0.0.1 provides a consistent interface for both CLI scripts and web applications, eliminating these specific "file not found" errors entirely.

Our network