{"id":9132,"library":"mysql-python","title":"mysql-python (Legacy MySQLdb)","description":"mysql-python, also widely known as MySQLdb, is a Python 2.x interface to the popular MySQL database server. The last official release, 1.2.5, dates back to 2014 and primarily supports Python versions 2.4 through 2.7. It adheres to the Python Database API Specification v2.0 (PEP 249). For modern Python 3.x development, this library is considered deprecated, with `mysqlclient` (a actively maintained fork providing a compatible API) and `mysql-connector-python` (Oracle's official pure Python driver) being the recommended alternatives.","status":"deprecated","version":"1.2.5","language":"en","source_language":"en","source_url":"https://github.com/farcepest/MySQLdb1","tags":["database","mysql","sql","legacy","python2"],"install":[{"cmd":"pip install mysql-python","lang":"bash","label":"Primary Install"}],"dependencies":[{"reason":"Required C development headers for MySQL client libraries.","package":"libmysqlclient-dev","optional":false},{"reason":"Required Python development headers for compilation.","package":"python-dev","optional":false}],"imports":[{"note":"mysql-python exposes its functionality via the 'MySQLdb' module, not 'mysql.connector' which belongs to Oracle's 'mysql-connector-python'.","wrong":"import mysql.connector","symbol":"MySQLdb","correct":"import MySQLdb"}],"quickstart":{"code":"import MySQLdb\nimport os\n\ntry:\n    # Establish a connection to the MySQL server\n    # Note: mysql-python (MySQLdb) primarily supports Python 2.x. \n    # Use 'passwd' for password and 'db' for database name.\n    conn = MySQLdb.connect(\n        host=os.environ.get('MYSQL_HOST', 'localhost'),\n        user=os.environ.get('MYSQL_USER', 'root'),\n        passwd=os.environ.get('MYSQL_PASSWORD', 'password'),\n        db=os.environ.get('MYSQL_DATABASE', 'testdb')\n    )\n\n    # Create a cursor object to execute queries\n    cursor = conn.cursor()\n\n    # Create a table (if it doesn't exist)\n    cursor.execute(\"\"\"\n        CREATE TABLE IF NOT EXISTS my_table (\n            id INT AUTO_INCREMENT PRIMARY KEY,\n            name VARCHAR(255),\n            age INT\n        )\n    \"\"\")\n    conn.commit() # Commit changes for DDL operations\n\n    # Insert data\n    cursor.execute(\"INSERT INTO my_table (name, age) VALUES (%s, %s)\", (\"Alice\", 30))\n    cursor.execute(\"INSERT INTO my_table (name, age) VALUES (%s, %s)\", (\"Bob\", 24))\n    conn.commit() # Commit changes for DML operations\n\n    print(f\"Inserted {cursor.rowcount} records.\")\n\n    # Fetch data\n    cursor.execute(\"SELECT id, name, age FROM my_table\")\n    rows = cursor.fetchall()\n\n    print(\"\\nData in my_table:\")\n    for row in rows:\n        print(f\"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}\")\n\nexcept MySQLdb.Error as e:\n    print(f\"Error: {e}\")\nfinally:\n    # Close the cursor and connection\n    if 'cursor' in locals() and cursor:\n        cursor.close()\n    if 'conn' in locals() and conn:\n        conn.close()\n    print(\"\\nConnection closed.\")","lang":"python","description":"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."},"warnings":[{"fix":"For 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.","message":"This 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`.","severity":"breaking","affected_versions":"All Python 3.x versions"},{"fix":"On 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`.","message":"Installation 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.","severity":"gotcha","affected_versions":"All versions on systems without development headers"},{"fix":"Migrate to `mysqlclient` or `mysql-connector-python` for ongoing support, security, and Python 3 compatibility.","message":"The `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.","severity":"deprecated","affected_versions":"All versions (1.2.5 and older)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"If on Python 3.x, switch to `mysqlclient` (`pip install mysqlclient`) or `mysql-connector-python` (`pip install mysql-connector-python`). If on Python 2.x, ensure `mysql-python` installed successfully (check for compilation errors).","cause":"Attempting to import `MySQLdb` on a Python 3.x environment, or `mysql-python` was not successfully installed.","error":"ImportError: No module named MySQLdb"},{"fix":"Install system-level development packages. On 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`.","cause":"Missing necessary C development headers for MySQL client libraries (`libmysqlclient-dev`) or Python (`python-dev`), preventing the C extension from compiling.","error":"Command \"python setup.py egg_info\" failed with error code 1 in /tmp/pip-build-xxxx/mysql-python/"},{"fix":"Ensure you are using `import MySQLdb` for `mysql-python` and that the installation was successful. If intentionally using `mysql.connector`, install `mysql-connector-python` (`pip install mysql-connector-python`) and use its API.","cause":"This error typically occurs if you've imported `mysql.connector` expecting `MySQLdb` functionality, or if the `MySQLdb` module was only partially loaded.","error":"AttributeError: 'module' object has no attribute 'connect'"}]}