Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar
Level 122

Correct method to specify a port number

I have local development environment using MAMP. The mysql port is 8889.

If I specify in my .env file DB_HOST = localhost:8889 then migrations work fine, but when accessing the database through my model, laravel complains that it cannot find the database.

So I change .env file so that DB_HOST = localhost and DB_PORT specifies 8889. Then in my app/config/database.php I add a line for port and pull in DB_PORT

This now works fine for my eloquent queries, but now migrations fail using Artisan.

Any suggestions about the correct way to specify non-standard port?

0 likes
2 replies
Vijay's avatar

Try this : Add 'unix_socket' line.

'mysql' => array(

    'driver'    => 'mysql',

    'host'      => 'localhost',

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

    'database'  => 'database',

    'username'  => 'root',

    'password'  => 'root',

    'charset'   => 'utf8',

    'collation' => 'utf8_unicode_ci',

    'prefix'    => '',

),
Snapey's avatar
Snapey
OP
Best Answer
Level 122

I found an answer in this github issue. https://github.com/laravel/framework/issues/2719

The answer is to a) specify the bespoke port number and b) to use 127.0.0.1 instead of localhost so that artisan commands don't try and use local sockets.

My .env file looks like;

DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=root

and my app/config/database.php;

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'port'      => env('DB_PORT', '3306'),     // default port number
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
1 like

Please or to participate in this conversation.