安装 MySQL 连接器有两种方法。优选第二种方式。
1. MySQL 安装程序 https://www.mysql.com/downloads/
这是Oracle 的产品安装程序。问题是它已经过时了。它只支持 Python 3.6 版本,没有更新的版本。要安装较新版本的 Python,请使用选项 2。
2.Python包管理器,pip
Python 包管理器是 Python 自带的,称为pip https://www.w3schools.com/python/python_pip.asp。它从以下位置下载软件包PyPI https://pypi.org/存储库并根据 Python 版本(或什么版本)将其安装在自动位置虚拟副本 https://realpython.com/python-virtual-environments-a-primer/)您用来安装它。你想要的包是mysql-connector-python
。事实上,官方文档 https://dev.mysql.com/doc/dev/connector-python/8.0/installation.html说这是安装 MySQL Connector 的推荐方法。
例如,在 Windows 上,打开命令提示符 (cmd.exe
)通过在开始菜单中搜索。导航到所在目录pip
已安装。或者确保pip
的目录包含在 $PATH 变量中(您可以通过在“开始”菜单中搜索“PATH”来编辑该变量)。
命令提示符将向您显示以下内容:
PS C:\Users\Ryan>
除了我的用户名之外,它会显示您的用户名,或者您找到后导航到的路径pip
,如“C:\blah\blah”。然后使用这个命令...
Input:
pip install mysql-connector-python
它会下载并安装它。
Results:
PS C:\Users\Ryan> pip install mysql-connector-python
Collecting mysql-connector-python
Downloading https://files.pythonhosted.org/packages/2d/65/3fc902c0f7635912800c6b935313b99b9d4426419ef7ba04f76231b24923/mysql_connector_python-8.0.12-py2.py3-none-any.whl (300kB)
100% |████████████████████████████████| 307kB 1.1MB/s
Collecting protobuf>=3.0.0 (from mysql-connector-python)
Downloading https://files.pythonhosted.org/packages/77/78/a7f1ce761e2c738e209857175cd4f90a8562d1bde32868a8cd5290d58926/protobuf-3.6.1-py2.py3-none-any.whl (390kB)
100% |████████████████████████████████| 399kB 1.8MB/s
Requirement already satisfied: setuptools in c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (40.4.3)
Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.12 protobuf-3.6.1 six-1.11.0
之后,您可以通过显示您已安装的所有软件包(不包括 Python 附带的标准库)来确保它已安装:
Input:
PS C:\Users\Ryan> pip list
Results:
Package Version
---------------------- -------
mysql-connector-python 8.0.12
pip 18.0
protobuf 3.6.1
setuptools 40.4.3
six 1.11.0
要检查您是否将其安装到了正确的 Python 版本,请使用pip -V
。为了确保将其安装到正确的副本(虚拟或原始),请查看安装包的文件路径:
PS C:\Users\Ryan> pip -V
Results:
pip 18.0 from c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)
例如,在我的系统中,它显示python37
在文件夹路径中,因此它将它安装到我原来的 Python 3.7.0 安装中,而不是旧版本或虚拟环境(conda
or virtualenv
, etc).
要使用 Python 可执行文件检查版本,而不是pip
:
PS C:\Users\Ryan> py -V
Python 3.7.0
如果您需要将其安装到比默认安装更旧或更新的 Python 版本,请在命令中插入版本号作为选项(也称为“开关”),使用-
。例如,选择版本 3.6:
py -3.6 -m pip install mysql-connector-python
The -
适用于 Windows 和类 Unix 操作系统。