MySQL Connector/Python

repository·trunk·Indexed 21 days ago

https://github.com/mysql/mysql-connector-python

A Python driver for MySQL providing a PEP 249 compliant Classic API and an X DevAPI for Document Store/NoSQL workloads. It includes support for MySQL HeatWave's AI and Machine Learning capabilities, a Django database backend client, and utilities for script splitting, comment removal, and custom asynchronous authentication plugins including WebAuthn.

Tokens
31K
Snippets
102
Records
151
Agent score
75%

What's inside mysql-connector-python

  1. Overview of MySQL Connector/Python

    trunk

    MySQL Connector/Python is Oracle's official Python driver for MySQL databases. It provides two primary ways to interact with MySQL:

    1. Python Database API Specification v2.0 (PEP 249) compliant API: A standard interface for traditional SQL-based database access.
    2. X DevAPI implementation: A modern API designed for NoSQL-style document store operations and advanced MySQL features.

    Use the PEP 249 API for standard relational database operations and the X DevAPI for document-based workflows.

  2. Use MySQL Connector/Python X DevAPI

    trunk

    MySQL Connector/Python provides a client library for the X DevAPI protocol (introduced in MySQL 8.0). This API allows developers to interact with MySQL 8.0 using both relational tables and JSON documents, enabling a hybrid approach where you can work with tables and collections simultaneously.

    For deep conceptual details on the X DevAPI protocol itself, refer to the official MySQL X DevAPI User Guide at http://dev.mysql.com/doc/x-devapi-userguide/en/.

  3. Set host priority in Connection Routers

    trunk

    You can control the order of connection attempts by assigning a priority to each host.

    • Range: Valid values are 1 to 100, where 100 is the highest priority.
    • Requirement: If you specify a priority for one host in a router group, you must provide a priority value for all hosts in that group.

    Priority can be set in the URI-like string or within the routers list in the connection dictionary.

    import mysqlx
    
    # Example using URI-like string with priority
    connection_str = 'mysqlx://root:@[(address=unreachable_host, priority=100),(address=127.0.0.1:33060, priority=90)]?connect-timeout=2000'
    options_string = '{}'
    client = mysqlx.get_client(connection_str, options_string)
    
    # Example using connection dictionary with priority
    routers = [
        {"host": "unreachable_host", "priority": 100},
        {"host": "127.0.0.1", "port": 33060, "priority": 90}
    ]
    connection_dict = {
        'routers': routers,
        'port': 33060,
        'user': 'mike',
        'password': 's3cr3t!',
        'connect_timeout': 2000
    }
    client = mysqlx.get_client(connection_dict, '{}')
  4. Use NOWAIT and SKIP_LOCKED to handle lock contention

    trunk

    By default, if a transaction requests a row that is already locked, it will block and wait until the lock is released. You can change this behavior using mysqlx.LockContention options passed to the locking methods:

    • mysqlx.LockContention.NOWAIT: The query executes immediately. If the requested row is locked, the operation fails with an error instead of waiting.
    • mysqlx.LockContention.SKIP_LOCKED: The query executes immediately. Instead of failing or waiting, it simply excludes any locked rows from the result set.
    # Using NOWAIT to fail immediately if locked
    collection.find("_id = :id").lock_shared(mysqlx.LockContention.NOWAIT).bind("id", "1").execute()
    
    # Using SKIP_LOCKED to ignore locked rows
    collection.find("_id = :id").lock_exclusive(mysqlx.LockContention.SKIP_LOCKED).bind("id", "1").execute()
  5. Integrate with MySQL HeatWave GenAI and Machine Learning

    trunk

    The mysql.ai module provides an optional API for integrating with MySQL HeatWave's AI and Machine Learning capabilities.

    Note: This requires manual installation of dependencies: langchain, pandas, and scikit-learn.

    GenAI

    Provides LangChain-compatible implementations:

    • MyLLM
    • MyVectorStore
    • MyEmbeddings

    AutoML

    Provides Scikit-Learn compatible estimators:

    • MyClassifier
    • MyRegressor
    • MyAnomalyDetector
    • MyGenericTransformer
  6. Use Connection Routers for failover

    trunk

    Connection Routers allow you to connect to multiple hosts using connection failover. If the primary endpoint is unavailable, the connector automatically attempts to connect to the next available endpoint before raising an error.

    To use this technique, you can either provide a URI-like string or a routers list in the connection settings when calling mysqlx.get_client().

    Important Considerations:

    • Credentials: The MySQL user and password provided apply to all endpoints; the same account must exist on every host.
    • Timeout: Because the connector may attempt to connect to all hosts before failing, it is highly recommended to set the connect_timeout option (as a positive integer) to avoid long delays when multiple hosts are down.
    • Selection Order: If no priority is specified, endpoints are chosen randomly. If priority is used, the host with the highest value is prioritized.
    import mysqlx
    
    # Using a URI-like string with multiple hosts
    connection_str = 'mysqlx://root:@[(address=unreachable_host),(address=127.0.0.1:33060)]?connect-timeout=2000'
    options_string = '{}'
    
    client = mysqlx.get_client(connection_str, options_string)
    session = client.get_session()
    
    session.close()
    client.close()
  7. How connection pooling works with X Protocol

    trunk

    Connection pooling in Connector/Python with the X Protocol manages a pool of ready-to-use connections to reduce connection creation time and improve application performance.

    When you use mysqlx.get_client(), you create a client that manages a pool. Every session obtained via client.get_session() uses a pooled connection. When you call session.close(), the connection is not destroyed but is instead returned to the pool to be reused by other requests or threads.

    import mysqlx
    
    connection_str = 'mysqlx://mike:s3cr3t!@localhost:33060'
    options_string = '{}'
    
    client = mysqlx.get_client(connection_str, options_string)
    session = client.get_session()
    
    # ... perform work ...
    
    session.close()  # Returns connection to the pool
    client.close()   # Closes the client and the pool
  8. How connection attributes work in MySQL X DevAPI

    trunk

    MySQL server tracks operational details for every connected client using connection attributes. These attributes fall into two categories:

    1. System-defined attributes: Automatically set by the X DevAPI (e.g., _client_name, _client_version, _os). These always start with an underscore (_).
    2. User-specified attributes: Custom metadata provided by the client.

    Constraint: User-defined connection attributes must not start with an underscore (_), as that prefix is reserved for system-defined attributes.

  9. How shared and exclusive locks work in X DevAPI

    trunk

    The X DevAPI supports row-level locking for mysqlx.Collection.find() and mysqlx.Table.select() methods. This enables safe, transactional updates to documents or rows.

    There are two primary lock types:

    • Shared Locks: Use mysqlx.ReadStatement.lock_shared() to permit the transaction holding the lock to read a row. Multiple transactions can hold shared locks on the same row simultaneously.
    • Exclusive Locks: Use mysqlx.ReadStatement.lock_exclusive() to permit the transaction holding the lock to update or delete a row. An exclusive lock prevents other transactions from acquiring any lock (shared or exclusive) on that row until the lock is released.
    # Example of acquiring a shared lock
    session.start_transaction()
    collection.find("_id = '1'").lock_shared().execute()
    
    # Example of acquiring an exclusive lock
    session.start_transaction()
    collection.find("_id = '1'").lock_exclusive().execute()
  10. Create collections in a schema

    trunk

    In the X DevAPI, documents of the same type are grouped into Collection objects. You can create a new collection using the mysqlx.Schema.create_collection() method on a mysqlx.Schema object.

    To prevent errors if a collection with the same name already exists, set the reuse_existing argument to True.

    import mysqlx
    
    # Connect to server on localhost
    session = mysqlx.get_session({
        'host': 'localhost',
        'port': 33060,
        'user': 'mike',
        'password': 's3cr3t!'
    })
    
    schema = session.get_schema('test')
    
    # Create 'my_collection' in schema
    schema.create_collection('my_collection', reuse_existing=True)
  11. Set user-specified connection attributes via connection URL

    trunk

    You can pass custom connection attributes when establishing a session by appending them to the connection URL using the connection-attributes key. The attributes should be formatted as a comma-separated list of key=value pairs within square brackets.

    import mysqlx
    
    # Using a connection URL with custom attributes
    # Format: mysqlx://user@host:port/schema?connection-attributes=[key1=value1,key2=value2]
    mysqlx.getSession('mysqlx://mike@localhost:33060/schema?connection-attributes=[my_attribute=some_value,foo=bar]')
  12. Resolve DNS SRV records for service discovery

    trunk

    If your environment uses DNS SRV records for service discovery, you can use the mysqlx+srv scheme or the dns-srv option. This allows the connector to automatically resolve available server addresses.

    Requirement: This requires the dnspython module to be installed.

    import mysqlx
    
    # Using the connection scheme
    session = mysqlx.get_session('mysqlx://root:@foo.abc.com')
    
    # Using the dns-srv option
    session = mysqlx.get_session({
        'host': 'foo.abc.com',
        'user': 'root',
        'password': '',
        'dns-srv': True
    })