mysqlclient 是python中的一个连接MySQL数据库的重要的三方库,但是在centos中使用pip3 install mysqlclient 方法安装一直报错,经过两天时间的排查,终于找到了解决问题的方法。
[root@d3acd2b8211d /]# pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-2.2.0.tar.gz (89 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
╰─> [22 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.
Traceback (most recent call last):
File "/usr/local/python3.8/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in
main()
File "/usr/local/python3.8/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/usr/local/python3.8/lib/python3.8/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-p5b_my6i/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/tmp/pip-build-env-p5b_my6i/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-p5b_my6i/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 338, in run_setup
exec(code, locals())
File "", line 154, in
File "", line 48, in get_config_posix
File "", line 27, 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.
关于这个报错显示,参考了好几篇博客,都是在说缺少 mysql-devel 库,用 yum -y install mysql-devel,在使用pip3安装就可了。但是实际上并没有什么卵用,不知这里尝试了多少遍,因为当使用yum安装mysql-deve的时候,结果安装的确是这个库,mariadb-devel,跟mysql,并没关系。(大家可自行对比,mysql跟mariadb 的区别)
Package 1:mariadb-devel-5.5.68-1.el7.x86_64 already installed and latest version
[root@d3acd2b8211d /]# rpm -aq | grep mysql
[root@d3acd2b8211d /]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
mariadb-devel-5.5.68-1.el7.x86_64
使用rpm 命名查看操作系统中存在mariadb数据库,但是不存在mysql,数据库 ,因为mariadb数据库是centos操作系统中自带的,mariadb-devel 是我们用 yum -y install mysql-devel安装的(这就很扯淡),为什么mysql-devel 跟mariadb 有关系。
[root@d3acd2b8211d /]# rpm -e mariadb-devel-5.5.68-1.el7.x86_64
[root@d3acd2b8211d /]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64
!!!强调一下,卸载完mariadb-devel mariadb-lis之后,再使用yum 安装mysql-devel 又会重新安装mariadb,不会安装mysql数据库。mysql数据库与mariadb数据库在centos中只能存在一个。
使用 rpm -e 方法卸载mariadb,而且要完全卸载,如果不卸载,安装mysql的时候会报错。
[root@d3acd2b8211d /]# rpm -e mariadb-devel-5.5.68-1.el7.x86_64
[root@d3acd2b8211d /]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64
2、将下载好的mysql包上传到服务器上,解压,并安装
解压后的安装装包有好多个,我们只安装下边标红的四个就可以,再次强调,我们不是安装mysql数据库服务跟mysql的客户端,我们是安装mysqlclient的编译的依赖包。
[root@d3acd2b8211d opt]# tar -xvf mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.43-1.el7.x86_64.rpm
mysql-community-common-5.7.43-1.el7.x86_64.rpm
mysql-community-devel-5.7.43-1.el7.x86_64.rpm
mysql-community-embedded-5.7.43-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.43-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.43-1.el7.x86_64.rpm
mysql-community-libs-5.7.43-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.43-1.el7.x86_64.rpm
mysql-community-server-5.7.43-1.el7.x86_64.rpm
mysql-community-test-5.7.43-1.el7.x86_64.rpm
四个rpm包的安装顺序,强调包跟包之间有强依赖关系,顺序一定不能错。下边是正确的安装顺序与安装结果的显示,如果mariadb数据库不卸载,也会安装不成功,请一定要先卸载再安装,谨记!!!
rpm -ivh mysql-community-common-5.7.43-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.43-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.43-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.43-1.el7.x86_64.rpm
当我们安装上边四个包成功后,会在/usr/lib64 目录下创建一个mysql的目录,mysql目录中包含的就是mysqlclient运行时需要的动态链接库,这个mysql目录一定不能删除!!! 否者,即使mysqlclient 安装成功也不能使用,报错就是缺少链接库 文件。
pip3 install mysqlclient,安装完之后可以看到在安装的过程把 mysqlclient-2.2.0.tar.gz 给我编译成了一个wheel文件mysqlclient-2.2.0-cp38-cp38-linux_x86_64.whl,这个whl文件是可以在任何地方直接安装的。(但是要注意操作系统跟python的版本是否适用)
[root@d3acd2b8211d opt]# pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-2.2.0.tar.gz (89 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Installing backend dependencies ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
Building wheel for mysqlclient (pyproject.toml) ... done
Created wheel for mysqlclient: filename=mysqlclient-2.2.0-cp38-cp38-linux_x86_64.whl size=121344 sha256=c89876ae62449ecb7c56e3e581c8e6ab8bd09cabb8b242650588e16d2784eb1f
Stored in directory: /root/.cache/pip/wheels/c6/45/60/65ff711ddb1059e530228ebc5ddf10289427af7bc77850923d
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.2.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@d3acd2b8211d opt]# cat testmysql.py
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect(host='192.168.109.135', port=3306, user='root', password='test_12345',
db='pools', charset="utf8")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
cursor.execute("show tables;")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print(data)
[root@d3acd2b8211d opt]# python3 testmysql.py
('app01_department',)
[root@d3acd2b8211d opt]#
使用pip工具安装的mysqlclient 完全没有报错,而且还给我们编译成了whl文件,在测试的时候也是可以看出mysqlclient连接数据库是没有问题的,查询也是OK的。
ln -s /usr/lib64/mysql/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20