繁体   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