Centos操作系统新安装的Python3中安装mysqlclient库

问题简介:

        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 <module>
          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 "<string>", line 154, in <module>
        File "<string>", line 48, in get_config_posix
        File "<string>", 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

问题说明:

1)使用rpm -qa 查看操作系统中是否安装过mysql数据库

[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 有关系。

 2)使用rpm -e 卸载操作系统中的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中只能存在一个。

解决方法:

1)完全卸载mariadb

使用 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-devel

1、从mysql官网下载mysql的数据库的rpm安装的压缩包

 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

2、使用rpm -ivh packages 安装

        四个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

 3、查看mysql动态链接库

        当我们安装上边四个包成功后,会在/usr/lib64 目录下创建一个mysql的目录,mysql目录中包含的就是mysqlclient运行时需要的动态链接库,这个mysql目录一定不能删除!!! 否者,即使mysqlclient 安装成功也不能使用,报错就是缺少链接库 文件。

 安装验证

1)安装 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

2)验证 mysqlclient库

[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]# 

3)验证结果

        使用pip工具安装的mysqlclient 完全没有报错,而且还给我们编译成了whl文件,在测试的时候也是可以看出mysqlclient连接数据库是没有问题的,查询也是OK的。

注意:在使用.whl 文件在另一个其它操作系统使用pip安装的时候也是没有问题的,但是当测试连接数据库的时候,会出现动态库找不到的问题,这里把/usr/lib64/mysql/*的动态链接库复制到新的操作系统中,目录不变,并创建软连接,文件以当前环境为准。

ln -s /usr/lib64/mysql/libmysqlclient.so.20 /usr/lib64/libmysqlclient.so.20

  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值