Home > Database > Mysql Tutorial > How to Securely Connect to MySQL via SSH Tunneling in Python?

How to Securely Connect to MySQL via SSH Tunneling in Python?

Patricia Arquette
Release: 2024-12-21 16:25:10
Original
491 people have browsed it

How to Securely Connect to MySQL via SSH Tunneling in Python?

Connecting Python to MySQL via SSH Tunnelling

In Python applications, accessing MySQL databases is often necessary. To establish secure connections, one technique involves using an SSH tunnel. This article explores how to leverage SSH key pairs to create an SSH tunnel and facilitate MySQL connectivity through Python.

Consider the following Python code:

import MySQLdb
db = MySQLdb.connect(host="sql.domain.com",
     user="dev", 
      passwd="*******", 
      db="appdb")
Copy after login

While the above code establishes a direct connection to the MySQL server, there may be scenarios where an SSH tunnel is required for enhanced security. An SSH tunnel encrypts the connection, providing a secure channel for communication.

To set up an SSH tunnel and connect to MySQL using Python, follow these steps:

  1. Import Required Modules: In your Python script, include the necessary modules, including pymysql, paramiko, pandas, and sshtunnel.
  2. Establish SSH Connection: Use paramiko to create an SSH client and connect to the SSH server. Specify the host, username, and private key path.
  3. Configure SSH Tunnel: Utilize the sshtunnel module to create an SSH TunnelForwarder object. Define the remote bind address as the MySQL server hostname and port.
  4. Establish MySQL Connection: Use pymysql to connect to the local MySQL server through the SSH tunnel. Specify the port as the local bind port of the SSH tunnel.
  5. Execute Queries: Run SQL queries as usual through the established MySQL connection.
  6. Close Connections: After completing your operations, close the MySQL and SSH connections to release resources.

Here's an example code snippet that demonstrates these steps:

import pymysql
import paramiko
import pandas as pd
from paramiko import SSHClient
from sshtunnel import SSHTunnelForwarder
from os.path import expanduser

# SSH Parameters
home = expanduser('~')
mypkey = paramiko.RSAKey.from_private_key_file(home + pkeyfilepath)
ssh_host = 'ssh_hostname'
ssh_user = 'ssh_username'
ssh_port = 22

# MySQL Parameters
sql_hostname = 'sql_hostname'
sql_username = 'sql_username'
sql_password = 'sql_password'
sql_main_database = 'db_name'
sql_port = 3306

with SSHTunnelForwarder(
        (ssh_host, ssh_port),
        ssh_username=ssh_user,
        ssh_pkey=mypkey,
        remote_bind_address=(sql_hostname, sql_port)) as tunnel:
    conn = pymysql.connect(host='127.0.0.1', user=sql_username,
            passwd=sql_password, db=sql_main_database,
            port=tunnel.local_bind_port)
    # Perform database operations here...
    conn.close()
Copy after login

By implementing these techniques, you can securely connect to a MySQL server via an SSH tunnel in Python. This approach ensures data confidentiality and integrity, making it suitable for applications that require elevated security measures.

The above is the detailed content of How to Securely Connect to MySQL via SSH Tunneling in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template