linux 安装python MySQLdb

转自:https://my.oschina.net/jywm/blog/757973

安装步骤

在服务器安装了mysql 或者mysql-connect的前提下

1、安装setuptools

2、安装mysql-python

1、No module named setuptools

*原因

没有安装setuptools工具

*解决方案

在python官网https://pypi.python.org搜 setuptools工具,复制工具连接,然后如下操作

#wget https://pypi.python.org/packages/6b/dd/a7de8caeeffab76bacf56972b3f090c12e0ae6932245abbce706690a6436/setuptools-2#md5=a46750b6bd90a1343466bd57b0e2721a
#tar -zxvf setuptools-28.3.0.tar.gz
#python setup.py build
#python setup.py install

2、mysql_config找不到

*现象

[[email protected] MySQL-python-1.2.5]# python setup.py  build
sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 17, in <module>
    metadata, options = get_config()
  File "/opt/MySQL-python-1.2.5/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/opt/MySQL-python-1.2.5/setup_posix.py", line 25, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
[[email protected] MySQL-python-1.2.5]# cd MySQL
-bash: cd: MySQL: No such file or directory
[[email protected] MySQL-python-1.2.5]#  cd MySQLdb/
-bash: $‘\345\207\272cd‘: command not found

*原因:

mysql_config 找不到,是因为在setup_posix.py文件中调用了mysql_config。而改参数的设置的值在site.cfg中

   11 def compiler_flag(f):
     12     return "-%s" % f
     13
     14 def mysql_config(what):
     15     from os import popen
     16
     17     f = popen("%s --%s" % (mysql_config.path, what))
     18     data = f.read().strip().split()
     19     ret = f.close()
     20     if ret:
     21         if ret/256:
     22             data = []
     23         if ret/256 > 1:
     24             raise EnvironmentError("%s not found" % (mysql_config.path,))
     25     return data
     26 mysql_config.path = "mysql_config"

打开 site.cfg 发现结果是linux的mysq_config 被注释掉了。所以只要将系统的mysql_config 注释去掉,并且后面的路径改成自己系统的mysql的mysql_config路径

[[email protected] MySQL-python-1.2.3]# cat site.cfg
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)

embedded = False
threadsafe = True
static = False

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
# mysql_config = /usr/local/bin/mysql_config

# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0

解决方案:

1、找到mysql_config

[[email protected] MySQL-python-1.2.3]# find / -name "mysql_config"
/usr/bin/mysql_config

2、修改site.cfg

[[email protected] MySQL-python-1.2.3]# cat site.cfg
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)

embedded = False
threadsafe = True
static = False

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/bin/mysql_config

# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
#registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0

3、unable to execute gcc: No such file or directory

*现象

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,2,5,‘final‘,1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
unable to execute gcc: No such file or directory
error: command ‘gcc‘ failed with exit status 1

*原因

 由于gcc工具没有安装导致无法使用gcc工具编译

*解决方案:

#yum install gcc -y

4、/usr/bin/ld: cannot find -lmysqlclient_r

*现象

[[email protected] MySQL-python-1.2.3]# python setup.py  build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
building ‘_mysql‘ extension
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,2,3,‘final‘,0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
In file included from _mysql.c:36:0:
/usr/include/mysql/my_config.h:440:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
 #define HAVE_WCSCOLL
 ^
In file included from /usr/include/python2.7/pyconfig.h:6:0,
                 from /usr/include/python2.7/Python.h:8,
                 from pymemcompat.h:10,
                 from _mysql.c:29:
/usr/include/python2.7/pyconfig-64.h:908:0: note: this is the location of the previous definition
 #define HAVE_WCSCOLL 1
 ^
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -L/usr/lib64 -lmysqlclient_r -lpthread -lm -lrt -ldl -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: error: ld returned 1 exit status
error: command ‘gcc‘ failed with exit status 1

*原因

