Python Application Connection to MySQL/MariaDB/Percona
MySQL, MariaDB, and Percona are highly popular among developers all over the world, when an open source SQL databases are required. In this instruction we’ll show you how to connect your Python application, hosted within the platform, to these DB servers.
1. Log into the platform dashboard and create a new environment with both Python and MySQL (or Python and MariaDB) servers.
2. After environment creation, access your application server via SSH Gate, e.g. by pressing the Web SSH button.
A terminal emulator with the automatically established SSH connection to your node will be opened in the appropriate tab.
3. Now, install a MySQL connector for Python (it works with MariaDB as well) with the following command:
|
|
4. Next, let’s create a simple Python script to establish database connection. You can use any preferable text editor for this task, as well as any filename with the .py extension (e.g. vim script.py).
|
|
Here, you need to adjust the connection string (all the required information is provided within email for your MySQL / MariaDB node):
- {user} - username to log into database with
- {password} - password for the appropriate user
- {host} - link to your MySQL / MariaDB container
- {database} - database to be accessed (e.g. the default mysql one)
This script will connect to the specified database server with the provided credentials and will print connection errors (if any) or just a “You are connected!” phrase.
5. So, let’s execute our code with the appropriate command:
|
|
If the “You are connected!” string appeared within terminal, the connection was successful. Now, you can be sure that your database server is accessible and you can extend the code to execute the required actions.