9

I'm working with MAMP on my local development server on my laravel application and I'm trying to figure out how I can safely setup my server so I don't have to use the following into the database connections mysql array because that should only be used when I'm on my development server. It works when I add the line into the mysql array however that isn't used if I was on a production server. Any ideas?

'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',

.env.development.php

<?php

return [
    'DB_HOST' => '127.0.0.1',
    'DB_USERNAME' => 'root',
    'DB_PASSWORD' => '1234',
    'DB_NAME' => 'mytable'
];

app/config/database.php

'connections' => array(

        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => getenv('DB_HOST'),
            'database'  => getenv('DB_NAME'),
            'username'  => getenv('DB_USERNAME'),
            'password'  => getenv('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
user3732216
  • 1,579
  • 8
  • 29
  • 54

5 Answers5

16

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

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

On config/database.php:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'unix_socket' => env('UNIX_SOCKET'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

On .env:

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mytable
DB_USERNAME=root
DB_PASSWORD=1234
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Fausto Braz
  • 184
  • 3
  • 15
5

Check the environment detection part in the bootstrap/start.php. You should add your machine's name to the array that has local key. (If you don't know your machine's name, run hostname in terminal. If it's something stupid, Google how to change it. It's pretty simple.) Then copy and paste your database configurations to app/config/local/database.php. Create the file if it doesn't exists.

Hkan
  • 3,243
  • 2
  • 22
  • 27
  • Good answer but nothing to do with the question. I was asking unix socket specific. – user3732216 Dec 08 '14 at 19:51
  • @user3732216 Your question stated you need to use the `unix_socket` value on your dev machine alone. So just include that line for the config in your `app/config/local/database.php`, and not include in your production `app/config/database.php`. – Bogdan Dec 08 '14 at 20:37
  • But if you notice I'm using environment variables and what not. – user3732216 Dec 08 '14 at 21:13
  • 1
    I understood that. Proceed with what I said. If you use `development` as your environment name, then create the file `app/config/development/database.php` and copy the exact contents of `app/config/database.php` into this file and just add the line about that socket thing. – Hkan Dec 08 '14 at 21:42
1

Make sure MAMP preference is set to Apache port: 80, Nginx Port: 80, MySQL Port: 3306

Here's what worked for me with Laravel 5.7:

go to config/database.php and find the line 54 below:

before: 'unix_socket' => env('DB_SOCKET', ''),

After: 'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),

Save the file.

Then in terminal run: php artisan config:cache php artisan migrate

JackWalsh
  • 43
  • 3
0

If none of the above solutions worked for you,

Try actually starting your webserver as this was the fix for me

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Friendly Code
  • 1,585
  • 1
  • 25
  • 41