这个可以看出是找不到mysqlclient_r文件或函数; ^
从gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -L/usr/lib64 可以得到gcc在/usr/lib64 路径下找mysqlclient_r,但是找不到。3.

*解决方案

这个错误找了很长时间,最后通过的 jk409博客https://my.oschina.net/jk409/blog/392035

解决了,感谢 jk409

解决方法(copy文件到/usr/lib64/路径下):

#cp /usr/lib64/mysql/libmysqlclient_r.a     /usr/lib64/

最后再编译,安装,如下

[[email protected] MySQL-python-1.2.3]# python setup.py  build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
[[email protected] MySQL-python-1.2.3]# python setup.py  install
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
reading manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
reading manifest template ‘MANIFEST.in‘
warning: no files found matching ‘MANIFEST‘
warning: no files found matching ‘ChangeLog‘
warning: no files found matching ‘GPL‘
writing manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/release.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb
creating build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/ER.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CR.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FLAG.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/REFRESH.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/cursors.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/times.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/converters.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/_mysql.so -> build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/ER.py to ER.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CR.py to CR.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FLAG.py to FLAG.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CLIENT.py to CLIENT.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/REFRESH.py to REFRESH.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/cursors.py to cursors.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/times.py to times.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pyc
byte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pyc
creating stub loader for _mysql.so
byte-compiling build/bdist.linux-x86_64/egg/_mysql.py to _mysql.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating dist
creating ‘dist/MySQL_python-1.2.3-py2.7-linux-x86_64.egg‘ and adding ‘build/bdist.linux-x86_64/egg‘ to it
removing ‘build/bdist.linux-x86_64/egg‘ (and everything under it)
Processing MySQL_python-1.2.3-py2.7-linux-x86_64.egg
Copying MySQL_python-1.2.3-py2.7-linux-x86_64.egg to /usr/lib64/python2.7/site-packages
Adding MySQL-python 1.2.3 to easy-install.pth file

Installed /usr/lib64/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.3
Finished processing dependencies for MySQL-python==1.2.3

操作过程

[[email protected] opt]# cd MySQL-python-1.2.5
[[email protected] MySQL-python-1.2.5]# ls
GPL-2.0  INSTALL      MySQL_python.egg-info  PKG-INFO   _mysql.c              doc           pymemcompat.h  setup.py         setup_posix.py    site.cfg
HISTORY  MANIFEST.in  MySQLdb                README.md  _mysql_exceptions.py  metadata.cfg  setup.cfg      setup_common.py  setup_windows.py  tests
[[email protected] MySQL-python-1.2.5]# vim site.cfg
[[email protected] MySQL-python-1.2.5]# python setup.py  build
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/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/connections.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/CR.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/ER.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
copying MySQLdb/constants/CLIENT.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,2,5,‘final‘,1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
In file included from _mysql.c:44:0:
/usr/include/mysql/my_config.h:440:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
 #define HAVE_WCSCOLL
 ^
In file included from /usr/include/python2.7/pyconfig.h:6:0,
                 from /usr/include/python2.7/Python.h:8,
                 from _mysql.c:29:
/usr/include/python2.7/pyconfig-64.h:908:0: note: this is the location of the previous definition
 #define HAVE_WCSCOLL 1
 ^
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -L/usr/lib64 -lmysqlclient_r -lpthread -lm -lrt -ldl -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so
[[email protected] MySQL-python-1.2.5]# cp /usr/lib64/mysql/libmysqlclient_r.a  /usr/lib64/lmysqlclient_r
[[email protected] MySQL-python-1.2.5]# python setup.py  build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
[[email protected] MySQL-python-1.2.5]# python setup.py  install
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
reading manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
reading manifest template ‘MANIFEST.in‘
writing manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/release.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb
creating build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/ER.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CR.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FLAG.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/REFRESH.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/cursors.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/times.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/converters.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/_mysql.so -> build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/ER.py to ER.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CR.py to CR.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FLAG.py to FLAG.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CLIENT.py to CLIENT.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/REFRESH.py to REFRESH.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/cursors.py to cursors.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/times.py to times.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pyc
byte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pyc
creating stub loader for _mysql.so
byte-compiling build/bdist.linux-x86_64/egg/_mysql.py to _mysql.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating dist
creating ‘dist/MySQL_python-1.2.5-py2.7-linux-x86_64.egg‘ and adding ‘build/bdist.linux-x86_64/egg‘ to it
removing ‘build/bdist.linux-x86_64/egg‘ (and everything under it)
Processing MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Copying MySQL_python-1.2.5-py2.7-linux-x86_64.egg to /usr/lib64/python2.7/site-packages
Removing MySQL-python 1.2.3 from easy-install.pth file
Adding MySQL-python 1.2.5 to easy-install.pth file

