https://github.com/mysql/mysql-connector-python
MySQL Connector/Python is implementing the MySQL Client/Server protocol completely in Python. No MySQL libraries are needed, and no compilation is necessary to run this Python DB API v2.0 compliant driver. Documentation & Download: http://dev.mysql.com/doc/connector-python/en
https://github.com/mysql/mysql-connector-python
Last synced: 7 months ago
JSON representation
MySQL Connector/Python is implementing the MySQL Client/Server protocol completely in Python. No MySQL libraries are needed, and no compilation is necessary to run this Python DB API v2.0 compliant driver. Documentation & Download: http://dev.mysql.com/doc/connector-python/en
- Host: GitHub
- URL: https://github.com/mysql/mysql-connector-python
- Owner: mysql
- License: other
- Created: 2014-08-01T09:11:26.000Z (over 11 years ago)
- Default Branch: trunk
- Last Pushed: 2025-04-15T13:04:09.000Z (8 months ago)
- Last Synced: 2025-05-03T03:10:46.803Z (7 months ago)
- Language: Python
- Homepage:
- Size: 22.1 MB
- Stars: 906
- Watchers: 66
- Forks: 297
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.txt
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-data-analysis - MySQL Connector/Python - MySQL driver for Python. (🗃️ SQL & Databases / Tools)
- awesome-mysql - MySQL Connector/Python - a standardized database driver for Python platforms and development. (Connectors)
README
MySQL Connector/Python
======================
.. image:: https://img.shields.io/pypi/v/mysql-connector-python.svg
:target: https://pypi.org/project/mysql-connector-python/
.. image:: https://img.shields.io/pypi/pyversions/mysql-connector-python.svg
:target: https://pypi.org/project/mysql-connector-python/
.. image:: https://img.shields.io/pypi/l/mysql-connector-python.svg
:target: https://pypi.org/project/mysql-connector-python/
.. === ====
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) `_ - We refer to it as the
`Classic API `_.
.. === ====
.. === [repl("It also", "MySQL Connector/Python")] ===
It also contains an implementation of the `XDevAPI `_
- An Application Programming Interface for working with the `MySQL Document Store
`_.
.. === ===
.. === [repl("* `XDevAPI `_", "")] ====
Features
--------
* `Asynchronous Connectivity `_
* `C-extension `_
* `Telemetry `_
* `XDevAPI `_
.. === ====
Installation
------------
Connector/Python contains the classic and XDevAPI 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:
* `RPM `_
* `WHEEL `_
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 `_.
.. === [repl("The *classic API* can be installed via pip as follows:", "")] ===
The *classic API* can be installed via pip as follows:
.. code-block:: bash
$ pip install mysql-connector-python
.. === ====
.. === [repl("similarly, the *XDevAPI* can be installed with:", "")] ===
similarly, the *XDevAPI* can be installed with:
.. code-block:: bash
$ pip install mysqlx-connector-python
Please refer to the `installation tutorial `_
for installation alternatives of the XDevAPI.
.. === ===
++++++++++++++++++++
Installation Options
++++++++++++++++++++
Connector packages included in MySQL Connector/Python allow you to install
optional dependencies to unleash certain functionalities.
.. === ===
.. code-block:: bash
# 3rd party packages to unleash the telemetry functionality are installed
$ pip install mysql-connector-python[telemetry]
.. === ===
.. === [repl("similarly, for the XDevAPI:", "")] ===
similarly, for the XDevAPI:
.. code-block:: bash
# 3rd party packages to unleash the compression functionality are installed
$ pip install mysqlx-connector-python[compression]
.. === ===
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.
.. === [repl("Options for the Classic connector:", "Available options:")] ===
Options for the Classic connector:
* dns-srv
* gssapi
* fido2
* telemetry
.. === ===
.. === [repl("Options for the XDevAPI connector:", "Available options:")] ===
Options for the XDevAPI connector:
* dns-srv
* compression
.. === ===
.. === [repl("Classic", "Sample Code"), repl("-------", "-----------")] ===
Classic
-------
.. code:: python
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()
.. === ===
.. === [repl("XDevAPI", "Sample Code"), repl("-------", "-----------")] ===
XDevAPI
-------
.. code:: python
import mysqlx
# Connect to server
session = mysqlx.get_session(
host="127.0.0.1",
port=33060,
user="mike",
password="s3cr3t!")
schema = session.get_schema("test")
# Use the collection "my_collection"
collection = schema.get_collection("my_collection")
# Specify which document to find with Collection.find()
result = collection.find("name like :param") \
.bind("param", "S%") \
.limit(1) \
.execute()
# Print document
docs = result.fetch_all()
print(r"Name: {0}".format(docs[0]["name"]))
# Close session
session.close()
.. === ===
.. === [repl-mysql("- `MySQL Connector/Python X DevAPI Reference `_", ""), repl-mysqlx("- `MySQL Connector/Python Developer Guide `_", "")] ===
Additional Resources
--------------------
- `MySQL Connector/Python Developer Guide `_
- `MySQL Connector/Python X DevAPI Reference `_
- `MySQL Connector/Python Forum `_
- `MySQL Public Bug Tracker `_
- `Slack `_ (`Sign-up `_ required if you do not have an Oracle account)
- `Stack Overflow `_
- `Oracle Blogs `_
.. === ===
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.