{"id":537,"library":"mysql-connector-python","title":"MySQL Connector/Python","description":"MySQL Connector/Python is 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). It also includes an implementation of the X DevAPI for working with the MySQL Document Store. The library is actively maintained, with its latest version (9.6.0) released in January 2026.","status":"active","version":"9.6.0","language":"python","source_language":"en","source_url":"https://github.com/mysql/mysql-connector-python","tags":["database","mysql","sql","driver","orm"],"install":[{"cmd":"pip install mysql-connector-python","lang":"bash","label":"Classic API (recommended)"},{"cmd":"pip install mysqlx-connector-python","lang":"bash","label":"X DevAPI (separate package)"}],"dependencies":[{"reason":"Optional, for DNS SRV record support.","package":"dnspython","optional":true},{"reason":"Optional, for GSSAPI authentication.","package":"gssapi","optional":true}],"imports":[{"wrong":"import mysql.connector","symbol":"connect","correct":"from mysql.connector import connect"}],"quickstart":{"code":"import os\nimport mysql.connector\nfrom mysql.connector import Error\n\nhost = os.environ.get('MYSQL_HOST', 'localhost')\nuser = os.environ.get('MYSQL_USER', 'root')\npassword = os.environ.get('MYSQL_PASSWORD', 'your_password')\ndatabase = os.environ.get('MYSQL_DATABASE', 'test_db')\n\nconn = None\ntry:\n    conn = mysql.connector.connect(\n        host=host,\n        user=user,\n        password=password,\n        database=database\n    )\n\n    if conn.is_connected():\n        print(f\"Connected to MySQL database: {database}\")\n        cursor = conn.cursor()\n\n        # Create a table (DDL - auto-commits)\n        cursor.execute(\"CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), email VARCHAR(255))\")\n        print(\"Table 'users' ensured to exist.\")\n\n        # Insert data (DML - requires commit)\n        sql_insert = \"INSERT INTO users (name, email) VALUES (%s, %s)\"\n        data_to_insert = (\"Alice\", \"alice@example.com\")\n        cursor.execute(sql_insert, data_to_insert)\n        conn.commit() # Important: Commit changes for DML operations\n        print(f\"Inserted: {cursor.rowcount} row(s)\")\n\n        # Select data\n        cursor.execute(\"SELECT id, name, email FROM users\")\n        records = cursor.fetchall()\n        print(\"\\nData from 'users' table:\")\n        for row in records:\n            print(row)\n\n    else:\n        print(\"Failed to connect to MySQL database.\")\n\nexcept Error as e:\n    print(f\"Error connecting to MySQL: {e}\")\nfinally:\n    if conn and conn.is_connected():\n        conn.close()\n        print(\"MySQL connection closed.\")","lang":"python","description":"This quickstart demonstrates how to establish a connection to a MySQL database, create a table (DDL), insert data (DML), and query data using `mysql.connector`. It highlights the importance of `conn.commit()` for data manipulation statements. Database credentials are retrieved from environment variables for security."},"warnings":[{"fix":"Always call `connection.commit()` after `cursor.execute()` for DML statements within a transaction block, or if `autocommit` is not enabled.","message":"Data Manipulation Language (DML) operations (e.g., INSERT, UPDATE, DELETE) are not automatically committed to the database. You must explicitly call `connection.commit()` after executing DML statements for changes to persist. Data Definition Language (DDL) operations (e.g., CREATE TABLE, ALTER TABLE) are auto-committed.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Add `use_pure=True` to your `mysql.connector.connect()` parameters, e.g., `mysql.connector.connect(..., use_pure=True)`.","message":"By default, MySQL Connector/Python attempts to use a C extension for improved performance. If the `libmysqlclient` library is missing or incompatible on your system (e.g., specific Python versions or platforms), this can lead to connection errors or failures. Setting `use_pure=True` in the `connect()` call forces the use of the pure Python implementation.","severity":"gotcha","affected_versions":"Versions 2.1.1 and higher, particularly affecting environments where the C extension is problematic."},{"fix":"For X DevAPI functionality, install `mysqlx-connector-python` via `pip install mysqlx-connector-python` and import from `mysqlx` instead of `mysql.connector`.","message":"X DevAPI support, which was previously part of the `mysql-connector-python` package, was separated into its own distinct package (`mysqlx-connector-python`) starting from version 8.3.0.","severity":"breaking","affected_versions":"8.3.0 and later"},{"fix":"Ensure your MySQL server version is 4.1 or higher. It is recommended to use MySQL Server version 8.0 or higher with the latest Connector/Python versions.","message":"MySQL Connector/Python does not support old MySQL Server authentication methods. This means it will not work with MySQL server versions prior to 4.1.","severity":"gotcha","affected_versions":"All versions of mysql-connector-python"},{"fix":"Verify all connection parameters are correct, check network connectivity, ensure the MySQL server is running and accessible from the client, and review MySQL server logs for connection attempts/failures. Implement robust exception handling and connection pooling for production applications.","message":"Frequent `mysql.connector.errors.OperationalError` often indicates underlying issues such as incorrect database credentials (host, user, password, database), network problems (firewall, unreachable host), or the MySQL server being down or inaccessible.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2026':61 '249':31 '9.6.0':57 'activ':51 'also':33 'api':19,27 'communic':13 'compliant':22 'connector/python':2,4 'contain':9 'databas':26,62 'devapi':40 'document':46 'driver':11,65 'implement':36 'includ':34 'januari':60 'latest':55 'librari':49 'maintain':52 'mysql':1,3,15,45,63 'orm':66 'pep':30 'python':10,25 'releas':58 'self':8 'self-contain':7 'server':16 'specif':28 'sql':64 'store':47 'use':17 'v2.0':29 'version':56 'work':42 'x':39","created_at":"2026-03-28T15:17:53.704675+00:00","updated_at":"2026-04-16T17:18:23.770770+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"9.7.0","cli_name":"mysql-connector-python","cli_version":"sh: 1: mysql-connector-python: not found","type":"library","homepage":"https://dev.mysql.com/doc/connector-python/en/","github":"https://github.com/mysql/mysql-connector-python","docs":"https://dev.mysql.com/doc/connector-python/en/","changelog":"https://dev.mysql.com/doc/relnotes/connector-python/en/","pypi":"https://pypi.org/project/mysql-connector-python/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":"verified"}}