Troubleshooting mysqli_real_connect(): (HY000/2002) Error: Fixing 'No Such File or Directory' Issue

If you've encountered the mysqli_real_connect() error message that reads "No such file or directory," you're not alone. This error occurs when the MySQL server is not running, or the mysqli extension is not enabled in your PHP configuration.

In this guide, we'll walk you through the steps to troubleshoot the mysqli_real_connect() error and fix the "No such file or directory" issue.

Prerequisites

Before we begin, you should have the following:

  • A server running MySQL
  • PHP 5.3+ with mysqli extension enabled
  • SSH access to your server

Step 1: Check MySQL Service Status

The first step is to check the status of the MySQL service. You can do this by running the following command:

sudo service mysql status

If the MySQL service is not running, you can start it by running the following command:

sudo service mysql start

Step 2: Check MySQL Socket File

If the MySQL service is running, the next step is to check if the socket file exists. You can do this by running the following command:

ls /var/run/mysqld/mysqld.sock

If the socket file does not exist, you can create it by running the following command:

sudo touch /var/run/mysqld/mysqld.sock
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock

Step 3: Update PHP Configuration

If the MySQL service is running, and the socket file exists, the next step is to check if the mysqli extension is enabled in your PHP configuration. You can do this by running the following command:

php -m | grep mysqli

If the mysqli extension is not enabled, you can enable it by editing your PHP configuration file. The location of this file varies depending on your server setup. Here are a few common locations:

  • /etc/php.ini
  • /etc/php/7.0/apache2/php.ini
  • /etc/php/7.0/cli/php.ini

Once you locate the file, open it with a text editor and find the following line:

;extension=mysqli.so

Remove the semicolon (;) at the beginning of the line to uncomment it, and save the file.

FAQ

Q1. What causes the "No such file or directory" error in mysqli_real_connect()?

This error occurs when the MySQL server is not running, or the mysqli extension is not enabled in your PHP configuration.

Q2. How do I check the status of the MySQL service?

You can check the status of the MySQL service by running the following command:

sudo service mysql status

Q3. How do I start the MySQL service?

You can start the MySQL service by running the following command:

sudo service mysql start

Q4. How do I check if the mysqli extension is enabled in my PHP configuration?

You can check if the mysqli extension is enabled by running the following command:

php -m | grep mysqli

Q5. How do I enable the mysqli extension in my PHP configuration?

To enable the mysqli extension, edit your PHP configuration file and find the following line:

;extension=mysqli.so

Remove the semicolon (;) at the beginning of the line to uncomment it, and save the file.

Conclusion

In this guide, we walked you through the steps to troubleshoot the mysqli_real_connect() error and fix the "No such file or directory" issue. By following these steps, you should be able to connect to your MySQL server using mysqli_real_connect(). If you have any further questions or issues, please refer to the official documentation or seek help from the community.

Sources:

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.