【发布时间】:2020-09-18 16:38:08
【问题描述】:
我在 Python 3.8 中使用 pymysql 以允许 Python 连接到另一个 MySQL 服务器。我收到一个错误:
with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection:
AttributeError: __enter__
蟒蛇:
from sshtunnel import SSHTunnelForwarder
import pymysql
with SSHTunnelForwarder(
('76.21.187.192', 2222),
ssh_username = '123',
ssh_password = '123',
remote_bind_address = ('127.0.0.1', 3306)) as server:
with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection:
print('OK')
如何正确连接MySQL?
【问题讨论】: