我正在将pymysql与Python 3.8配合使用,以允许Python与其他MySQL服务器建立连接。我收到错误消息:
with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection:
AttributeError: __enter__
python:
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?
答案 0 :(得分:1)
在python中使用with块时,with语句中的对象将调用其 enter 方法,with内的块将运行,然后 exit 被调用(如果引发了则可选地带有异常信息)。因此,如果您在课堂上没有定义 enter ,则会看到此错误。
否则,您可以使用:
连接= pymysql.connect('127.0.0.1',用户='123',密码='123',端口= server.local_bind_port)