Connecting to MariaDB

Anaconda Enterprise enables you to easily connect to MariaDB, a fork of MySQL relational database management system (RDBMS).

Before you can do so, however, you’ll need to install the mysql-connector-python conda package, which contains the Python driver for communicating with MySQL servers:

conda install -c anaconda mysql-connector-python

NOTE: Any packages you install from the command line are available during the current session only. If you want them to persist, add them to the project’s anaconda-project.yml file. For more information, see Developing a project.

You can then use code such as this to connect to MySQL from within a notebook session:

import mysql.connector as mariadb
import json


# Read the credentials from secret
credentials = None
with open('/var/run/secrets/user_credentials/mariadb_credentials') as f:
    credentials = json.load(f)

# Ensure your credentials were setup
if credentials:
    # Connect to the DB
    connection = mariadb.connect(
        user=credentials.get('user'),
        password=credentials.get('password'),
        database='credentials.get('database'),
        host='credentials.get('host')'
    )
    cursor = connection.cursor()

    # Execute the query
    cursor.execute("SELECT first_name, last_name FROM employees LIMIT 20")

    # Loop through the results
    for first_name, last_name in cursor:
        print(f'First name: {first_name}, Last name: {last_name}')

    # Close the connection
    connection.close()

See Storing secrets for information about adding credentials to the platform, to make them available in your projects. Any secrets you add will be available across all sessions and deployments associated with your user account.