简体   繁体   English

如何在不使用密码的情况下连接MySQL? (PyMySQL)

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

Here's the relevant part of my code: 这是我的代码的相关部分:

        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)))

Here's the error: 这是错误:

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

There's no password on "root" here, and PyMySQL is supposing that it has one. 这里“root”上没有密码, PyMySQL假设它有一个密码。 How can I connect without using a password? 如何在不使用密码的情况下连接? (I've tried to omit the password option when the password field / string is empty, but PyMySQL doesn't take it into account). (当密码字段/字符串为空时,我试图省略密码选项,但PyMySQL不考虑它)。

When there is no password for the database you need to pass "" to the function as password. 当没有数据库密码时,您需要将“”作为密码传递给该函数。

Try this in your else: 在你的其他地方尝试这个:

        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
            )

I had the same problem while using mysql.connector library. 使用mysql.connector库时遇到了同样的问题。 Got around it by simply creating a new connection-user in addition to root. 通过简单地创建除root之外的新连接用户来解决它。

After googling for a while it seemed like the access to root could be the problem, but I dropped looking further into it after I found the mentioned solution. 谷歌搜索了一段时间后,似乎访问root可能是问题所在,但在找到上述解决方案之后,我放弃了进一步研究。 The user has the same settings in MySQL as root@localhost. 用户在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