Installed /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
[[email protected] MySQL-python-1.2.5]#

出现问题

>>> import  MySQLdb
/usr/lib64/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib64/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /opt/MySQL-python-1.2.3 is being added to sys.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: /root/.cache/Python-Eggs/MySQL_python-1.2.3-py2.7-linux-x86_64.egg-tmp/_mysql.so: undefined symbol: __cxa_pure_virtual

解决

[[email protected] build]# cd ..
[[email protected] MySQL-python-1.2.5]# cd build/

[[email protected] build]#  g++ -pthread -shared ./temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -l mysqlclient_r -lpthread -lm -lrt -ldl -o ./lib.linux-x86_64-2.7/_mysql.so
-bash: g++: command not found
[[email protected] build]# yum install -y gcc-c++
.................
Running transaction
  Installing : libstdc++-devel-4.8.5-4.el7.x86_64                                                                                                                           1/2
  Installing : gcc-c++-4.8.5-4.el7.x86_64                                                                                                                                   2/2
  Verifying  : libstdc++-devel-4.8.5-4.el7.x86_64                                                                                                                           1/2
  Verifying  : gcc-c++-4.8.5-4.el7.x86_64                                                                                                                                   2/2

Installed:
  gcc-c++.x86_64 0:4.8.5-4.el7

Dependency Installed:
  libstdc++-devel.x86_64 0:4.8.5-4.el7

[[email protected] build]#  g++ -pthread -shared ./temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -l mysqlclient_r -lpthread -lm -lrt -ldl -o ./lib.linux-x86_64-2.7/_mysql.so
[[email protected] build]#

再重新build,install python

[[email protected] build]# cd ..
[[email protected] MySQL-python-1.2.5]# ls
GPL-2.0  INSTALL      MySQL_python.egg-info  PKG-INFO   _mysql.c              build  doc           pymemcompat.h  setup.py         setup_common.pyc  setup_posix.pyc   site.cfg
HISTORY  MANIFEST.in  MySQLdb                README.md  _mysql_exceptions.py  dist   metadata.cfg  setup.cfg      setup_common.py  setup_posix.py    setup_windows.py  tests
[[email protected] MySQL-python-1.2.5]# python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
[[email protected] MySQL-python-1.2.5]# python setup.py install
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
reading manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
reading manifest template ‘MANIFEST.in‘
writing manifest file ‘MySQL_python.egg-info/SOURCES.txt‘
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/release.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb
creating build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/ER.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CR.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FLAG.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/REFRESH.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
copying build/lib.linux-x86_64-2.7/MySQLdb/cursors.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/times.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/MySQLdb/converters.py -> build/bdist.linux-x86_64/egg/MySQLdb
copying build/lib.linux-x86_64-2.7/_mysql.so -> build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/ER.py to ER.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CR.py to CR.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FLAG.py to FLAG.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CLIENT.py to CLIENT.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/REFRESH.py to REFRESH.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/cursors.py to cursors.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/times.py to times.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pyc
byte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pyc
creating stub loader for _mysql.so
byte-compiling build/bdist.linux-x86_64/egg/_mysql.py to _mysql.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying MySQL_python.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating ‘dist/MySQL_python-1.2.5-py2.7-linux-x86_64.egg‘ and adding ‘build/bdist.linux-x86_64/egg‘ to it
removing ‘build/bdist.linux-x86_64/egg‘ (and everything under it)
Processing MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Removing /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Copying MySQL_python-1.2.5-py2.7-linux-x86_64.egg to /usr/lib64/python2.7/site-packages
MySQL-python 1.2.5 is already the active version in easy-install.pth

Installed /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
[[email protected] MySQL-python-1.2.5]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.pyc, but /opt/MySQL-python-1.2.5 is being added to sys.path
>>>
>>> MySQLdb.version_info
(1, 2, 5, ‘final‘, 1)
>>> dir(MySQLdb)
[‘BINARY‘, ‘Binary‘, ‘Connect‘, ‘Connection‘, ‘DATE‘, ‘DATETIME‘, ‘DBAPISet‘, ‘DataError‘, ‘DatabaseError‘, ‘Date‘, ‘DateFromTicks‘, ‘Error‘, ‘FIELD_TYPE‘, ‘IntegrityError‘, ‘InterfaceError‘, ‘InternalError‘, ‘MySQLError‘, ‘NULL‘, ‘NUMBER‘, ‘NotSupportedError‘, ‘OperationalError‘, ‘ProgrammingError‘, ‘ROWID‘, ‘STRING‘, ‘TIME‘, ‘TIMESTAMP‘, ‘Time‘, ‘TimeFromTicks‘, ‘Timestamp‘, ‘TimestampFromTicks‘, ‘Warning‘, ‘__all__‘, ‘__author__‘, ‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘__path__‘, ‘__revision__‘, ‘__version__‘, ‘_mysql‘, ‘apilevel‘, ‘connect‘, ‘connection‘, ‘constants‘, ‘debug‘, ‘escape‘, ‘escape_dict‘, ‘escape_sequence‘, ‘escape_string‘, ‘get_client_info‘, ‘paramstyle‘, ‘release‘, ‘result‘, ‘server_end‘, ‘server_init‘, ‘string_literal‘, ‘test_DBAPISet_set_equality‘, ‘test_DBAPISet_set_equality_membership‘, ‘test_DBAPISet_set_inequality‘, ‘test_DBAPISet_set_inequality_membership‘, ‘thread_safe‘, ‘threadsafety‘, ‘times‘, ‘version_info‘]

© 著作权归作者所有

打赏点赞 (0)收藏 (0)

分享

举报

上一篇:centos下安装pip

下一篇:安装mysql出的错误

Mr_Tea伯奕

粉丝 5

博文 217

码字总数 51690

作品 0

朝阳

其他

关注 私信 提问

相关文章最新文章

ImportError: No module named MySQLdb

ImportError: No module named MySQLdb 该错误是源于我们没有安装Python连接MySQL所需的MySQLdb库而引起。 MySQL是最流行的开源数据库之一,但在Python标准库中并没有集成MySQL接口程序,MyS...

netmouse

2014/10/17

0

0

fedora 14下配置python开发环境

先装python,由于系统已有python,将python安装至home目录下; 修改home目录下.bash_profile文件,添加python/bin路径到PATH中; 添加MySQLdb,遇到很多问题,以下内容装载自 http://tobeno1...

黑桃

2010/12/18

0

0

python MySQLdb安装和使用

MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-python/或者点击链接下载http://downloads.sour...

我的去哈哈

2014/03/28

0

2

Fedora 14下配置Python开发环境

pythone新闻1则 Python十分适合解决很多简单的、有趣的问题,比用C语言要省力的多。(这是实话,几行Python代码你就能制作一个简单的拼写检查程 序。)你会发现这种语言里几乎没有任何复杂或让...

墙头草

2011/03/29

0

1

Pycharm开发工具里面python3连接MySQL问题

参考地址:http://www.runoob.com/python3/python3-mysql.html 以前的代码是python2.7开发的,Python2中则使用mysqldb,现在准备用python3,则用不了mysqldb组件;PyMySQL 是在 Python3.x 版...

