Access MariaDB (MySQL) from Python code

On the host machine, it is required to install the driver to connect to the database from the Python code.

On Ubuntu or Debian-based Linux systems:

apt-get install python-mysql.connector

On Redhat or CentOS Linux systems:

yum install python-mysql.connector

Connect to the database server using the command line client, then  the database to connect to:

CREATE DATABASE flask DEFAULT CHARACTER SET utf8 DEFAULT collate utf8_bin;

GRANT ALL PRIVILEGES ON flask.* to flaskuser@'%' IDENTIFIED BY 'flaskuser';
GRANT ALL PRIVILEGES ON flask.* to flaskuser@'localhost' IDENTIFIED BY 'flaskuser';

SET PASSWORD FOR 'flaskuser'@'localhost' = PASSWORD('<PASSWORD>!');

Finally, from the Python script, import the connector, connect to the database, and start querying the records:

#!/usr/bin/env python

import mysql.connector as mariadb

mariadb_connection = mariadb.connect(user='flaskuser', password='<PASSWORD>', database='flask')
cursor = mariadb_connection.cursor()