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

jrdavidson's avatar

DB_HOST for ENV file.

Can someone explain why I have to do have DB_HOST=127.0.0.1 for some tasks but it makes me have to do DB_HOST=localhost for others.

0 likes
17 replies
jrdavidson's avatar

So my thing is why is it that I have to have one at one point vs. one another point. Why can't I just have one and use it.

jrdavidson's avatar

I've looked at some of the videos were @JeffreyWay uses mysql instead of lite and I did what he did but he doesn't get these issues.

Snapey's avatar

I think I found that 127.0.0.1 should work in all circumstances but you also might need to specify the port number. Using 'localhost' causes php to connect via sockets rather than TCPIP.

For MAMP configured mysql on my machine, in .env;

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

and then in config/database/php

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),                            // additional line for the port
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

jrdavidson's avatar

I stopped using MAMP and now I use Homestead.

jrdavidson's avatar

So as far as I know all my environment variables are correct.

jrdavidson's avatar

Any thoughts on how to handle this if I'm using Homestead?

jekinney's avatar

If your using homestead I suggest using homesteads ip.

It's located in your homestead yaml file. First or second line. This should eliminate any issues.

2 likes
jrdavidson's avatar

Why doesn't @JeffreyWay need to do that when he need to do that in the videos that talk about seeding and the videos that talk about Homestead?

Snapey's avatar

If you are running artisan commands on the homestead vm itself then then use 127.0.0.1

jrdavidson's avatar

Right but if I load the page up in my browser I get a message about my database connection and to get it to work I have to put localhost. So I constantly go back and forth.

Snapey's avatar

What port number are you using?

jrdavidson's avatar

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=33060 DB_DATABASE=ringside DB_USERNAME=homestead DB_PASSWORD=secret

eduphp8's avatar
eduphp8
Best Answer
Level 26

I think I get what you are doing wrong...

the correct port is DB_PORT=3306

33060 is used when you need to access your Homestead database from outside programs

you can execute artisan comands from inside Homestead using the vagrant ssh command to access it

if you are executing artisan commands from outside the virtual machine you need change the port to 33060 but if you execute it from inside you dont need to change anything

1 like

Please or to participate in this conversation.