【问题标题】:Unable to install mysqlclient on centoscentos无法安装mysqlclient
【发布时间】:2021-07-05 22:16:51
【问题描述】:

我正在构建一个 django 应用程序,我需要为其配置 mysql。我正在尝试为 sql 连接安装 mysqlclient 模块,这就是我正在尝试的

pip install mysqlclient --no-cache-dir

它抛出了以下错误。它在链接到 gcc 库时抛出错误。

Collecting mysqlclient
  Downloading mysqlclient-1.3.12.tar.gz (89kB)
    100% |################################| 92kB 4.0MB/s 
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /home/admin/awx.varadev.com/awxenv/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-6m2TNP/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-WFoARo-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/admin/awx.varadev.com/awxenv/include/site/python2.7/mysqlclient:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
    creating build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o
    In file included from /usr/include/python2.7/pyconfig.h:6:0,
                     from /usr/include/python2.7/Python.h:8,
                     from _mysql.c:32:
    /usr/include/python2.7/pyconfig-64.h:1188:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
     #define _POSIX_C_SOURCE 200112L
     ^
    In file included from /usr/include/sys/types.h:25:0,
                     from /usr/include/mysql/mysql.h:38,
                     from _mysql.c:29:
    /usr/include/features.h:168:0: note: this is the location of the previous definition
     # define _POSIX_C_SOURCE 200809L
     ^
    In file included from /usr/include/python2.7/pyconfig.h:6:0,
                     from /usr/include/python2.7/Python.h:8,
                     from _mysql.c:32:
    /usr/include/python2.7/pyconfig-64.h:1210:0: warning: "_XOPEN_SOURCE" redefined [enabled by default]
     #define _XOPEN_SOURCE 600
     ^
    In file included from /usr/include/sys/types.h:25:0,
                     from /usr/include/mysql/mysql.h:38,
                     from _mysql.c:29:
    /usr/include/features.h:170:0: note: this is the location of the previous definition
     # define _XOPEN_SOURCE 700
     ^
    gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -L/usr/lib64 -lmariadb -lpthread -lz -ldl -lm -lssl -lcrypto -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so
    /usr/bin/ld: cannot find -lmariadb
    collect2: error: ld returned 1 exit status
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/home/admin/awx.varadev.com/awxenv/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-6m2TNP/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-WFoARo-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/admin/awx.varadev.com/awxenv/include/site/python2.7/mysqlclient" failed with error code 1 in /tmp/pip-build-6m2TNP/mysqlclient/

这个 gcc 默认功能是为 mysql 链接一个库文件吗?我已经对此进行了谷歌搜索,它表明 gcc 需要作为 .so 文件,我需要与某个位置进行符号链接。我在我的服务器上运行 mariadb 10.9,我在我的系统中没有找到任何这样的文件.

【问题讨论】:

  • 有什么原因你不能从 CentOS 软件包存储库(例如通过yum install MySQL-python)获取客户端而不是通过 pip 安装?

标签: python mysql django


【解决方案1】:

在 CentOS 7 + MariaDB 10.2 中解决

我有同样的问题,我想贡献我的答案。我刚刚安装在我的 2 台运行 CentOS 7 和 MariaDB 的服务器上(10.2.14-MariaDB MariaDB 服务器) .

$ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)

$ mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is XXXX
Server version: 10.2.14-MariaDB MariaDB Server

我已经安装了 MariaDB,这个包:

$ yum list installed | grep mariadb
MariaDB-client.x86_64                   10.3.13-1.el7.centos           @mariadb
MariaDB-common.x86_64                   10.3.13-1.el7.centos           @mariadb
MariaDB-compat.x86_64                   10.3.13-1.el7.centos           @mariadb
MariaDB-server.x86_64                   10.3.13-1.el7.centos           @mariadb
galera.x86_64                           25.3.25-1.rhel7.el7.centos     @mariadb

我发现问题是mysqlclient需要mysql-devel包,和mariadb-devel不一样。 不要安装 mariadb-devel!

所以,要只安装 mysql-devel,你需要:

1。删除任何 MariaDB-devel

$ sudo yum erase MariaDB-devel.x86_64

2。在 yum 中添加 MySQL 仓库

  1. 转到 https://dev.mysql.com/downloads/repo/yum/ 并为您的 CentOS 选择 RPM 文件(对我来说,我选择 "Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture独立),RPM 包”。点击“下载”。)
  2. 无需注册即可下载,复制底部链接“不,谢谢,开始我的下载”
  3. 转到您的终端并输入:
$ wget link-to-rpm-you-choose
  1. 下载完成后,输入:
$ sudo rpm -Uvh your-rpm-downloaded

3。现在,安装 mysql-devel

  1. 输入(这是我的版本,看你的):
$ sudo yum install mysql-community-devel.x86_64

4。现在,最后:mysqlclient

  1. 类型:
$  sudo pip install mysqlclient
Collecting mysqlclient
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz (85kB)
    100% |████████████████████████████████| 92kB 758kB/s
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-1.4.2.post1
You are using pip version 8.1.2, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

