使用Python连接MySQL - MySQL Connector出现问题

3
我有Python 2.7版本,并下载了MySQL 8.0.13版本。当我尝试从链接安装正确的连接器并运行以下代码时:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="Pass"
)

print(mydb)

模块未被识别:

Traceback (most recent call last):
  File "C:\Users\carmel.han\Desktop\mysqltest.py", line 1, in <module>
    import mysql.connector
ImportError: No module named mysql.connector

当我使用pip安装SQL Connector并运行相同的代码时,会出现以下情况:
Traceback (most recent call last):
  File "C:\Users\carmel.han\Desktop\mysqltest.py", line 7, in <module>
    auth_plugin='mysql_native_password'
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect
    self._open_connection()
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\connection.py", line 210, in _open_connection
    self._ssl)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\connection.py", line 144, in _do_auth
    self._auth_switch_request(username, password)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\connection.py", line 167, in _auth_switch_request
    auth = get_auth_plugin(new_auth_plugin)(
  File "C:\Python27\ArcGIS10.4\lib\site-packages\mysql\connector\authentication.py", line 191, in get_auth_plugin
    "Authentication plugin '{0}' is not supported".format(plugin_name))
NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported

我从pip 7.0.1升级到pip 18.1,卸载并重新安装了SQL-connector,但结果相同。即使我插入了这一行:

auth_plugin='mysql_native_password'

基于这篇文章,我猜可能是SQL连接器版本的问题。我搜索了一下,发现可以安装特定版本的SQL连接器,但我不知道需要哪个版本。 pip默认安装了2.1.6,我也尝试了2.1.4,但没有成功。我使用的是Windows 10 64位操作系统。
有人有想法吗?

可能是Authentication plugin 'caching_sha2_password' is not supported的重复问题。 - snakecharmerb
1个回答

0

首先,您需要从以下命令安装mysql-connector。

python -m pip install mysql-connector

然后你就可以运行你的程序了。


这是我用pip安装mysql-connector的命令,但它不起作用。然后我得到了这个错误: “NotSupportedError:不支持认证插件'caching_sha2_password'” - Carmel Han
请参考此链接:[https://dev59.com/aVUL5IYBdhLWcg3wSmco] - vipul prajapati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接