This document provides an introduction to using MariaDB with Python. It discusses installing Python and various Python drivers for MariaDB, including the official MariaDB Connector/Python, PyMySQL, and MariaDB Connector/ODBC. It also covers connecting to MariaDB from Python using a client configuration file for security, running multiple queries across MariaDB hosts with multiprocessing, and performing data aggregation tasks.
3. New to Python?
● Python version 2 or 3?
○ 3!
● Official Python Documentation, Tutorials, FAQ’s, HOWTO’s and more:
○ https://docs.python.org/3
● Composing Programs:
○ http://www.composingprograms.com
● Automate the Boring Stuff with Python:
○ https://automatetheboringstuff.com
4. Install Python
● Official Installer form www.python.org
● Microsoft Windows Store (look for Python 3.7)
● Anaconda
● In your Operating System software repository
11. MariaDB Connector/Python
● A new Connector for Python, for MariaDB by MariaDB!
○ https://github.com/MariaDB/mariadb-connector-python
● Developed against MariaDB Connector/C.
● Open Source, GPL v2.1
● Coming Soon …
○ https://mariadb.com/resources/blog/
12. PyMySQL
● Pure python, no binaries.
● It's in your repo!
○ sudo dnf install python3-pymysql
○ pip3 install pymysql
● No root access? Install into your home:
■ pip3 install --user pymysql
13. MariaDB Connector/ODBC
● Can’t wait? It MUST be from MariaDB?
○ Install MariaDB Connector/ODBC and use PyODBC!
■ sudo dnf install python3-pyodbc
■ sudo dnf install mariadb-connector-odbc
14. DBAPI
This API has been defined to encourage similarity between the Python modules that
are used to access databases. By doing this, we hope to achieve a consistency
leading to more easily understood modules, code that is generally more portable
across databases, and a broader reach of database connectivity from Python.
https://www.python.org/dev/peps/pep-0249/
16. Import Shopping List
● Standard Library (https://docs.python.org/3/library/index.html)
○ configparser
○ os
○ sys
○ argparse
○ multiprocessing
○ datetime and time
○ sqlite3
● Third Party (https://pymysql.readthedocs.io/en/latest/)
○ pymysql
17. Connecting with the Client Config
● Read Connection details from a client config
○ The [client] section in ~/.my.cnf
● No plain text passwords in process list!
● No plain text passwords in version control!