pythonanywhere 유료 요금제 MySQL 외부에서 사용하는 법
2024. 12. 19. 03:05ㆍ기타
반응형
상황
- pythonanywhere 유료 요금제 사용 중 (5달러)
- pythonanywhere의 Database인 MySQL은 유료 요금제 사용 시 외부에서 접근이 가능하다.
무료 버전 사용 시 pythonanywhere에서 돌리는 코드에서만 데이터베이스에 접근이 가능하다.
- 외부에서 pythonanywhere의 데이터베이스에 접근하는 파이썬 코드
환경 설정
- sqlclient 설치
- sshtunnel 설치
파이썬 코드
import MySQLdb
import sshtunnel
# 타임아웃 설정
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
# SSH 터널 설정
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='본인 username',
ssh_password='본인 password',
remote_bind_address=('본인 username.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = MySQLdb.connect(
host="127.0.0.1",
user="본인 username",
password="데이터베이스 password",
db="본인 username$default",
port=tunnel.local_bind_port,
)
cursor = connection.cursor()
# 하고 싶은 작업하기 (아래는 예시)
cursor.execute(
f"""
SELECT img, link
FROM Thumbnails
WHERE title = '{title}'
"""
)
''' 이런저런 작업 '''
connection.commit()
cursor.close()
connection.close()
참고
https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/
Accessing your MySQL database from outside PythonAnywhere
Warning -- this will only work in paid accounts MySQL databases on PythonAnywhere are protected by a firewall, so external computers can't access them. However, if you have a paid account, you can ac
help.pythonanywhere.com
반응형
반응형
'기타' 카테고리의 다른 글
2025 전기 유니스트(UNIST) 컴퓨터공학과 대학원 면접 후기 (1) | 2024.09.30 |
---|