-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_database.py
More file actions
42 lines (36 loc) · 1.11 KB
/
Copy pathaccess_database.py
File metadata and controls
42 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from sshtunnel import SSHTunnelForwarder
import mysql.connector
# SSH tunnel settings
ssh_host = '142.132.147.131'
ssh_port = 22
ssh_username = 'mysqltunnel'
ssh_password = 'uH3iN0cP3hZ5kH3o'
# MySQL settings
mysql_host = '127.0.0.1' # The MySQL server's IP address (localhost because we're using an SSH tunnel)
mysql_port = 3306 # The MySQL server's port (default is 3306)
mysql_username = 'root' # MySQL username
mysql_password = 'Gp570402BG' # MySQL password
mysql_db = 'qira' # MySQL database name
# Create the SSH tunnel
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_username,
ssh_password=ssh_password,
remote_bind_address=(mysql_host, mysql_port)
) as tunnel:
# Connect to MySQL
conn = mysql.connector.connect(
host=mysql_host,
port=tunnel.local_bind_port,
user=mysql_username,
password=mysql_password,
database=mysql_db
)
# Do some MySQL operations here
cursor = conn.cursor()
cursor.execute("SHOW DATABASES;")
for x in cursor:
print(x)
# Close MySQL connection
cursor.close()
conn.close()