问题描述
当前在使用python 3.7的macOS 10.15上,安装了MySQL 8.0.19.使用虚拟环境设置在VScode中进行开发.我也在phpmyadmin中创建了一个本地数据库.我想无论如何都可以连接到它.脚本:
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")
运行脚本时,执行将无限期挂起.打印"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.
推荐答案
问题是通过在pymysql连接函数中添加"unix_socket"参数来解决的.在此处查看答案:在此处输入链接描述
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")
此脚本有效.
这篇关于Pymysql无法连接MySQL DB:挂在"self.sock.recv_into(b)"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!