Introduction
The reason for this error is just because PHP and MySQL can’t get connected themselves. And for PHP to connect with the database it needs to locate the socket/TCP file, but it is encountering difficulties in doing so.
Causes for this error
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
- Incorrect Database Host: There’s a chance that the IP address or hostname of the database server that was entered in the .env is off.
- Incorrect Password or User Name: Verify that the password and user name you specified in the .env are correct and grant you the necessary access rights to your database.
- Database Server Not Running: It’s possible that the database server is not running. Verify that the database server is operational (e.g., MySQL or MariaDB).
- Incorrect Port Number: Verify that the port number you’re using in your connection settings is the right one if your database server isn’t using the default one, which is 3306 for MySQL.
- Socket Configuration: Localhost will cause the MySQL client to try a UNIX socket in a standard directory. If that doesn’t exist or is somewhere else, you won’t be able to connect. 127.0.0.1 always uses a TCP connection.
- Server setup Errors: Incorrect permissions for remote connections or exceeding the maximum connection limit are examples of setup errors in your database server settings that could be keeping you from connecting.
Solution for this error
- Verify the status of your MySQL or MariaDB server by checking its running or not. This can be accomplished by getting into the server that hosts MariaDB and MySQL and using commands such as systemctl status mysql or systemctl status mariadb to see how it is doing.
- Verify the hostname and port settings in the database connection configuration of your .env file. Make sure that the MySQL/MariaDB server is correctly pointing to them.
- Sometimes Mysql instances are running only via TCP and not socket and this can be solved by changing the “host” in the /app/config/database.php file from “localhost” to “127.0.0.1”.
- If your application is set up to connect over a Unix socket as opposed to TCP/IP, confirm that the socket file mentioned in the configuration file is both present and reachable. This can be achieved by following steps:
-> Open a terminal and connect to the mysql with:
mysql -u root -p
-> It will ask you for the related password. Then once you get the mysql promt type the next command:
mysql> show variables like '%sock%'
-> You will get something like this:
+-----------------------------------------+-----------------+
| Variable_name | Value | +-----------------------------------------+-----------------+ |performance_schema_max_socket_classes | 10 | |performance_schema_max_socket_instances | 322 |
| socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+
-> Keep the value of the last row:
/tmp/mysql.sock
-> In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end:
'unix_socket' => '/tmp/mysql.sock'
-> You must have something like this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'SchoolBoard',
'username' => 'root',
'password' => 'venturaa',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/tmp/mysql.sock',
),
-> Save these changes and reload the page.
- Occasionally, socket-related issues can be resolved by restarting the MariaDB or MySQL service. For this, you can use commands like “sudo systemctl restart mysqL” or “sudo systemctl restart mariadb”.
- Ensure that the user you designated in your application’s database settings has the necessary rights to access the databases and the MySQL / MariaDB server.
- Examine whether any firewall rules are obstructing the connection between your server and MySQL or MariaDB. If required, open the relevant ports (3306 for MariaDB and MySQL).