centos7 安装python-pip 以及 Python的mysql驱动 MySQLdb

本文档详细介绍了在CentOS7系统中如何安装Python的包管理工具pip,以及Python的MySQL驱动MySQLdb。首先解决网络配置问题,通过yum安装pip,然后在遇到官方源滞后的问题时,采用EPEL源来安装,并逐步安装python-devel和mysql-devel以确保成功安装MySQL驱动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、pip——python库的安装工具

注意:以下所有命令都需要在root下执行,或者使用sudo进行权限提升,我是在root用户下执行的。

1、在命令行键入以下命令  yum install python-pip

[root@localhost xiayzn]# yum install python-pip

遇到问题:

[root@localhost xiayzn]# yum install python-pip
已加载插件:fastestmirror, langpacks
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"


 One of the configured repositories failed (未知),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

原因:百度了一下这个错误,发现报错原因是“本机无法连接网络的网络设置”的原因。

解决方法及步骤:配置CentOS 7的网络 

该解决方法参照“How to Setup network on centos 7”这篇博文,配置了下DHPC,最终解决了这个error。

GUI模式

1、在终端中输入命令  nmcli d  ,查看安装在本机的网卡(下图中用蓝色框框起来的就是我需要配置的网卡名,不同centos镜像文件安装的centos7系统文件名各不相同)

[root@localhost xiayzn]# nmcli d
设备        类型      状态    连接   
virbr0      bridge    连接的  virbr0 
ens33       ethernet  已断开  --     
lo          loopback  未托管  --     
virbr0-nic  tun       未托管  --   

2、在GUI模式下进行网络配置,在命令行键入命令“ nmtui ”打开网络管理器,选择“编辑”(Edit)后按Enter键

[root@localhost xiayzn]# nmtui

中文版参考左边,英文版参考右边

3、对DHCP进行配置(中文版上图,英文版下图所示)

①在IPv4 CONFIGURATION中选择“ 自动 ”(英文选择“Automatic”)

②选择“自动连接”复选框,如果像我一样没得选的话这一步可以跳过直接进行第③步

③返回并确定,退出网络管理器。

 ④在命令行键入以下命令  systemctl restart network  重新启动网络服务,现在您的服务器将从DHCP获取IP地址

[root@localhost xiayzn]# systemctl restart network

 4、手动配置IP地址

①首先重复第二步进入GUI模式,重复第三步,只是在在IPv4 配置中使用回车键选择“ 手动 ”
②使用子网,网关和DNS服务器添加IP地址(参见下图)
③选择“自动连接”复选框
④返回并确定,退出网络管理器,现在键入以下命令重新启动网络服务(参考第三步的④),这样 Interface 将拥有静态IP

命令模式

