It sounds like you're encountering an issue with connecting to the MySQL server when using Laravel Sail on a Linux environment. The error message indicates that the MySQL client is trying to connect via a Unix socket, which is not available in the Docker container setup used by Sail.
The solution you found—changing localhost to 127.0.0.1 or 0.0.0.0—is indeed correct. This is because localhost in many Linux distributions resolves to a Unix socket, whereas 127.0.0.1 and 0.0.0.0 resolve to TCP connections, which is what Docker containers use.
Here is a more detailed solution:
-
Update your
.envfile: Change theDB_HOSTvalue fromlocalhostto127.0.0.1or0.0.0.0.DB_HOST=127.0.0.1 -
Restart Laravel Sail: After making changes to the
.envfile, you should restart the Sail environment to ensure the changes take effect../vendor/bin/sail down ./vendor/bin/sail up -d -
Configure TablePlus: When setting up your connection in TablePlus, use the following settings:
-
Host:
127.0.0.1or0.0.0.0 -
Port:
3306 -
User:
sail -
Password:
password(or whatever you have set in your.envfile) -
Database:
your_database_name
-
Host:
Here is a sample configuration for TablePlus:
Host: 127.0.0.1
Port: 3306
User: sail
Password: password
Database: your_database_name
By making these changes, you should be able to connect to your MySQL database running in the Laravel Sail Docker container without encountering the Unix socket error.
If you continue to experience issues, ensure that your Docker containers are running and that there are no firewall rules blocking the connection.
Hope this helps!