使用python连接到局域网数据库

2024-06-16 10:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地在我的两台电脑上安装了xampp。当我在pc1上通过python连接到pc1的本地主机时,一切正常。 但当我试图连接到pc2xamppsql时,我无法获得连接。 我已经将xampp配置为接受来自局域网中其他设备的连接,我可以通过我的web浏览器完美地访问它,但不能从python程序访问它。在

这是我的代码:

def GetResults(self):
        try:
            cnx = mysql.connector.connect(user='test', password= '123456', host='paraliass-pc', port='3306', database='test')
            cursor = cnx.cursor()
        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                print("Something is wrong with your username or password")
                self.statusBar().showMessage("Wrong Username or Password")
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                print("Database does not exist")
                self.statusBar().showMessage("Database does not exist")

        if cnx :               
            print("Connected")
            self.statusBar().showMessage("Connected")

        cursor = cnx.cursor()
        cursor.execute("SELECT * FROM STAFF WHERE NAME LIKE '" + self.textEdit.text() + "%' OR SURNAME LIKE '" + self.textEdit.text() + "%'" + "OR RANK LIKE '" + self.textEdit.text() + "%'")

当我连接到运行该程序的同一台电脑时,我得到了这个错误

^{pr2}$

顺便说一句,如果我故意将本地主机上的用户名或密码更改为错误的,我会得到相同的错误。在


Tags: textself程序错误cursorlikeerrprint
1条回答
网友
1楼 · 发布于 2024-06-16 10:27:09

您应该考虑授予远程连接到MySQL的权限,并绑定MySQL使用您选择的IP地址和端口。默认情况下,MySQL只监听通过localhost/socket的连接。在

相关问题 更多 >