10

I was developing my application originally in Laravel 4.2 but have since decided to move it to the 5.0 version so that it covers a lot more changes and strengths that 5.0 has over 4.2.

I am trying to run my migratiosn however I am getting the error:

[PDOException]
  SQLSTATE[HY000] [2002] No such file or directory

I looked into this and noticed how it is because I'm running MAMP for my server instead of vagrant and homestead. I'm not knocking the uses of those two but I at this point feel more comfortable with MAMP until it fails me. The reason I know its MAMP is because of needing to declare the unix socket value to be used.

Now on my 4.2 version of my application I have the following:

'mysql' => array(
    'driver'    => 'mysql',
    'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
    'host'      => getenv('DB_HOST'),
    ...
),

With my Laravel 5.0 version I am making use of the .env file for my Environment variables and not sure how I need to do this so that it knows to use the unix socket value.

Cans someone clue me into how I should adopt this into the new version or a better way to add it into the settings so that I don't have to do that?

user3732216
  • 1,579
  • 8
  • 29
  • 54
  • and are you sure the unix socket is at this location? have you installed MAMP in a non-default location? – Félix Adriyel Gagnon-Grenier Mar 27 '15 at 16:29
  • Yes and I know it is correct. – user3732216 Mar 27 '15 at 16:46
  • yes to what? I dare say that there is no file named `mysql.sock` on your computer at the specified place. Computers don't make mistakes on that. If it says there is no file there, it is because there is no file there. If you installed your MAMP in a non-default location, you may solve your problem by updating your config with the good path to the `mysql.sock` file, but MAMP themselves do not recommend installing their software somewhere else, because many problems such as this one will arise. – Félix Adriyel Gagnon-Grenier Mar 27 '15 at 17:12
  • I know the path to unix is correct. – user3732216 Mar 27 '15 at 17:21
  • Ok. Sorry, I really do not want us to compete. What I'm trying to tell you is that the exception returning `No such file or directory` is **not** making an error, leaving us two choices, either the file is not where you write it is, or the function appends something to the path, making it wrong. But please, please, *please*, **PLEASE** stop believing the computer is making a mistake. The file it is looking for is not where it is, period. There's nothing else to it. Find the exact path the function is trying to include, and post it here. – Félix Adriyel Gagnon-Grenier Mar 27 '15 at 19:24

5 Answers5

25

Try this:

'mysql' => array(
'driver'    => 'mysql',
'unix_socket'   => getenv('UNIX_SOCKET'),
'host'      => getenv('DB_HOST'),
...
),

In .env add

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Vlad Slobodkin
  • 266
  • 3
  • 3
  • Just tested this out. Worked like a charm! – Troy Apr 08 '15 at 20:28
  • It should be noted that some installs of Laravel 5 don't come with a config/database.php file by default. I don't know why, but I had to create one and that's not really mentioned in the Laravel 5 documentation (that I could find.) – russellmania Jun 24 '15 at 16:02
  • Thanks for sharing solutions. I am using Laravel 5.3.x – Fairuz Sulaiman Oct 30 '16 at 21:13
  • 1
    in Laravel 5.5 and above only need to add `DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock` to .env and it will work – Rory Apr 19 '19 at 19:48
8

though quite old question, but still can help others. so adding answer.

there is even simple solution. add this to ur .env file

DB_HOST=localhost;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock
6

In laravel 5.5 the unix_socket changes to DB_SOCKET

inside .env file :

DB_USERNAME=root
DB_PASSWORD=root
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

inside config/database.php:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
2

Thanks, help fix migration issue for me using:

MAMP PRO 4.2
Laravel 5.5

inside .env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=<database name>
DB_USERNAME=<username - default root>
DB_PASSWORD=<password - default root>
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

inside config/database.php:

'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', '<database name>'),
        'username' => env('DB_USERNAME', '<username - default root>'),
        'password' => env('DB_PASSWORD', '<password - default root>'),
        'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],

Also don't forget to add to app/Providers/AppServiceProviders.php:

use Illuminate\Support\Facades\Schema;
public function boot()
{
   Schema::defaultStringLength(191);
}
  • This works, but beware in .env file it has to be `DB_SOCKET` not `UNIX_SOCKET`, please correct it. – tinyCoder Apr 04 '18 at 09:32
  • Made edit based on more recent info by @tinyCoder and Esteban Bautista regarding the UNIX_SOCKET change to DB_SOCKET. – DerekOBrien Apr 19 '18 at 09:26
-3

I created a StackOverflow account solely to answer this question, and maybe help prevent someone going through the pain that I went through.

Answers that I found online ranged from changing 127.0.0.1 to localhost, changing the port from 3306 to 33060 and vice-versa, and making sure the unix_socket was correct.

The solution that solved my problem was changing:

DB_CONNECTION=mysql
DB_HOST=localhost

to

DB_CONNECTION=mysql
DB_HOST=mysql

I hope this helps someone out there. It took me 4 hours to find this painfully obvious solution...and it was found 1min29s into an obscure YouTube video with under 1000 views.

  • 1
    Unless your computer's hostname is `mysql` and you have your database listening on the network, this will not work. – miken32 Feb 26 '20 at 17:28