gpt4 book ai didi

python - 无法使用 python 连接到 mysql db - 握手不好

转载 作者:行者123 更新时间:2023-12-03 16:13:53 24 4
gpt4 key购买 nike

我正在尝试使用我的 python 脚本(在本地运行)连接到 mysql 数据库(托管在媒体神庙上),但在运行时收到错误。

错误是:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 179, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Bad handshake

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/Charlie/Documents/python/myscript/mysql_insert.py", line 8, in <module>
port="3306"
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/__init__.py", line 172, in connect
return CMySQLConnection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 78, in __init__
self.connect(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 735, in connect
self._open_connection()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 182, in _open_connection
sqlstate=exc.sqlstate)
mysql.connector.errors.OperationalError: 1043 (08S01): Bad handshake


这是脚本中的代码
import mysql.connector

mydb = mysql.connector.connect(
host="external-db.s157688.gridserver.com",
user="myusername",
passwd="mypassword",
database="mydatabase",
port="3306"
)

mycursor = mydb.cursor()

sql = "INSERT INTO test (post_id, title) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

我试图谷歌它但没有找到任何解决方案,有人可以帮忙吗?

最佳答案

确保您已安装 mysql-connector 而不是 mysql-connector-python,以确保只需运行以下命令:pip3 uninstall mysql-connector-python pip3 install mysql-connector

关于python - 无法使用 python 连接到 mysql db - 握手不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54722251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com