簡體   English   中英

如何在不使用密碼的情況下連接MySQL? (PyMySQL)

[英]How do I connect to MySQL without using a password? (PyMySQL)

這是我的代碼的相關部分:

        try:
            if self.pass_entry.get():
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    password=self.pass_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
            else:
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
        except Exception as e:
            self.console.insert(tk.END, "Error: {err}\n".format(err=str(e)))

這是錯誤:

Connecting to 127.0.0.1...
Error: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

這里“root”上沒有密碼, PyMySQL假設它有一個密碼。 如何在不使用密碼的情況下連接? (當密碼字段/字符串為空時,我試圖省略密碼選項,但PyMySQL不考慮它)。

當沒有數據庫密碼時,您需要將“”作為密碼傳遞給該函數。

在你的其他地方嘗試這個:

        else:
            self.connection = pymysql.connect(
                host=self.host_entry.get(),
                user=self.user_entry.get(),
                password="",
                db=self.db_entry.get(),
                charset="utf8mb4",
                cursorclass=pymysql.cursors.DictCursor
            )

使用mysql.connector庫時遇到了同樣的問題。 通過簡單地創建除root之外的新連接用戶來解決它。

谷歌搜索了一段時間后,似乎訪問root可能是問題所在,但在找到上述解決方案之后,我放棄了進一步研究。 用戶在MySQL中具有與root @ localhost相同的設置。

def mySQLcnx(someVariable):
        dbCnx = mysql.connector.connect(host="localhost", user="pythonCnx", database="someDatabase")
        dbCnxCursor = dbCnx.cursor()
        dbCnxCursor.execute(someStatement)
        DbQueryResults = dbCnxCursor.fetchall()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM