I am trying to create an app for my clients. I am using python, flask and xampp - mysql database, flask_mysqldb library for it. I did import MySQL.
- First client is going to register to app.
- Then app is going to save client data to maindb
- After that new database will be created and a new database user for it
- That user is going to get all privilleges for the database just created
Mysql saves clients data on maindb, creates user but can not grant privilleges to that client.
When i use my app on localhost i get this error. MySQLdb.OperationalError: (2006, '')
But this codes were working perfectly before i get some errors about xampp. I handeled those errors. I tryed my app again and it is not working like before.
Now i want to share my codes. I got 3 files about this process.
- main.py file
@gymsw.route("/register", methods = ["GET", "POST"])
def register():
if request.method == "POST":
return registeration(db)
else:
return render_template("register.html")
- database.py file
class DataBase:
def __init__(self, app):
self.app = app
self.mysql = MySQL(self.app)
def configure_db(self):
self.app.config["MYSQL_HOST"] = "localhost"
self.app.config["MYSQL_USER"] = "root"
self.app.config["MYSQL_PASSWORD"] = ""
self.app.config["MYSQL_CURSORCLASS"] = "DictCursor"
self.app.config["MYSQL_DB"] = "maindb"
self.mysql.init_app(self.app)
def create_user(self, dbAdi, dbUser, dbPassword):
self.cursor = self.mysql.connection.cursor()
sorguForUser = f"CREATE USER '{dbUser}'@'localhost' IDENTIFIED BY '{dbPassword}'"
sorguForPrivileges = f"GRANT ALL PRIVILEGES ON {dbAdi}.* TO '{dbUser}'@'localhost'"
self.cursor.execute(sorguForUser)
self.mysql.connection.commit()
self.cursor.execute(sorguForPrivileges)
self.mysql.connection.commit()
self.cursor.close()
- utility.py file
def registeration(db):
adSoyad = request.form.get("adSoyad")
ePosta = request.form.get("ePosta")
telefon = request.form.get("telefon")
password = request.form.get("password")
kulupAdi = request.form.get("kulup")
aktivasyon = True
dbAdi = createDbName(kulupAdi)
dbUser = createDbUsername(ePosta)
dbPassword = "mkrdmn"
#Kullanıcıyı maindbusers veritabanına kayıt edelim.
sorgu = "Insert Into users (adSoyad, ePosta, telefon, password, kulupAdi, aktivasyon, dbAdi, dbUser, dbPassword) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (adSoyad, ePosta, telefon, password, kulupAdi, aktivasyon, dbAdi, dbUser, dbPassword)
db.exec_db(sorgu, values)
db.create_db(dbAdi)
db.create_user(dbAdi, dbUser, dbPassword)
db.change_db(dbAdi)
db.create_ogrenciler()
db.create_gruplar()
#kullanıcının okul adına göre veri tabanını oluşturalım.
flash("Başarıyla Kayıt Oldunuz", "success")
return redirect(url_for("index"))
When i get the error that i mentioned about it shows me the lines below. And they are connected ofcourse
- this line in main.py
return registeration(db)
2)this line in utility.py
db.create_user(dbAdi, dbUser, dbPassword)
- this line in database.py
self.cursor.execute(sorguForPrivileges)
I think the source of this error is step 3. But i do not know why. Because it was working perfectly before i reinstalled xampp. Thanks for the people who can help me even.
Same code was working perfectly before i reinstalled the xampp. I reinstalled it because with mssql server i can not run mysql on xampp. I checked online, and they said some files were broken. Copy from mysql folder and paste those files to data folder in xampp. I did it but this time mysql runs, but my databases were not working. So I reinstalled it and changed mysql port to 16000 so xampp will be the only application which uses port 3306. After all this, I got this issue.
Error Mesages
on browser - MySQLdb.OperationalError: (2006, '')
on visual stuido Code
MySQLdb.OperationalError: (2006, '') 127.0.0.1 - - [17/Dec/2023 17:57:55] "GET /register?debugger=yes&cmd=resource&f=style.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Dec/2023 17:57:55] "GET /register?debugger=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Dec/2023 17:57:55] "GET /register?debugger=yes&cmd=resource&f=console.png HTTP/1.1" 304 -
127.0.0.1 - - [17/Dec/2023 17:57:55] "GET /register?debugger=yes&cmd=resource&f=console.png HTTP/1.1" 304 -
I think i fixed the issue. I wanna share it so anyone who has the same problem can see. I commented a line
(self.mysql.connection.commit)and it worked.self.cursor.execute(sorguForUser) #self.mysql.connection.commit() (this is the line i make comment) self.cursor.execute(sorguForPrivileges)self.mysql.connection.commit()– mkrdmn