在linux上进行Django应用开发,数据库使用Mysql,而django连接mysql数据库需要 mysqlclient 包。但是 pip install mysqlclient 出现以下报错:
(yolo8) z@z-CV15S:~/python_workspace/fall_down_application$ pip install mysqlclient
Looking in indexes: https://mirrors.aliyun.com/pypi/simple, https://pypi.ngc.nvidia.com
Collecting mysqlclient
Downloading https://mirrors.aliyun.com/pypi/packages/7d/62/51fbcd851834c830c940ded80280f593bd031137603329dd89479c68c5be/mysqlclient-2.2.6.tar.gz (91 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [26 lines of output]
Trying pkg-config --exists mysqlclient
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
Trying pkg-config --exists mariadb
Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
Trying pkg-config --exists libmariadb
Command 'pkg-config --exists libmariadb' returned non-zero exit status 1.
Trying pkg-config --exists perconaserverclient
Command 'pkg-config --exists perconaserverclient' returned non-zero exit status 1.
Traceback (most recent call last):
File "/home/z/anaconda3/envs/yolo8/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/home/z/anaconda3/envs/yolo8/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/z/anaconda3/envs/yolo8/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/pip-build-env-rylnxc61/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 334, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
File "/tmp/pip-build-env-rylnxc61/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-rylnxc61/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 320, in run_setup
exec(code, locals())
File "<string>", line 155, in <module>
File "<string>", line 49, in get_config_posix
File "<string>", line 28, in find_package_name
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.note: This error originates from a subprocess, and is likely not a problem with pip.
报错的核心信息是:
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
出现这个报错,可能的原因及其解决方案:
1.mysql数据库没安装或没有正确安装。将mysql安装配置好再安装 mysqlclient,具体mysql安装方案可参考这篇文章。下面是简易的安装命令(前提是正确配置了mysql的apt源才可正确执行下面的命令)
sudo apt-get install mysql-server
2.检查pkg-config包是否安装。安装mysqlclient需要系统有pkg-config包,若系统中缺少pkg-config命令,导致无法找到正确的库文件
sudo apt install pkg-config
3.在linux上,没有安装mysql客户端开发工具----libmysqlclient-dev
这里解释一下libmysqlclient-dev是怎么回事
libmysqlclient-dev 是一个在基于 Linux 发行版(如 Ubuntu、Debian等)中使用的软件包,它提供了 MySQL 数据库的客户端库的开发文件。这个软件包通常用于开发需要与 MySQL 数据库进行交互的应用程序。它提供开发 MySQL 应用程序所需的开发文件。无论你在linux上开发一个 C 或 C++ 、python应用程序,需要连接到 MySQL 数据库并执行 SQL 查询。为了实现这一点,你需要:
(1)安装 MySQL 服务器(就是Mysql-Server):首先,你需要在你的系统上安装 MySQL 服务器,这样应用程序才能连接到它。
(2)安装 libmysqlclient-dev:通过运行 `apt install libmysqlclient-dev` 命令,你可以安装 MySQL 客户端库的开发文件。
(3)编写代码:在你的应用程序中,你可以使用 MySQL 提供的 API 来连接数据库、执行查询等
像我这里就是由于Linux系统是个新系统,此前从未在此系统上安装过ibmysqlclient-dev,只安装了mysql-server,因此无法安装mysqlclient。注意这个是linux系统特有的错误,像在windows中进行mysql数据库开发,是不需要libmysqlclient这样的mysql客户端开发库的。