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
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.
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
Open Web SSH
After environment creation, access the Python application-server node through SSH. For example, click the Web SSH button in the dashboard.
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
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:
mysql database.
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!
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
{database} and confirm that the database is available on the target node.pip install mysql-connector-python on the Python application-server node and verify the installation.What’s Next?
- Java Connection to MySQL
- PHP Connection to MySQL
- Node.js Connection to MySQL
- MySQL Primary-Secondary Replication
- MySQL Multi-Primary Replication