步骤1»网络接口配置文件位于/etc/sysconfig/network-scripts /目录中。打开ifcfg-ens33文件(对于接口ens33),您可以看到如下所示的内容。
[root@krizna ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s17
UUID=7f1aff2d-b154-4436-9497-e3a4dedddcef
ONBOOT=no
HWADDR=00:0C:29:A1:B5:D6
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

»DHCP配置

步骤2»对于DHCP 
在配置文件中查找以下行。并通过键入以下命令替换为Now Restart网络服务。现在您的服务器将从DHCP获取IP地址
BOOTPROTO=none
ONBOOT=no

BOOTPROTO=dhcp
ONBOOT=yes

systemctl restart network

»静态配置

第3步»对于静态IP。
在配置文件中找到以下行。并替换为并在文件的末尾添加以下行。更改后文件将如下所示。现在键入以下命令重新启动网络服务。现在Interface将拥有静态IP。此外,您可以使用/ etc / sysconfig / network文件作为主机名和DNS。
BOOTPROTO=none
ONBOOT=no

BOOTPROTO=static
ONBOOT=yes

IPADDR=172.27.0.32
NETMASK=255.255.255.0
GATEWAY=172.27.0.1
DNS1=172.27.0.5

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s17
UUID=f0c5b37d-299a-43cb-b74b-618bb252d129
ONBOOT=yes
HWADDR=00:0C:29:A1:B5:CC
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=192.168.1.5


systemctl restart network

HOSTNAME=server.krizna.com
DNS1=192.168.1.5
DNS2=8.8.8.8
SEARCH=krizna.com

2、如果遇到上述问题,重新执行 yum install python-pip,没遇到则跳过

遇到问题:

[root@localhost xiayzn]# yum install python-pip
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
没有可用软件包 python-pip。
错误:无须任何处理

原因:因为CentOS官方的源有些时候滞后导致的。

解决方法:我们可以用fedora社区打造的EPEL(http://fedoraproject.org/wiki/EPEL)来进行安装

该解决方法参考:https://www.cnblogs.com/magic-zero/p/6512303.html

1、yum -y install epel-release

[root@localhost xiayzn]# yum -y install epel-release
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
正在解决依赖关系
--> 正在检查事务
---> 软件包 epel-release.noarch.0.7-9 将被 安装
--> 解决依赖关系完成

依赖关系解决

=============================================================================================================
 Package                       架构                    版本                    源                       大小
=============================================================================================================
正在安装:
 epel-release                  noarch                  7-9                     extras                   14 k

事务概要
=============================================================================================================
安装  1 软件包

总下载量:14 k
安装大小:24 k
Downloading packages:
警告:/var/cache/yum/x86_64/7/extras/packages/epel-release-7-9.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
epel-release-7-9.noarch.rpm 的公钥尚未安装
epel-release-7-9.noarch.rpm                                                           |  14 kB  00:00:00     
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 检索密钥
导入 GPG key 0xF4A80EB5:
 用户ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 指纹       : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 软件包     : centos-release-7-3.1611.el7.centos.x86_64 (@anaconda)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : epel-release-7-9.noarch                                                                  1/1 
  验证中      : epel-release-7-9.noarch                                                                  1/1 

已安装:
  epel-release.noarch 0:7-9                                                                                  

完毕!

2、再次执行yum install python-pip 

 

[root@localhost xiayzn]# yum  install python-pip
已加载插件:fastestmirror, langpacks
epel/x86_64/metalink                                                                  | 5.6 kB  00:00:00    
epel                                                                                  | 4.3 kB  00:00:00    
(1/3): epel/x86_64/group_gz                                                           | 170 kB  00:00:00    
(2/3): epel/x86_64/updateinfo                                                         | 749 kB  00:00:04    
(3/3): epel/x86_64/primary_db                                                         | 4.6 MB  00:02:25    
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
正在解决依赖关系
--> 正在检查事务
---> 软件包 python2-pip.noarch.0.8.1.2-5.el7 将被 安装
--> 解决依赖关系完成
 
依赖关系解决
 
=============================================================================================================
 Package                     架构                   版本                          源                    大小
=============================================================================================================
正在安装:
 python2-pip                 noarch                 8.1.2-5.el7                   epel                 1.7 M
 
事务概要
=============================================================================================================
安装  1 软件包
 
总下载量:1.7 M
安装大小:7.2 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/epel/packages/python2-pip-8.1.2-5.el7.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
python2-pip-8.1.2-5.el7.noarch.rpm 的公钥尚未安装
python2-pip-8.1.2-5.el7.noarch.rpm                                                    | 1.7 MB  00:00:07    
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 检索密钥
导入 GPG key 0x352C64E5:
 用户ID     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 指纹       : 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 软件包     : epel-release-7-9.noarch (@extras)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : python2-pip-8.1.2-5.el7.noarch                                                           1/1
  验证中      : python2-pip-8.1.2-5.el7.noarch                                                           1/1
 
已安装:
  python2-pip.noarch 0:8.1.2-5.el7                                                                          
 
完毕!

3、安装python库    pip install BeautifulSoup4
4、yum install python-devel
5、pip install mysql-python

注:如果4、5两步安装不成功,可以先执行 yum install mysql-devel  再执行4、5两步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值