A self-contained Python driver for communicating with MySQL servers, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249).
Project description
MySQL Connector/Python enables Python programs to access MySQL databases, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249).
Features
Installation
Connector/Python contains the Classic and X DevAPI connector APIs, which are installed separately. Any of these can be installed from a binary or source distribution.
Binaries are distributed in the following package formats:
On the other hand, the source code is distributed as a compressed file from which a wheel package can be built.
The recommended way to install Connector/Python is via pip, which relies on WHEEL packages. For such a reason, it is the installation procedure that is going to be described moving forward.
Please, refer to the official MySQL documentation Connector/Python Installation to know more about installing from an RPM, or building and installing a WHEEL package from a source distribution.
Before installing a package with pip, it is strongly suggested to have the most recent pip version installed on your system. If your system already has pip installed, you might need to update it. Or you can use the standalone pip installer.
$ pip install mysql-connector-python
Installation Options
Connector packages included in MySQL Connector/Python allow you to install optional dependencies to unleash certain functionalities.
# 3rd party packages to unleash the telemetry functionality are installed
$ pip install mysql-connector-python[telemetry]
This installation option can be seen as a shortcut to install all the dependencies needed by a particular feature. Mind that this is optional and you are free to install the required dependencies by yourself.
Available options:
dns-srv
gssapi
webauthn
telemetry
Sample Code
import mysql.connector
# Connect to server
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="mike",
password="s3cre3t!")
# Get a cursor
cur = cnx.cursor()
# Execute a query
cur.execute("SELECT CURDATE()")
# Fetch one result
row = cur.fetchone()
print("Current date is: {0}".format(row[0]))
# Close connection
cnx.close()
HeatWave GenAI and Machine Learning Support
MySQL Connector/Python now includes an optional API for integrating directly with MySQL HeatWave’s AI and Machine Learning capabilities. This new SDK is designed to reduce the time required to generate proofs-of-concept (POCs) by providing an intuitive Pythonic interface that automates the management of SQL tables and procedures.
The new mysql.ai module offers two primary components:
GenAI: Provides implementations of LangChain’s abstract LLM, VectorStore, and Embeddings classes (MyLLM, MyVectorStore, MyEmbeddings). This ensures full interoperability with existing LangChain pipelines, allowing developers to easily substitute existing components with HeatWave-backed versions.
AutoML: Provides Scikit-Learn compatible estimators (MyClassifier, MyRegressor, MyAnomalyDetector, MyGenericTransformer) that inherit from standard Scikit-Learn mixins. These components accept Pandas DataFrames and can be dropped directly into existing Scikit-Learn pipelines and grid searches.
Note on Dependencies: These features introduce dependencies on langchain, pandas, and scikit-learn. To keep existing installations unchanged and the base connector lightweight, these dependencies are not installed by default. You must install them separately to use the mysql.ai features.
Example: GenAI Chatbot with Memory
This example demonstrates how to use MyLLM within a loop to create a simple chatbot that maintains conversation history.
from collections import deque
from mysql import connector
from mysql.ai.genai import MyLLM
def run_chatbot(db_connection, chat_history_size=5):
# Initialize MyLLM with the database connection
my_llm = MyLLM(db_connection)
# Maintain a limited history for context
chat_history = deque(maxlen=chat_history_size)
system_msg = "System: You are a helpful AI assistant."
while True:
user_input = input("\nUser: ")
if user_input.lower() in ["exit", "quit"]:
break
# Format history and invoke the LLM
history = [system_msg] + list(chat_history) + [f"User: {user_input}"]
prompt = "\n".join(history)
# Invoke HeatWave GenAI
response = my_llm.invoke(prompt)
print(f"Bot: {response}")
# Update history
chat_history.append(f"User: {user_input}")
chat_history.append(f"Bot: {response}")
# Usage
with connector.connect(user='root', database='mlcorpus') as db_connection:
run_chatbot(db_connection)
Example: HeatWave AutoML in a Scikit-Learn Pipeline
This example shows how to use MyClassifier as a drop-in replacement within a standard Scikit-Learn pipeline.
import pandas as pd
from mysql import connector
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from mysql.ai.ml import MyClassifier
# 1. Setup Data (Pandas DataFrame)
X = pd.DataFrame([[0.5, 0.1], [1.0, 0.8], [0.1, 0.2]], columns=["feat1", "feat2"])
y = pd.Series([0, 1, 0], name="target")
# 2. Connect and Train
with connector.connect(user='root', database='mlcorpus') as db_connection:
# Initialize the HeatWave classifier
clf = MyClassifier(db_connection)
# Create a standard Scikit-Learn pipeline
pipe = Pipeline([
("scaler", StandardScaler()),
("mysql_clf", clf)
])
# Fit the model (automates upload and training on HeatWave)
pipe.fit(X, y)
# Predict
preds = pipe.predict(X)
print(f"Predictions: {preds}")
# Score
score = pipe.score(X, y)
print(f"Accuracy: {score}")
Additional Resources
Contributing
There are a few ways to contribute to the Connector/Python code. Please refer to the contributing guidelines for additional information.
License
Please refer to the README.txt and LICENSE.txt files, available in this repository, for further details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mysql_connector_python-9.6.0-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 34.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adabbc5e1475cdf5fb6f1902a25edc3bd1e0726fa45f01ab1b8f479ff43b3337
|
|
| MD5 |
e70ccb7e9bfe084324cb7a6363ea627c
|
|
| BLAKE2b-256 |
181dfe1133eb76089342854d8fbe88e28598f7e06bc684a763d21fc7b23f1d5e
|
File details
Details for the file mysql_connector_python-9.6.0-cp311-cp311-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp311-cp311-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 34.2 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61deca6e243fafbb3cf08ae27bd0c83d0f8188de8456e46aeba0d3db15bb7230
|
|
| MD5 |
532291e2d093f98bb1a9858ba979344e
|
|
| BLAKE2b-256 |
5338ecd6d35382b6265ff5f030464d53b45e51ff2c2523ab88771c277fd84c05
|
File details
Details for the file mysql_connector_python-9.6.0-cp311-cp311-macosx_14_0_x86_64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp311-cp311-macosx_14_0_x86_64.whl
- Upload date:
- Size: 18.4 MB
- Tags: CPython 3.11, macOS 14.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5212372aff6833473d2560ac87d3df9fb2498d0faacb7ebf231d947175fa36a
|
|
| MD5 |
5ea0fdc3e832fabd65e3ab850a26a52a
|
|
| BLAKE2b-256 |
93aa3dd4db039fc6a9bcbdbade83be9914ead6786c0be4918170dfaf89327b76
|
File details
Details for the file mysql_connector_python-9.6.0-cp311-cp311-macosx_14_0_arm64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp311-cp311-macosx_14_0_arm64.whl
- Upload date:
- Size: 17.6 MB
- Tags: CPython 3.11, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
011931f7392a1087e10d305b0303f2a20cc1af2c1c8a15cd5691609aa95dfcbd
|
|
| MD5 |
3cf8b249fac1504bbd8cb827fd99f4a8
|
|
| BLAKE2b-256 |
2a080e9bce000736454c2b8bb4c40bded79328887483689487dad7df4cf59fb7
|
File details
Details for the file mysql_connector_python-9.6.0-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 16.5 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b507372c060eb39e2a10ebcb3eb1b12e1778b0120808062fc23e3856268cd2d9
|
|
| MD5 |
0011733f339d44b7753ed7dcf5c0b283
|
|
| BLAKE2b-256 |
7ced9ff5ff9ea8421ffdf248fa71b9328fd8b9a23e1e3a2a523db5f7d58240c6
|
File details
Details for the file mysql_connector_python-9.6.0-cp310-cp310-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 34.5 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bf932724a2702d8b9cde4bf764b843a35e85c59479a870997d37a2a68a5632d
|
|
| MD5 |
be60924ac30b2622538568478aa8b07a
|
|
| BLAKE2b-256 |
5d801b0012fd86b5a7c0952878e33938ccd4b2862eb882e08a502b99348988d0
|
File details
Details for the file mysql_connector_python-9.6.0-cp310-cp310-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp310-cp310-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 34.2 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
477e86182aefbf693b71ff8bda7679ab4487af64c027759af831a70080aaaeac
|
|
| MD5 |
e806cd41c229b0cc6247e1eba55dc033
|
|
| BLAKE2b-256 |
6b9e0e14e30dddb8f9a5230c6dbce6f874fba2aeaab1ce971dce3912cf81773e
|
File details
Details for the file mysql_connector_python-9.6.0-cp310-cp310-macosx_14_0_x86_64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp310-cp310-macosx_14_0_x86_64.whl
- Upload date:
- Size: 18.4 MB
- Tags: CPython 3.10, macOS 14.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
228000bb951810dad724821d04000174ffcc7fa94b4dcef884b17a3cdae07283
|
|
| MD5 |
0b4e9f25718aaf030975c42b8d8a1141
|
|
| BLAKE2b-256 |
1797d3cbab27663d7b5063d891c6bd322db6cf1cdb42d4e0d2d2ec9a1952c67f
|
File details
Details for the file mysql_connector_python-9.6.0-cp310-cp310-macosx_14_0_arm64.whl.
File metadata
- Download URL: mysql_connector_python-9.6.0-cp310-cp310-macosx_14_0_arm64.whl
- Upload date:
- Size: 17.6 MB
- Tags: CPython 3.10, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
478e035ebcf734b3a1497bfd3eb72ce3632da6384545b08cf6329471b3849b6e
|
|
| MD5 |
46c2280ba53497802d0926c0dd0f2c82
|
|
| BLAKE2b-256 |
1627106f5b7a69381c58cb0ba6bf44e6488969ce6cd9f69f62df340f379141ee
|