Python Connection

Python Connection to MySQL, MariaDB and Percona

Connect a Python application hosted on the platform to MySQL, MariaDB, or Percona by installing the official MySQL Connector for Python and using the database credentials supplied for the database node.

Create the Environment

1

Create a Python and database environment

Log in to the platform dashboard and create a new environment containing an Apache Python or Python Engine application server together with a MariaDB, MySQL, or Percona database.

Python MariaDB environment
Create an Apache Python or Python Engine environment with a MariaDB, MySQL, or Percona database.

Separate environments are also supported

The Python server and database do not have to be in the same environment. The same connection approach can be used when they are deployed in separate environments.

Connect to the Python Node via SSH

2

Open Web SSH

After environment creation, access the Python application-server node through SSH. For example, click the Web SSH button in the dashboard.

Python Web SSH button
Open a terminal session to the Python application-server node.

Install MySQL Connector for Python

Install the MySQL Connector for Python package. The same connector can also be used with MariaDB.

pip install mysql-connector-python
Install MySQL Connector for Python
Install the database connector from the Python node terminal.

Create a Database Connection Test Script

Create a Python file with any .py filename, for example:

vim test-db-connection.py

Add the following connection logic:

import mysql.connector
from mysql.connector import errorcode

try:
    cnx = mysql.connector.connect(
        user='{user}',
        password='{password}',
        host='{host}',
        database='{database}'
    )
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)
else:
    print("You are connected!")
    cnx.close()

Replace the placeholders with the database-node values:

{user}Database username.
{password}Password for the database user.
{host}MySQL, MariaDB, or Percona container hostname.
{database}Target database name, for example the default mysql database.
Python database connection code
Insert the database credentials and hostname into the test script.

Run and Verify the Connection

Run the test script from the Python node:

python test-db-connection.py

If the database connection succeeds, the terminal prints:

You are connected!
Python database connection test
A successful terminal message confirms the Python-to-database connection.

After successful testing, extend the script with the SQL queries and other database operations required by the application.

Expected Result

The Python application-server node has the MySQL Connector for Python installed and can connect successfully to the selected MySQL, MariaDB, or Percona database using the supplied hostname, user credentials, and database name.

Important Notes

  • The documented connector package is mysql-connector-python.
  • The connector works with MariaDB as well as MySQL.
  • The Python and database nodes can be in the same or separate environments.
  • Use the database container hostname supplied for the database node.
  • The sample script handles incorrect credentials and an invalid database name separately.
  • Close the database connection after a successful test.

Common Issues and Solutions

Access deniedCheck the database username and password used in the Python script.
Database does not existVerify the value supplied for {database} and confirm that the database is available on the target node.
Connector cannot be importedRun pip install mysql-connector-python on the Python application-server node and verify the installation.
Host cannot be reachedCheck the MySQL, MariaDB, or Percona container hostname and database-node availability.
No success message appearsRead the error printed by the sample script and verify all four connection parameters.

What’s Next?

  • Java Connection to MySQL
  • PHP Connection to MySQL
  • Node.js Connection to MySQL
  • MySQL Primary-Secondary Replication
  • MySQL Multi-Primary Replication