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

earmsby's avatar

Connect to Forge DB on localhost?

Is it possible to connect to the database (Mysql) on my Forge server while working on locahost? I'm trying to test something with data from a table on the server. I changed the values in my .env file but the connection times out.

I'm assuming that's because I have to connect to the db over SSH. I was able to configure the SSH connection in my GUI on my local machine. Is there some similar configuration that can be done with the Laravel project and/or Herd?

0 likes
2 replies
LaryAI's avatar
Level 58

To connect to a MySQL database on a Forge server from your local development environment, you can use an SSH tunnel. This allows you to securely connect to the remote database as if it were running locally. Here's how you can set it up:

Step 1: Set Up SSH Tunnel

  1. Open your terminal and run the following command to create an SSH tunnel. Replace forge_user, forge_server_ip, and 3306 with your Forge server's SSH username, IP address, and MySQL port, respectively:

    ssh -L 3307:127.0.0.1:3306 forge_user@forge_server_ip
    
    • 3307 is the local port you will use to connect to the remote MySQL server. You can choose any available port on your local machine.
    • 127.0.0.1:3306 is the address and port of the MySQL server on the Forge server.
  2. Keep this terminal window open while you work, as closing it will terminate the SSH tunnel.

Step 2: Update Your .env File

Modify your Laravel project's .env file to connect to the database through the SSH tunnel:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
  • DB_HOST should be 127.0.0.1 because the SSH tunnel makes the remote database appear as if it's local.
  • DB_PORT should match the local port you specified in the SSH tunnel command (3307 in this example).

Step 3: Test the Connection

Run a simple database query or use Laravel's artisan command to ensure the connection is working:

php artisan migrate:status

If everything is set up correctly, you should see the status of your migrations without any connection errors.

Additional Tips

  • SSH Key Authentication: Ensure your local machine can SSH into the Forge server without a password prompt by using SSH keys.
  • Firewall Rules: Make sure your Forge server's firewall allows SSH connections from your IP address.
  • Database User Permissions: Verify that the database user has the necessary permissions to access the database.

By following these steps, you should be able to connect to your Forge server's MySQL database from your local development environment securely.

1 like
martinbean's avatar

@earmsby I’d really urge you to not connect to your production database and “test” things. It just takes one time to forget you’ve got your .env file pointing to your production database, running a destructive command like php artisan migrate:fresh --seed, and you’ve lost all of your production data.

Use seeders to generate data in your local database for testing. Again, you shouldn’t be copying “live” data out of the production data store as it can lead to embarrassing issues (accidentally sending test mails with embarrassing test copy to real customers’ email addresses) but may also be flat-out illegal under data protection laws for your jurisdiction. It would be here in the UK, for example, as personal (and potentially sensitive) data is ending up on devices (i.e. developer’s individual machines) where there is no real legal justification for it to be there.

Please or to participate in this conversation.