Decoding the mysqli::real_connect(): (HY000/2002) Error on Production
When you move a CodeIgniter application from a local environment to a live server, the most common stumbling block isn't usually your logic or routing; it's often how the database connection string is interpreted. The specific error mysqli::real_connect(): (HY000/2002): No such file or directory is a classic indicator of a protocol mismatch between what your application expects and what the server provides.
This error occurs when the MySQL client attempts to connect via the Unix socket interface, but the socket file either does not exist, has incorrect permissions, or the hostname configuration forces the client to look for it in a location where it isn't present. In your provided configuration, you are setting 'hostname' => 'localhost'. While this works perfectly fine on a standard development machine running XAMPP, MAMP, or WampServer, production Linux servers operate differently.
On a typical Linux server, the mysqld service usually listens on TCP/IP (port 3306) rather than a local Unix socket. When you pass 'localhost' to the mysqli extension in this environment, the PHP driver attempts to resolve it as the loopback interface (127.0.0.1) or tries to find the socket file at /var/run/mysqld/mysqld.sock. If your server is configured strictly for TCP connections and no socket exists, or if the socket path isn't standard, you get the "No such file or directory" response because the driver is looking for a file-based connection that isn't there.
Switching to TCP/IP Socket
The most reliable fix for production environments is to explicitly disable the Unix socket lookup and force a TCP connection. You can do this by changing your database configuration from 'hostname' => 'localhost' to 'hostname' => '127.0.0.1'.
Although both strings often resolve to the same network interface, PHP's mysqli driver treats them distinctly:
localhost: Resolved via DNS first, then falls back to looking for a Unix socket file if the hostname resolves to127.0.0.1.127.0.0.1: Forces an immediate connection attempt over the TCP/IP loopback interface, bypassing any search for a socket file entirely.
Update your .env or config file accordingly:
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1', // Changed from 'localhost'
'username' => 'netservice',
'password' => '*********',
'database' => 'valedastrutas',
'dbdriver' => 'mysqli',
'pconnect' => FALSE,
'db_debug' => FALSE,
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
// ... other settings remain the same
);
If your application is hosted on a cloud provider like AWS, DigitalOcean, or Azure, they often use security groups or virtual firewalls. In these cases, simply connecting to 127.0.0.1 might work if you are running inside the VM, but if your application code lives in a container (Docker) while the database runs outside of it, 127.0.0.1 will point to nothing. In that scenario, you must use the actual host IP address assigned to your server or container network bridge.
Verifying Socket Paths and Permissions
If switching to the IP address does not resolve the issue, you need to investigate whether a socket file exists but is inaccessible. You can check if the socket file exists on your server by running:
ls -l /var/run/mysqld/mysqld.sock
# Or sometimes located at:
ls -l /tmp/mysql.sock
If the file exists, the error might be due to permission issues. Ensure that the user running your PHP-FPM process has read and execute permissions on the socket file. If you are unsure whether a socket is active at all, check the MySQL configuration file (my.cnf or mysql.ini) under the [mysqld] section. Look for a line resembling:
socket = /var/run/mysqld/mysqld.sock
If this line is commented out or missing, MySQL is likely listening only on TCP port 3306. In that case, ensuring your PHP config uses 127.0.0.1 is the correct approach.
Why This Matters in Production
The distinction between local development and production environments is a frequent source of headaches. Local stacks like XAMPP rely heavily on Unix sockets for inter-process communication because it's faster than TCP. However, once you deploy to Linux servers, especially those managed via Docker or cloud instances, the networking model shifts.
By defaulting to 127.0.0.1 in your production configuration ensures that your application explicitly requests a network connection rather than guessing based on file system paths. This practice makes your database connections more predictable and prevents intermittent failures where the socket file might be temporarily missing or moved by a package update.
For further guidance on securing database connections and managing environment-specific configurations, resources like https://laravelcompany.com offer excellent insights into production best practices, though the specific fix for this error lies in understanding how the PHP MySQLi driver resolves hostnames under the hood.
