1

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.

J.E.C.
  • 2,616
  • 2
  • 18
  • 21

1 Answers1

1

Problem was solved by adding the "unix_socket" param in pymysql connect function. See answer here: pymysql can't connect to MySQL on localhost

import pymysql

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

This script worked.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
J.E.C.
  • 2,616
  • 2
  • 18
  • 21