Connect to staging database from a local environment - Laravel 5 Connect to staging database from a local environment - Laravel 5 laravel laravel

Connect to staging database from a local environment - Laravel 5


You are trying to connect to another host via a Unix socket, that is not possible because sockets, usually, are only directly visible to the system on which they are created.

For your staging env to work you have to comment out the unix_socket config line and add a port:

'port' => env('DB_PORT', '3306'),

And configure mysql to listen on that port.


This seems to be primarily a networking issue between the Host and Guest.

  • First, let's get the Host to talk to the Guest (VM)
    • Login to VM (assuming you are using VirtualBox, other VMs will be similar)
    • Run ifconfig and note down the IP address. It will be something like 10.0.2.5
    • Go to VM instance window -> Menu -> Network adapters:
      • set Adapter to NAT
      • Click on "port forwarding" at the bottom
      • Create new record (click the + icon on right hand side)
      • Set Host IP = 127.0.0.1
      • For guest IP address enter the value you got earlier (10.0.2.5 - your IP may be different)
      • In both cases for port use 3306
    • Click on OK button twice and you should be out of the settings screens and all done
  • Verify you can connect to MySQL instance via command line on the VM
    • Open a terminal window on VM
    • Run this command mysql -hlocalhost -ujohn -p
    • Enter the password when prompted and you should be able to connect. If not, try the following
      • mysql -h127.0.0.1 -ujohn -p
      • If you still can't connect then make sure you have the correct password
    • Once you are able to connect locally, it's time to connect from Host (Mac)
  • Use the following command:
    • mysql -h127.0.0.1 -ujohn -p
    • Enter the password when prompted
  • If you still can't connect, try turning off the firewall on Guest/Ubuntu


What you could try if you have SSH access to the VM (or any remote actually) then you can try port forwarding the connection over SSH:

ssh foo@bar -L 3307:localhost:3306

Now port 3307 on your local machine is acting as port 3306 on the remote machine. In your config file you can then go

DB_HOST=localhostDB_PORT=3307

The downside of this approach is that you need to have a terminal session setup and running when you want to use your local environment.