就是这样!它就像一个魅力,现在我可以使用 Python + Django + MariaDB/MySQL

哦,mysqlclient 是 Django 推荐的连接器。见:https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers

祝你好运,再见! :-)

【讨论】:

    【解决方案2】:

    我在https://github.com/r-dbi/RMySQL/issues/197找到了这个解决方案:

    MariaDB-devel-10.2.6-1.fc25.x86_64 包仅包含一个名为:

    /usr/lib64/libmariadbclient.a
    

    但是他们的 pkg-config 产生了另一个名字:

    > pkg-config --libs mariadb
        -lmariadb
    

    解决方法是将 libmariadbclient.a 符号链接到 libmariadb.a:

    sudo ln -s /usr/lib64/libmariadbclient.a /usr/lib64/libmariadb.a
    

    基本上要在 Python 3.6 和 CentOS 7 中安装 mysqlclient,您需要运行:

    sudo yum install -y python36-devel mysql-devel gcc
    
    sudo ln -s /usr/lib64/libmariadbclient.a /usr/lib64/libmariadb.a
    
    pip install mysqlclient
    

    【讨论】:

      【解决方案3】:

      它抱怨无法找到 mariadb 库。运行以下命令找出原因:

      ld -lmariadb --verbose
      

      这应该会具体告诉您 gcc 缺少什么。

      我的猜测是您缺少一些 MySQL 开发标头。安装它们:

      debian / ubuntu: sudo apt-get install python-dev libmysqlclient-dev
      redhat / centos: sudo yum install python-devel mysql-devel
      

      更新:确实缺少 MariaDB 共享库。我认为在 CentOS 上,这应该解决它:

      sudo yum install MariaDB-devel
      

      最终更新:只需使用 PyMySQL - 纯 python,不需要标头,无需跳过这些类型的箍。

      【讨论】:

      • 我的问题是如何安装所有这些文件而不是检查它是否安装,因为这个命令给了我这个cannot find -lzlib
      • 我打错了 ld 推荐 - 再试一次 :)
      • 对不起,没有成功。得到这个回复:Failed to set locale, defaulting to C Package python-devel-2.7.5-58.el7.x86_64 already installed and latest version Package MariaDB-devel-10.2.9-1.el7.centos.x86_64 already installed and latest version Nothing to do
      • ld: cannot find -lmariadb
      • @AtulAgrawal - 我想你需要安装它们 - 我更新了我的答案,但 sudo yum install MariaDB-devel
      【解决方案4】:

      打开终端或使用 ssh 客户端登录到工作站/笔记本电脑/开发服务器。以 root 用户身份输入以下命令 yum 命令:

      # yum install mysql
      Loaded plugins: downloadonly, fastestmirror, security
      Loading mirror speeds from cached hostfile
       * base: mirror.wiredtree.com
       * extras: mirrors.serveraxis.net
       * updates: bay.uchicago.edu
      Setting up Install Process
      Resolving Dependencies
      --> Running transaction check
      ---> Package mysql.x86_64 0:5.1.71-1.el6 will be installed
      --> Finished Dependency Resolution
      
      Dependencies Resolved
      
      ================================================================================
       Package         Arch             Version                  Repository      Size
      ================================================================================
      Installing:
       mysql           x86_64           5.1.71-1.el6             base           893 k
      
      Transaction Summary
      ================================================================================
      Install       1 Package(s)
      
      Total download size: 893 k
      Installed size: 2.4 M
      Is this ok [y/N]: y
      Downloading Packages:
      mysql-5.1.71-1.el6.x86_64.rpm                            | 893 kB     00:00     
      Running rpm_check_debug
      Running Transaction Test
      Transaction Test Succeeded
      Running Transaction
        Installing : mysql-5.1.71-1.el6.x86_64                                    1/1 
        Verifying  : mysql-5.1.71-1.el6.x86_64                                    1/1 
      
      Installed:
        mysql.x86_64 0:5.1.71-1.el6                                                   
      
      Complete!
      

      mysql客户端基本语法是:

      mysql -u USER-NAME-HERE -h MYSQL-DB-SERVER-IP-ADDRESS-HERE -p DB-NAME
      mysql -u nixcraft -h server1.cyberciti.biz -p salesdata
      

      【讨论】:

      • 这是我收到的回复Failed to set locale, defaulting to C Package MariaDB-client-10.2.9-1.el7.centos.x86_64 already installed and latest version Nothing to do
      • 将以下与语言环境相关的设置放入 /etc/bashrc(对于所有用户,bash): export LANG=en_US.UTF-8 export LANGUAGE=en_US.UTF-8 export LC_COLLATE=C export LC_CTYPE= en_US.UTF-8 然后运行: source /etc/bashrc 或注销并登录。
      【解决方案5】:

      对我有用的最简单的解决方案是:

      yum install python-devel mysql-devel
      

      然后使用pip安装mysqlclient

      pip install mysqlclient
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 2018-08-05
        • 2018-03-14
        • 2018-06-10
        • 1970-01-01
        • 2022-11-23
        相关资源
        最近更新 更多