简体   繁体   English

Python mysql(使用pymysql)无法连接

[英]Python mysql (using pymysql) failing to connect

I am using Python Pymysql to connect to AWS RDS db. 我正在使用Python Pymysql连接到AWS RDS数据库。 This was working all these days but now I am not able to connect to the db. 这些天一直在工作,但是现在我无法连接到数据库。 However when I am using the same machine and same credentials it does connect through Mysql workbench, but through pycharm this does not work., 但是,当我使用同一台机器和相同的凭据时,它确实通过Mysql工作台连接,但是通过pycharm无法连接。

Code : 代码:

   configParser = ConfigParser.RawConfigParser()
   configFilePath = r'docs/credentials.cfg'
   configParser.read(configFilePath)

   User = configParser.get('aws-config','user')
   Pswd = configParser.get('aws-config','pswd')
   Host = configParser.get('aws-config','hostdps')
   Databs = configParser.get('aws-config','databs')
   db = pymysql.connect(host=Host, user=User, password=Pswd, db=Databs, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor,local_infile=True)

Error : 错误:

File "/usr/local/lib/python2.7/dist-packages/pymysql/__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 688, in __init__
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 906, in connect
    self._request_authentication()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1114, in _request_authentication
    auth_packet = self._read_packet()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 961, in _read_packet
    packet_header = self._read_bytes(4)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 995, in _read_bytes
    "Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([Errno 104] Connection reset by peer)')

How to make sure that this does not happen on production? 如何确保在生产中不会发生这种情况? (prevent such failures in the future). (防止将来发生此类故障)。

why u not use connection pool for these? 为什么你不使用这些连接池?
connection pool can auto manage connection,so can fix this. 连接池可以自动管理连接,因此可以解决此问题。

U can try any python mysql pool . 您可以尝试任何python mysql池。
pool example 池示例
for this this link or directly use github handy code . 为此, 此链接或直接使用github方便的代码。 PyMysqlPool PyMysqlPool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM