相关推荐recommended
离线安装python2的MySQLdb
作者:mmseoamin日期:2024-01-24

离线安装python2的MySQLdb

需求

环境:

centos7.9

pytthon2.7.5

安装MySQLdb模块

下载MySQLdb模块地址

直接安装报错如下

[root@localhost MySQL-python-1.2.5]# ls
doc      HISTORY  MANIFEST.in   _mysql.c  _mysql_exceptions.py   PKG-INFO       README.md  setup_common.py  setup.py          site.cfg
GPL-2.0  INSTALL  metadata.cfg  MySQLdb   MySQL_python.egg-info  pymemcompat.h  setup.cfg  setup_posix.py   setup_windows.py  tests
[root@localhost 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 
    metadata, options = get_config()
  File "/root/MySQL-python-1.2.5/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/root/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

解决报错需要安装python-devel mysql-devel 找个相同的环境下载这两个的rpm离线

配置阿里yum源

wget http://mirrors.aliyun.com/repo/Centos-7.repo
清理缓存
[root@localhost yum.repos.d]# sudo yum clean all
[root@localhost yum.repos.d]# sudo yum makecache

1、下载python-devel mysql-devel安装包

[root@localhost yum.repos.d]# yum install --downloadonly --downloaddir=/root/devel/ python-devel mysql-devel
[root@localhost yum.repos.d]# cd /root/devel/
[root@localhost devel]# ls
e2fsprogs-1.42.9-19.el7.x86_64.rpm          krb5-libs-1.15.1-50.el7.x86_64.rpm         libselinux-devel-2.5-15.el7.x86_64.rpm  mariadb-devel-5.5.68-1.el7.x86_64.rpm   python-devel-2.7.5-89.el7.x86_64.rpm
e2fsprogs-libs-1.42.9-19.el7.x86_64.rpm     libcom_err-1.42.9-19.el7.x86_64.rpm        libsepol-devel-2.5-10.el7.x86_64.rpm    openssl-devel-1.0.2k-19.el7.x86_64.rpm  python-libs-2.7.5-89.el7.x86_64.rpm
keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm  libcom_err-devel-1.42.9-19.el7.x86_64.rpm  libss-1.42.9-19.el7.x86_64.rpm          python-2.7.5-89.el7.x86_64.rpm          python-rpm-macros-3-34.el7.noarch.rpm
krb5-devel-1.15.1-50.el7.x86_64.rpm         libkadm5-1.15.1-50.el7.x86_64.rpm          libverto-devel-0.2.5-4.el7.x86_64.rpm   python2-rpm-macros-3-34.el7.noarch.rpm  python-srpm-macros-3-34.el7.noarch.rpm

2、强制不校验安装python-devel mysql-devel

[root@localhost devel]# rpm -ivh *.rpm --force
error: Failed dependencies:
	python < 2.7.5-89.el7 conflicts with python-devel-2.7.5-89.el7.x86_64
[root@localhost devel]# rpm -ivh *.rpm --force --nodeps
Preparing...                          ################################# [100%]
Updating / installing...
   1:libcom_err-1.42.9-19.el7         ################################# [  5%]
   2:krb5-libs-1.15.1-50.el7          ################################# [ 10%]
   3:python-libs-2.7.5-89.el7         ################################# [ 15%]
   4:python-2.7.5-89.el7              ################################# [ 20%]
   5:libkadm5-1.15.1-50.el7           ################################# [ 25%]
   6:e2fsprogs-libs-1.42.9-19.el7     ################################# [ 30%]
   7:libcom_err-devel-1.42.9-19.el7   ################################# [ 35%]
   8:libss-1.42.9-19.el7              ################################# [ 40%]
   9:python-srpm-macros-3-34.el7      ################################# [ 45%]
  10:python-rpm-macros-3-34.el7       ################################# [ 50%]
  11:python2-rpm-macros-3-34.el7      ################################# [ 55%]
  12:libverto-devel-0.2.5-4.el7       ################################# [ 60%]
  13:libsepol-devel-2.5-10.el7        ################################# [ 65%]
  14:libselinux-devel-2.5-15.el7      ################################# [ 70%]
  15:keyutils-libs-devel-1.5.8-3.el7  ################################# [ 75%]
  16:krb5-devel-1.15.1-50.el7         ################################# [ 80%]
  17:openssl-devel-1:1.0.2k-19.el7    ################################# [ 85%]
  18:mariadb-devel-1:5.5.68-1.el7     ################################# [ 90%]
  19:python-devel-2.7.5-89.el7        ################################# [ 95%]
  20:e2fsprogs-1.42.9-19.el7          ################################# [100%]

再次回到MySQL-python-1.2.5目录下

3、安装MySQL-python-1.2.5

[root@localhost 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
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient -lpthread -lz -lm -ldl -lssl -lcrypto -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so

可以看到不报错了,接着安装模块

root@localhost MySQL-python-1.2.5]# python setup.py install
running install
running bdist_egg
running egg_info
/usr/lib/python2.7/site-packages/setuptools/command/egg_info.py:177: PkgResourcesDeprecationWarning: Parameters to load are deprecated.  Call .resolve and .require separately.
  writer = ep.load(installer=installer)
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
copying build/lib.linux-x86_64-2.7/_mysql_exceptions.py -> build/bdist.linux-x86_64/egg
creating 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
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/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdb
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
creating 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/FIELD_TYPE.py -> 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/FLAG.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/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants
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/_mysql.so -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.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/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/FIELD_TYPE.py to FIELD_TYPE.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/FLAG.py to FLAG.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/CLIENT.py to CLIENT.pyc
byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.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
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
Traceback (most recent call last):
  File "setup.py", line 21, in 
    setuptools.setup(**metadata)
  File "/usr/lib64/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 73, in run
    self.do_egg_install()
  File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 101, in do_egg_install
    cmd.run()
  File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 380, in run
    self.easy_install(spec, not self.no_deps)
  File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 604, in easy_install
    return self.install_item(None, spec, tmpdir, deps, True)
  File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 655, in install_item
    self.process_distribution(spec, dist, deps)
  File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 701, in process_distribution
    distreq.project_name, distreq.specs, requirement.extras
TypeError: __init__() takes exactly 2 arguments (4 given)

4、导入MySQLdb模块

[root@localhost MySQL-python-1.2.5]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] 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 /root/MySQL-python-1.2.5 is being added to sys.path
>>> import MySQLdb
>>> import MySQLdb
>>> quit()
六级标题