Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
build_error
glibcpy 3.10–3.910 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
MySQLdb
✓ import MySQLdb
✗ import mysql.connector
mysql-python exposes its functionality via the 'MySQLdb' module, not 'mysql.connector' which belongs to Oracle's 'mysql-connector-python'.
This quickstart demonstrates how to connect to a MySQL database, create a table, insert data, and fetch results using the `MySQLdb` module. Ensure your MySQL server is running and `MYSQL_HOST`, `MYSQL_USER`, `MYSQL_PASSWORD`, and `MYSQL_DATABASE` environment variables are set with appropriate credentials. Remember to commit changes after DDL and DML operations.
import MySQLdb
import os
try:
# Establish a connection to the MySQL server
# Note: mysql-python (MySQLdb) primarily supports Python 2.x.
# Use 'passwd' for password and 'db' for database name.
conn = MySQLdb.connect(
host=os.environ.get('MYSQL_HOST', 'localhost'),
user=os.environ.get('MYSQL_USER', 'root'),
passwd=os.environ.get('MYSQL_PASSWORD', 'password'),
db=os.environ.get('MYSQL_DATABASE', 'testdb')
)
# Create a cursor object to execute queries
cursor = conn.cursor()
# Create a table (if it doesn't exist)
cursor.execute("""
CREATE TABLE IF NOT EXISTS my_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
age INT
)
""")
conn.commit() # Commit changes for DDL operations
# Insert data
cursor.execute("INSERT INTO my_table (name, age) VALUES (%s, %s)", ("Alice", 30))
cursor.execute("INSERT INTO my_table (name, age) VALUES (%s, %s)", ("Bob", 24))
conn.commit() # Commit changes for DML operations
print(f"Inserted {cursor.rowcount} records.")
# Fetch data
cursor.execute("SELECT id, name, age FROM my_table")
rows = cursor.fetchall()
print("\nData in my_table:")
for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}")
except MySQLdb.Error as e:
print(f"Error: {e}")
finally:
# Close the cursor and connection
if 'cursor' in locals() and cursor:
cursor.close()
if 'conn' in locals() and conn:
conn.close()
print("\nConnection closed.")
Debug
Known issues
breakingThis library (mysql-python 1.2.5) officially supports Python 2.4-2.7 only. It is not compatible with Python 3.x, and attempts to install or run it on Python 3 will typically fail with `ImportError` or `SyntaxError`.fixFor Python 3.x, use `mysqlclient` (a fork providing MySQLdb compatibility) or `mysql-connector-python` (Oracle's official driver). Install with `pip install mysqlclient` or `pip install mysql-connector-python` respectively.
affects: All Python 3.x versions
gotchaInstallation often fails due to missing C development headers for MySQL client libraries or Python itself. This is particularly common on fresh OS installations or minimal Docker images.fixOn Debian/Ubuntu: `sudo apt-get install python-dev libmysqlclient-dev`. On RHEL/CentOS: `sudo yum install python-devel mysql-devel` or `sudo dnf install python-devel mysql-devel`.
affects: All versions on systems without development headers
deprecatedThe `mysql-python` project (MySQLdb) is no longer actively maintained. The last release was in 2014, meaning it receives no new features, performance improvements, or security patches.fixMigrate to `mysqlclient` or `mysql-connector-python` for ongoing support, security, and Python 3 compatibility.
affects: All versions (1.2.5 and older)
Upgrade
Version history
1.2.5latest on PyPI · released Jan 2, 2014
Audit
Dependencies
libmysqlclient-devrequiredRequired C development headers for MySQL client libraries.
python-devrequiredRequired Python development headers for compilation.