Connecting a Database to an Azure Virtual Machine and Retrieving Data via Public IP

In this blog, I will provide step-by-step instructions on how to establish a connection to the database on your Azure Virtual Machine

Step 1: Create a Virtual Machine with Your Preferred Operating System (OS) and Assign it to a Specific Resource Group and Availability Zone...

Step2: Connect to the VM Using its Public IP

Step 3: Grant Root Privileges with 'sudo su'

Step 4: Update Packages with 'apt update'

Step 5:Install MySQL Server

Step 6: Allowing MySQL Connections Through the Firewall

The command sudo ufw allow 3306 is used to allow incoming traffic on port 3306, which is the default port for MySQL. This command opens up the firewall to allow MySQL connections.

Step 7:Connect to MySQL

Step 8: Create a database

Step 9:Create a table

Step 10:Insert values into the table

Step 11:view the table

Step 12:Install Python, Flask, and MySQL Client

Step 13:Install mysql-python-connector

Step 14: Create a project directory, navigate to it, and create a subdirectory for 'index.html' along with an 'app.py' file.

Step 15:Create a user in mysql and grant priveleges to the user and reload the priveleges.

Step 16:write this code in app.py file using your database credentials

import mysql.connector from flask import Flask, render_template

app = Flask(name)

#Define your database credentials

db_config = { "host": "localhost", "user": "lucky", "password": "12345", # Replace with your MySQL password "database": "db", # Replace with your database name }

try: # Connect to the MySQL server conn = mysql.connector.connect(**db_config) cursor = conn.cursor()

# Execute a query to select all rows from your table query = "SELECT * FROM student_results" # Replace "student_results" with your table name cursor.execute(query)

# Fetch all the rows from the table rows = cursor.fetchall()

# Get the column names column_names = [i[0] for i in cursor.description]

# Close the cursor and connection cursor.close() conn.close()

cursor.close() conn.close()

except mysql.connector.Error as e: # Handle the exception and print an error message print(f"MySQL Error: {e}")

@app.route('/') def index(): return render_template('index.html', column_names=column_names, rows=rows)

if name == 'main': app.run(host="0.0.0.0", port=80)

Step 17:Write this code in index.html file

Step 18:Now go to project directory and run this command

python3 app.py

Step 19:Open a web browser on your local machine.In the address bar, enter the public IP address of your Azure Virtual Machine (VM).Press Enter or click Go in your browser.This URL will take you to your Flask application, and you should see the data from your MySQL database displayed in the browser. Make sure that your Azure VM's network and firewall settings allow incoming traffic on port 80, and that your Flask application is running as expected.

Step 20: Success!

you have successfully set up and accessed your Flask application that retrieves data from a MySQL database on your Azure Virtual Machine (VM).