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 from remote website

I have a php site set up on Forge (not Laravel). Another related site on a different server needs to access the database for my Forge site. I researched how to do this a while ago but now I can not find the location where I had found a way to do it.

I did search the Forge documentation and got as far as setting up firewall rule for the remote site. But there must be more to it than that. In my remote site (non-Forge) I set up the database connection like:

$link_lms=new mysqli('x.x.x.x','forge','*******','db_name');

Of course x.x.x.x is the real IP of my forge server and ******* is the actual password. I was pretty sure that wouldn't work since you can only access the DB via SSH. So, I'm kind of stumped as to the rest of the steps, assuming this is possible.

0 likes
3 replies
LaryAI's avatar
Level 58

To connect to a MySQL database hosted on a Laravel Forge server remotely, there are a few things you need to set up, since Forge configures MySQL to only allow local connections by default. Here are the required steps:

1. Allow Remote Connections in MySQL

By default, MySQL on Forge listens only on 127.0.0.1 (localhost), which means external servers can’t connect directly.

  • Change the MySQL bind address

SSH into your Forge server and edit the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find the line:

bind-address = 127.0.0.1

Change it to:

bind-address = 0.0.0.0

Warning: This makes MySQL accessible from any IP. Restrict access via firewall rules (see below).

Restart MySQL:

sudo service mysql restart

2. Create a Specific MySQL User For Remote Access

Login to MySQL on your Forge server:

mysql -u root -p

Create a user that is allowed to connect from the remote server's IP address:

CREATE USER 'your_user'@'remote_server_ip' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON db_name.* TO 'your_user'@'remote_server_ip';
FLUSH PRIVILEGES;

Replace 'your_user', 'your_password', 'db_name', and remote_server_ip accordingly.


3. Allow the Remote Server Through the Firewall (On Forge)

In the Forge dashboard, for your server:

  • Go to "Network" > "Firewall".
  • Add a rule to allow your remote server’s IP address access to port 3306 (the MySQL port).

4. Connect From Your Remote Site

Now use the remote Forge server's public IP as the mysqli host:

$link_lms = new mysqli('forge_server_ip', 'your_user', 'your_password', 'db_name');

5. (Recommended) Use an SSH Tunnel (for Better Security)

Instead of making MySQL globally accessible, consider connecting through an SSH tunnel, which is more secure. On your remote server, set up the tunnel:

ssh -L 3307:127.0.0.1:3306 forge_user@forge_server_ip

Then, on your PHP site, connect to localhost:3307:

$link_lms = new mysqli('127.0.0.1', 'forge_db_user', 'password', 'db_name', 3307);

This forwards all traffic securely through SSH.


Summary

  • For simplicity, open MySQL (bind-address, user privileges, firewall).
  • For security, use an SSH tunnel (no changes to MySQL config or firewall).

Forge security best practice: Don’t open MySQL to the world; use tunneling/whitelist only necessary IPs.


Let me know if you need more specifics on any of these steps!

earmsby's avatar

How do I set up the tunnel on my remote server?

martinbean's avatar

@earmsby This isn’t really a Forge issue. You have a database. That database will be set up to listen to connections from specified IP addresses. For security reasons, you usually only allow connections from local host (scripts running on the same machine).

If you want to connect to that database from a completely different server, then you will need to whitelist that server’s IP address so that the database will allow connections from it.

Please or to participate in this conversation.