Search code examples
phpmysqllaravelmacosmamp

Connection refused to MySQL on laravel and MAMP


I have a laravel installation on my macos, and MAMP running mysql, i know i have all the info right in the .env file, the ports are right, the user is right, the database exists, the admin and password are right. What am i missing? i am getting this error:

*PDOException::("SQLSTATE[HY000] [2002] Connection refused")*

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:bWFE4NsYpgdx10/8ODfLxLPJxru5bgY7m4KMg9LwHk8=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=some
DB_PASSWORD=some

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Solution

  • The thing is you have to assign the mysql socket from MAMP in the .env like so.

    DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
    

    EDIT: i am using laravel 5.8, on previous versions you have to add the socket directly on the connections array like so:

    config/database.php

    'mysql' => [
                'driver' => 'mysql',
                'url' => env('DATABASE_URL'),
                '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' => '/Applications/MAMP/tmp/mysql/mysql.sock', <----HERE
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => true,
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],