PDOException SQLSTATE The file or directory does not exist
P粉038161873
2023-08-10 17:44:11
<p> I believe I have successfully deployed my (very basic) website to fortrabbit, but once I connect to SSH run some commands like <code>php artisan migrate</code> or <code>php artisan db:seed</code>), I will receive an error message: </p>
<pre class="brush:php;toolbar:false;">[PDOException]
SQLSTATE[HY000] [2002] No such file or directory</pre>
<p>At some point the migration must have been successful because my tables exist - but that doesn't explain why it doesn't work for me now. </p>
One of the simplest causes of this error is that the MySQL server is not running. So the first step is to verify. If it's working, continue with other suggestions:
I encountered the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".
Not sure why "localhost" doesn't work by default, but I found this answer to a similar problem that was solved in a symfony2 post. https://stackoverflow.com/a/9251924
renew: Some people asked why this fix worked, so I did some research on the issue. As explained in this post, they use different connection types. https://stackoverflow.com/a/9715164
The problem here is that "localhost" uses a UNIX socket and cannot find the database in the standard directory. However, "127.0.0.1" uses TCP (Transmission Control Protocol), which means it runs over the "local network" on your computer and is more reliable than a UNIX socket.
The error message indicates an attempt to establish a MySQL connection over a socket (which is not supported).
In the context of Laravel (artisan) you may want to use a different/correct environment. For example:
php artisan migrate --env=production
(or any other environment). See here.