Pymysql can't connect do MySQL DB: hangs at “self.sock.recv_into(b)”?

徘徊边缘 提交于 2020-03-23 08:02:49

问题


Currently on macOS 10.15 using python 3.7, with MySQL 8.0.19 installed. Developing in VScode with a virtual environment setup. I've created a local database in phpmyadmin as well. I want to connect to it anyway possible. Script:

import pymysql

print("Before")
connection = pymysql.connect(host='localhost',
    user='myUserName', password='myPassword', db='db_name', charset='utf8mb4', 
          cursorclass=pymysql.cursors.DictCursor)
print("After")

When I run the script, execution hangs indefinitely. After printing "After", I have to quit execution. Trackback is:

Traceback (most recent call last):
File "connect.py", line 5, in <module>
  user='myUserName', password='myPassword', db='db_name', charset='utf8mb4', 
  cursorclass=pymysql.cursors.DictCursor)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
  packages/pymysql/__init__.py", line 94, in Connect
  return Connection(*args, **kwargs)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
  packages/pymysql/connections.py", line 325, in __init__
  self.connect()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
  packages/pymysql/connections.py", line 598, in connect
  self._get_server_information()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
  packages/pymysql/connections.py", line 975, in _get_server_information
  packet = self._read_packet()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
packages/pymysql/connections.py", line 657, in _read_packet
  packet_header = self._read_bytes(4)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site- 
  packages/pymysql/connections.py", line 691, in _read_bytes
  data = self._rfile.read(num_bytes)
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/
python3.7/socket.py", line 589, in readinto
  return self._sock.recv_into(b)
KeyboardInterrupt

No idea why this is occurring. What is happening here? I have searched a ton of questions on stack overflow and none helped me.


回答1:


Problem was solved by adding the "unix_socket" param in pymysql connect function. See answer here: enter link description here

import pymysql

print("Before")
conn = pymysql.connect(db='w_dev', user='root', passwd='celebrate', 
unix_socket="/tmp/mysql.sock")
print("After")

This script worked.



来源:https://stackoverflow.com/questions/60317075/pymysql-cant-connect-do-mysql-db-hangs-at-self-sock-recv-intob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!