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

mostafa-amine's avatar

How to connect to a remote database via ssh using PHP?

Hey developers, I want to know how to connect to a remote database via SSH using PHP?

0 likes
3 replies
JussiMannisto's avatar
Level 50

For that, you'd have to establish an SSH tunnel using something like autossh.

Do you control the database server? If this is a live service, it's better to use direct DB connections secured with TLS. You can restrict access to specific IPs using firewall rules.

1 like
martinbean's avatar

@mostafa-amine You don’t, because that’s just going to cause unnecessary latency in your application as it establishes an SSH connection, and then connects to the database. Your web app’s server should be whitelisted to be allowed to connect to the MySQL database server instead.

1 like
mostafa-amine's avatar

Cool, guys! I got valuable information. I really appreciate it!

Please or to participate in this conversation.