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

Coderooney's avatar

Homestead remote database connections

I'm having trouble figuring out how to connect to databases. When I run 'php artisan migrate' or '$object->save()' form tinker, I get an error, most commonly: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. If I SSH into homestead, then run 'mysql', I get: ERROR 1045 (28000): Access denied for user 'vagrant'@'localhost' (using password: NO)

My best guess is that it's a credentials issue or a port issue, but I haven't found the same situation online. I've been at this for two weeks, trying Laravel with XAMPP in Linux Mint, countless reinstallations of VirtualBox/Vagrant/Homestead, giving Laragon a try and starting on a different computer yet I still keep getting the same errors. So at this point I'm left wondering how much a framework needs to crush a beginner and what super obvious thing my little brain is missing. I turn to you in humble defeat.

This is how I setup my latest attempt on a second computer (Windows 7 64-bit):

  1. Fresh install of VirtualBox 4.3.28
  2. Fresh install of Vagrant 1.7.2 + restart
  3. Fresh install PHP 5.6.9 standalone
  4. php_mbstring.dll, php_openssl.dll, php_mysql.dll, php_pdo_mysql.dll, php_pgsql.dll, php_pdo_pgsql.dll and '"extension_dir = "ext"' uncommented from php.ini
  5. Fresh install of composer (using .exe installer)
  6. Git for Windows installed
  7. Vagrant box added with vagrant box add laravel/homestead
  8. Homestead installed with composer global require "laravel/homestead=~2.0"
  9. 'C:\Users\Coderooney\AppData\Roaming\Composer\vendor\bin' added to environment path
  10. Homestead initialised with homestead init
  11. SSH keys created in Git Bash with ssh-keygen -t rsa -C "you@homestead"
  12. Configured homestead.yaml
  13. Changed homestead.rb port forward 80 => 8000 to 80 => 7020
  14. Added '192.168.10.10 laraduck.app' to hosts file
  15. Laravel installer installed with composer global require "laravel/installer=~1.1"
  16. Laravel installed with laravel new laraduck in C:\dev\projects
  17. VM booted with homestead up
  18. SSH into VM with homestead ssh
  19. App name served with serve laraduck.app /home/vagrant/dev/laraduck/public
  20. http://laraduck.app, http://localhost:7020 and http://127.0.0.1:7020 all show Laravel 5 welcome page
  21. Stuff generally works, php artisan works, following "Laravel 5 Fundamentals" series goes fine until lesson 7 - Migration
  22. Can't connect to remote database, whether using MySQL or PostgreSQL. $object->save(); in tinker doesn't work. php artisan migrate doesn't work. Running mysql in SSH gives me 'Access denied'.

My homestead.yaml:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: C:\Users\Coderooney\.ssh\id_rsa.pub

keys:
    - C:\Users\Coderooney\.ssh\id_rsa

folders:
    - map: C:\dev
      to: /home/vagrant/dev

sites:
    - map: laraduck.app
      to: /home/vagrant/dev/projects/laraduck/public

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 93000
#       to: 9300
#     - send: 7777
#       to: 777
#       protocol: udp

My database.php is untouched. 'mysql' section:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

My .env is mostly untouched:

APP_ENV=local
APP_DEBUG=true
APP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //My actual .env has a real key

DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

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

Let me know what I can do to help you help me help you help me. Thank you so much.

0 likes
10 replies
TimeSocks's avatar

Change your database.php config to match the app.env file's details.

If you want to create a new database, you can access mysql in your VM with:

mysql -uhomestead -p

then enter the password secret when prompted.

1 like
Coderooney's avatar

Okie pokie thanks, I'm giving this all a try now.

bashy's avatar
bashy
Best Answer
Level 65

I would suggest using the full LAN IP so you do it anywhere in the LAN network. That's what I do (192.168.10.10)

1 like
TimeSocks's avatar

@bobbybouwmann is it not bad practice to include the password in the command line though? Sure I've read that somewhere.

Coderooney's avatar

@TimeSocks @bobbybouwmann Yepperooney, both mysql -uhomestead -p and mysql -uhomestead -psecret allowed me to access MySQL with the latter not requiring a prompt. I then get a 'mysql' prompt as expected- thanks :) My cat often sleeps at my desk, so she could be stealing all my command line passwords... As to where you may have read that, MySQL itself throws it up when you enter -psecret!: 'Warning: Using a password on the command line interface can be insecure."

I replaced the database.php variables to match those in my .env, though not much difference. I thought env('DB_HOST', 'localhost') referenced the .env file's DB_HOST value meaning you don't have to hard code it- is that not true?

@bashy Huhnn. You know, that just worked. I know I tried 192.168.10.10 (amongst others: localhost, 127.0.0.1, 192.168.10.10:3306, localhost:54320, etc) before to no avail. Must've been a combination of things. php artisan migrate now successfully creates the migrated table and gives no errors. Committing with $object->save(); in tinker also properly interacts with the database. It feels like such a simple answer to so much hair-pulling, but I'm not complaining about a happy ending!

Thank you all so much- now I can get back to learning :D

bashy's avatar

Just think that

  1. 127.0.0.1 will be relative to where you run the command.
  2. Using the full IP will always be the correct machine/server no matter where you run the command from.
  3. Using localhost is the same as 127.0.0.1
  4. Remote access needs to be enabled (/etc/mysql/my.cnf - comment out bind-address) if you want to be able to connect from outside of 127.0.0.1/localhost
1 like
Coderooney's avatar

That actually makes sense now that you put it like that. Through my oodles of Googles, I just kept seeing 'try this IP!' and 'this IP might work!' and tried them all without understanding why.

Thanks again.

1 like

Please or to participate in this conversation.