Per the mysql-connector-python package bug report linked by steeldriver,
the cause of the issue appears to be that Python 3.12 removed ssl.wrap_socket (it had been marked deprecated since Python 3.7), but the version of the Python mysql.connector package provided by Ubuntu's python3-mysql.connector package is an old version incompatible with Python 3.12 that expects ssl.wrap_socket to still exist.
From the upstream mysql-connector-python repo, it appears that support for Python 3.12 was first introduced in version v8.2.0. But, in Ubuntu 24.04 LTS, if we apt install python3-msql.connector, inspecting /usr/lib/python3/dist-packages/mysql/connector/version.py and /usr/lib/python3/dist-packages/mysql/connector/network.py shows that it is version v8.0.15, which is not documented as supporting Python 3.12, and attempts to call ssl.wrap_socket are still present inside network.py. So we expect that not to work.
Here's an example following Steeldriver's suggested workaround, avoiding use of the globally installed mysql.connector package provided by ubuntu's python3-mysql.connector package that is incompatible with python3.12, and instead installing a newer version of that package from pypi to an isolated python virtual environment:
$ cat issue.sh
#! /usr/bin/env bash
set -uxo pipefail
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade mysql-connector-python
python3 test.py
$ cat test.py
import mysql.connector
db = mysql.connector.connect(host="localhost", user="ray", password="...")
print(db)
exit(0)
Run it with chmod +x ./issue.sh && ./issue.sh.
When I run this, it installs version 9.5.0 of the msql-connector-python package.
MySQL problem: "Exception has occurred: AttributeError: module 'ssl' has no attribute 'wrap_socket' " on python 3.12.3
[Python 3.12] AttributeError: module 'ssl' has no attribute 'wrap_socket'
Python 3.12.7 module ssl has no attribute wrap_socket - Stack Overflow
AttributeError: module 'ssl' has no attribute 'wrap_socket'
OS: Linux Mint 22.1 CinnamonIDE: VS CodePython version: 3.12.3
(apologies for a long post. Half of this is a rant. TL;DR mysql-connector module is installed, but is not connecting due to the SSL having no wrap_socket )
Hey all, this is driving me insane, and its not making sense at all. I'm trying to get MySQL running on my python script and I really want it running...
I've been following the w3schools tutorial on MySQL, and I originally had it connected with no problem. I leave the project to go refactor and maintain my current project. (I didn't touch anything, or install any packages)
When I return, using the same venv and suddenly gives me the error "Module 'ssl' has no attribute 'wrap_socket' " here is the full error. (Pastebin)
Of course, I look up my problem and I find a stack overflow with a similar problem and still not fixed and throwing the same problem. I use pip to uninstall and reinstall mysql-connector-python and still the same problem. I check my installed packages (Pastebin) and its still installed on this venv.
Hell, I even tried the pyOpenSSL and STILL the same problem.
Here's my code:
db = mysql.connector.connect(
host="localhost",
user="me-lol",
password="WpjrslYpjr",
database="VeryCoolDB"
)
# will output when it has
# connected to MySQL server
print("hello world!")If I find a solution, I will share it, so no poor schmuck like me will have to go though this again.