Python Application Connection to MySQL/MariaDB/Percona
When an open-source SQL database is needed, developers worldwide are quite fond of MySQL, MariaDB, and Percona. We’ll walk you through connecting your Python application, hosted on the platform, to MySQL DB servers in this instruction.
Step 1. Access the platform dashboard and establish a new environment with MySQL (or MariaDB) and Python servers.
Step 2. Once the environment has been created, use SSH Gate to access your application server, for example, by clicking the Web SSH button.
The relevant tab will open with a terminal emulator that has the SSH connection to your node established automatically.
Step 3. Next, use the following command to install a MySQL connector for Python (it also functions with MariaDB):
pip install mysql-connector==2.2.9
Step 4. Next, let’s create a simple Python script to establish a 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 the 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.
Step 5. So, let’s execute our code with the appropriate command
python script.py
If the message “You are connected!” appears within the terminal, it confirms a successful connection. This indicates that your database server is accessible, allowing you to proceed with implementing the necessary actions in your code.
Conclusion
In this article, connect a Python application to MySQL on AccuWeb.Cloud provides a streamlined and reliable solution for database management. Leveraging Python’s simplicity and MySQL‘s robustness, developers can seamlessly integrate database functionality into their projects. With scalability and ease of deployment, managing Python applications becomes efficient and hassle-free. This powerful combination empowers developers to build dynamic, data-driven applications with confidence, driving innovation and efficiency in their projects.




