Home > Software engineering >  Python securely connect to MariaDB database
Python securely connect to MariaDB database

Time:12-31

I am trying to read the database of my MariaDB server. I have set it up like this:

CREATE DATABASE database1;
CREATE USER RaspberryPi@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database1.* TO RaspberryPi@'%';
SELECT database1;
CREATE TABLE Users;

This is on my Raspberry Pi 4 with the IP: 192.168.0.92. Now I have This Python script on my Windows computer:

import mysql.connector
mydb = mysql.connector.connect(host="192.168.0.92", user="RaspberryPi", passwd="password", database="database1")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Users")
print(mycursor)

Now I convert my Python script to an .exe file using pyinstaller. My problem is, that if I give this file to some other people, he can easily convert the .exe file to his original .py file and then he has my login credentials. Can I code it somehow, that the username and password isn't shown or the script can't be converted back?

Thanks.

CodePudding user response:

you can use this snipet:

Module Imports

import mariadb
import sys

# Connect to MariaDB Platform
try:
    conn = mariadb.connect(
        user="db_user",
        password="db_user_passwd",
        host="192.0.2.1",
        port=3306,
        database="employees"

    )
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)

# Get Cursor
cur = conn.cursor()

for more reference use this - https://mariadb.com/resources/blog/how-to-connect-python-programs-to-mariadb/

Update: in order to make it more secure you can put the username and password with get input

  • Related