Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

python - Unable to install mysqlclient on centos

I am building a django app and for which i need to configure mysql.I am trying to install mysqlclient module for sql connection and this is what i am trying

pip install mysqlclient --no-cache-dir

It is throwing me following error.It is throwing error while linking to gcc library.

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('
', '
');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('
', '
');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/

is this gcc default feature to link a library file for mysql? I have googled on this and it showing that gcc needs as .so file which i need to symlink with some location.I am running mariadb 10.9 on my server and i did not find any such file in my system.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved in CentOS 7 + MariaDB 10.2

I'm having same issue, and I would like to contribute with my answer. I've just installed in my 2 servers running CentOS 7 with MariaDB (10.2.14-MariaDB MariaDB Server) .

$ 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

I've installed MariaDB, this packages:

$ 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

I found that the problem is that mysqlclient requires mysql-devel packages, which is different from mariadb-devel. Don't install mariadb-devel!

So, to install only mysql-devel, you need to:

1. Remove any MariaDB-devel

$ sudo yum erase MariaDB-devel.x86_64

2. Add MySQL repository in yum

  1. Go to https://dev.mysql.com/downloads/repo/yum/ and select the RPM file for your CentOS (for me, I choose "Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package". Click "download".)
  2. Download without registration, copy bottom link "No thanks, just start my download".
  3. Go to your terminal and type:
$ wget link-to-rpm-you-choose
  1. After download complete, type:
$ sudo rpm -Uvh your-rpm-downloaded

3. Now, install mysql-devel

  1. Type (this is my version, watch yours):
$ sudo yum install mysql-community-devel.x86_64

4. Now, finally: mysqlclient

  1. Type:
$  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.

And that's it! It worked like a charm and now I can use Python + Django + MariaDB/MySQL

Oh, and mysqlclient is the connector recommended by Django. See: https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers

Good luck and see ya! :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...