"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Securely Connect to a Remote MySQL Server Over SSH in PHP?

How to Securely Connect to a Remote MySQL Server Over SSH in PHP?

Published on 2024-11-08
Browse:156

How to Securely Connect to a Remote MySQL Server Over SSH in PHP?

Securely Connect to a Remote MySQL Server over SSH in PHP

To establish a secure tunnel for PHP database connectivity, the following SSH tunnel solution offers a robust approach.

SSH Tunnel Setup

  1. Create an SSH tunnel to your MySQL database server. For this, use a command like:
ssh -fNg -L 3307:10.3.1.55:3306 [email protected] 
  1. Choose a local port (e.g., 3307) and forward all traffic to the remote database server's IP and port (10.3.1.55:3306).

PHP Connection

Once the SSH tunnel is established, you can connect to the database securely through PHP using:

$smysql = mysql_connect( "127.0.0.1:3307", "dbuser", "passphrase" );
mysql_select_db( "db", $smysql ); 

This will establish a connection to the remote database via the encrypted SSH tunnel.

Benefits

  • Encrypted Communication: Data is transmitted over an encrypted SSH connection, ensuring confidentiality.
  • Security Enhancement: Limiting direct access to the database server reduces security vulnerabilities.
  • Compatibility: Can be used with various PHP database functions, such as mysql_connect and mysqli_connect.
Release Statement This article is reprinted at: 1729521196 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3