mchdba

2018/04/18

0

0

加载更多

原文地址:https://www.cnblogs.com/laosu8/p/11025693.html

时间: 2024-08-08 05:37:34

linux 安装python MySQLdb的相关文章

Windows+Linux安装Python工具setuptools

Windows+Linux安装Python工具setuptools setuptools是 Python Enterprise Application Kit(PEAK)的一个副项目,它 是一组Python的 distutilsde工具的增强工具(适用于 Python 2.3.5 以上的版本,64 位平台则适用于 Python 2.4 以上的版本),可以让程序员更方便的创建和发布 Python 包,特别是那些对其它包具有依赖性的状况. 经常接触Python可能会注意到,当需要安装第三方pytho

Linux 安装python爬虫框架 scrapy

Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 1.1 测试是否已经安装pip # pip --version 如果没有pip,安装: # sudo apt-get install python-pip 1.2 然后安装scrapy Import the GPG key used to sign Scrapy packages into APT

Centos下安装python mysqldb

环境:centos6.6 python版本:2.6 mysql版本:5.1.73 需要安装的mysqldb版本:1.2.5 下载完mysqldb后,进入其目录,运行python setup.py install会报出一大堆错,原因是很多依赖库都没有安装,我们需要在这之前运行: yum install gcc python-devel mysql-devel -y 然后再运行setup就没问题了.

linux 安装python,pip,

Linux下python升级步骤 http://www.cnblogs.com/lanxuezaipiao/archive/2012/10/21/2732864.html 在 https://www.python.org/downloads/source/ 找到地址 wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz tar -xzvf Python-3.4.2.tgz 解压缩后开始安装 ..... Linux 下安装pyt

Linux安装python环境方法

打开WEB浏览器访问http://www.python.org/download/ 选择适用于Unix/Linux的源码压缩包. 下载及解压压缩包. 如果你需要自定义一些选项修改Modules/Setup 执行 ./configure 脚本 make make install 执行以上操作后,Python会安装在 /usr/local/bin 目录中,Python库安装在/usr/local/lib/pythonXX,XX为你使用的Python的版本号.

windows安装python 和 linux安装python

一.Windows 1.下载安装包     https://www.python.org/downloads/ 2.安装    python2.7安装     默认安装路径:C:\python27          下一步 下一步,注意勾选自动配置环境变量选项:          安装完成.运行cmd命令行 → 输入python 进入python交互模式:python2.7安装完成.    python3.5安装:     运行python-3.5.2.exe     注意勾选自动配置环境变量选

Linux 安装 python IDE

参考:http://blog.sina.com.cn/s/blog_4c451e0e01013419.html http://blog.csdn.net/voidcc/article/details/25834331 http://www.cnblogs.com/Realh/archive/2010/10/10/1847251.html 我的操作系统信息: Ubuntu 12.04 查看操作系统版本: cat /etc/issue 1.安装eclipse,默认版本3.7 sudo apt-ins

Linux 安装Python虚拟环境,virtualenvwrapper

virtualenvwrapper工具学习 1.因为virtualenv 工具使用的并不方便 2.安装virtualenvwrapper pip3 install -i https://pypi.douban.com/simple virtualenvwrapper 3.配置virtualenvwrapper的环境变量,每次开机就启动 1.#这个文件是用户个人配置文件 vim ~/.bashrc 2.写入以下几行代码export 和source一样都是读取linux shell变量的命令 exp

linux 安装 python 最全教程

环境:centos6.5 centos6.5 自带的 python 版本是 2.6.6,需要重新安装 2.7: centos7 自带的 python 版本是 2.7.5 基本操作 在安装新版本之前,一定要先执行如下操作 yum install -y zlib-devel bzip2-devel xz-libs wget openssl openssl-devel 如果你没有执行该操作就安装了,删掉重来,不听话后果自负 然后安装新版本 # 下载 python tar 包 # 解压 # cd 到解压