前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Python全栈100天学习笔记】Day35 Linux系统软件安装、Shell编程及配置服务工具

【Python全栈100天学习笔记】Day35 Linux系统软件安装、Shell编程及配置服务工具

作者头像
天道Vax的时间宝藏
发布2022-04-02 08:18:59
2580
发布2022-04-02 08:18:59
举报
使用包管理工具
  1. yum - Yellowdog Updater Modified。
    • yum search:搜索软件包,例如yum search nginx
    • yum list installed:列出已经安装的软件包,例如yum list installed | grep zlib
    • yum install:安装软件包,例如yum install nginx
    • yum remove:删除软件包,例如yum remove nginx
    • yum update:更新软件包,例如yum update可以更新所有软件包,而yum update tar只会更新tar。
    • yum check-update:检查有哪些可以更新的软件包。
    • yum info:显示软件包的相关信息,例如yum info nginx
  2. rpm - Redhat Package Manager。
    • 安装软件包:rpm -ivh <packagename>.rpm
    • 移除软件包:rpm -e <packagename>
    • 查询软件包:rpm -qa,例如可以用rpm -qa | grep mysql来检查是否安装了MySQL相关的软件包。

下面以Nginx为例,演示如何使用yum安装软件。

代码语言:javascript
复制
[root ~]# yum -y install nginx
...
Installed:
  nginx.x86_64 1:1.12.2-2.el7
Dependency Installed:
  nginx-all-modules.noarch 1:1.12.2-2.el7
  nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7
  nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
  nginx-mod-http-perl.x86_64 1:1.12.2-2.el7
  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7
  nginx-mod-mail.x86_64 1:1.12.2-2.el7
  nginx-mod-stream.x86_64 1:1.12.2-2.el7
Complete!
[root ~]# yum info nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name        : nginx
Arch        : x86_64
Epoch       : 1
Version     : 1.12.2
Release     : 2.el7
Size        : 1.5 M
Repo        : installed
From repo   : epel
Summary     : A high performance web server and reverse proxy server
URL         : http://nginx.org/
License     : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
            : IMAP protocols, with a strong focus on high concurrency, performance and low
            : memory usage.
[root ~]# nginx -v
nginx version: nginx/1.12.2

移除Nginx。

代码语言:javascript
复制
[root ~]# yum -y remove nginx

下面以MySQL为例,演示如何使用rpm安装软件。要安装MySQL需要先到MySQL官方网站下载对应的RPM文件,当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品,在MySQL被收购后,MySQL的作者重新制作了一个MySQL的分支MariaDB,可以通过yum进行安装。

代码语言:javascript
复制
[root mysql]# ls
mysql-community-client-5.7.22-1.el7.x86_64.rpm
mysql-community-common-5.7.22-1.el7.x86_64.rpm
mysql-community-libs-5.7.22-1.el7.x86_64.rpm
mysql-community-server-5.7.22-1.el7.x86_64.rpm
[root mysql]# yum -y remove mariadb-libs
[root mysql]# yum -y install libaio
[root mysql]#rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
...
[root mysql]#rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
...
[root mysql]#rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
...
[root mysql]#rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
...

说明:由于MySQL和MariaDB的底层依赖库是有冲突的,所以上面我们首先用yum移除了名为mariadb-libs的依赖库并安装了名为libaio支持异步I/O操作的依赖库。关于MySQL和MariaDB之间的关系,可以阅读维基百科上关于MariaDB的介绍。

移除安装的MySQL。

代码语言:javascript
复制
[root ~]# rpm -qa | grep mysql | xargs rpm -e
下载解压配置环境变量

下面以安装MongoDB为例,演示这类软件应该如何安装。

代码语言:javascript
复制
[root ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
--2018-06-21 18:32:53--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 52.85.83.16, 52.85.83.228, 52.85.83.186, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|52.85.83.16|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 100564462 (96M) [application/x-gzip]
Saving to: ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’
100%[==================================================>] 100,564,462  630KB/s   in 2m 9s
2018-06-21 18:35:04 (760 KB/s) - ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’ saved [100564462/100564462]
[root ~]# gunzip mongodb-linux-x86_64-rhel70-3.6.5.tgz
[root ~]# tar -xvf mongodb-linux-x86_64-rhel70-3.6.5.tar
mongodb-linux-x86_64-rhel70-3.6.5/README
mongodb-linux-x86_64-rhel70-3.6.5/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.6.5/MPL-2
mongodb-linux-x86_64-rhel70-3.6.5/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongodump
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongostat
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongotop
mongodb-linux-x86_64-rhel70-3.6.5/bin/bsondump
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoreplay
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongod
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongos
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongo
mongodb-linux-x86_64-rhel70-3.6.5/bin/install_compass
[root ~]# vim .bash_profile
...
PATH=$PATH:$HOME/bin:$HOME/mongodb-linux-x86_64-rhel70-3.6.5/bin
export PATH
...
[root ~]# source .bash_profile
[root ~]# mongod --version
db version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64
[root ~]# mongo --version
MongoDB shell version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

说明:当然也可以通过yum来安装MongoDB,具体可以参照官方网站上给出的说明。

源代码构建安装

配置服务

我们可以Linux系统下安装和配置各种服务,也就是说我们可以把Linux系统打造成数据库服务器、Web服务器、缓存服务器、文件服务器、消息队列服务器等等。Linux下的大多数服务都被设置为守护进程(驻留在系统后台运行,但不会因为服务还在运行而导致Linux无法停止运行),所以我们安装的服务通常名字后面都有一个字母d,它是英文单词daemon的缩写,例如:防火墙服务叫firewalld,我们之前安装的MySQL服务叫mysqld,Apache服务器叫httpd等。在安装好服务之后,可以使用systemctl命令或service命令来完成对服务的启动、停止等操作,具体操作如下所示。

计划任务

网络访问和管理

进程管理

系统诊断

Shell编程

之前我们提到过,Shell是一个连接用户和操作系统的应用程序,它提供了人机交互的界面(接口),用户通过这个界面访问操作系统内核的服务。Shell脚本是一种为Shell编写的脚本程序,我们可以通过Shell脚本来进行系统管理,同时也可以通过它进行文件操作。总之,编写Shell脚本对于使用Linux系统的人来说,应该是一项标配技能。

互联网上有大量关于Shell脚本的相关知识,我不打算再此对Shell脚本做一个全面系统的讲解,我们通过下面的代码来感性的认识下Shell脚本就行了。

例子1:输入两个整数m和n,计算从m到n的整数求和的结果。

代码语言:javascript
复制
#!/usr/bin/bash
printf 'm = '
read m
printf 'n = '
read n
a=$m
sum=0
while [ $a -le $n ]
do
    sum=$[ sum + a ]
    a=$[ a + 1 ]
done
echo '结果: '$sum

例子2:自动创建文件夹和指定数量的文件。

代码语言:javascript
复制
#!/usr/bin/bash
printf '输入文件名: '
read file
printf '输入文件数量(<1000): '
read num
if [ $num -ge 1000 ]
then
    echo '文件数量不能超过1000'
else
    if [ -e $dir -a -d $dir ]
    then
        rm -rf $dir
    else
        if [ -e $dir -a -f $dir ]
        then
            rm -f $dir
        fi
    fi
    mkdir -p $dir
    index=1
    while [ $index -le $num ]
    do
        if [ $index -lt 10 ]
        then
            pre='00'
        elif [ $index -lt 100 ]
        then
            pre='0'
        else
            pre=''
        fi
        touch $dir'/'$file'_'$pre$index
        index=$[ index + 1 ]
    done
fi

例子3:自动安装指定版本的Redis。

代码语言:javascript
复制
#!/usr/bin/bash
install_redis() {
    if ! which redis-server > /dev/null
    then
        cd /root
        wget $1$2'.tar.gz' >> install.log
        gunzip /root/$2'.tar.gz'
        tar -xf /root/$2'.tar'
        cd /root/$2
        make >> install.log
        make install >> install.log
        echo '安装完成'
    else
        echo '已经安装过Redis'
    fi
}

install_redis 'http://download.redis.io/releases/' $1

相关资源

  1. Linux命令行常用快捷键 快捷键功能说明tab自动补全命令或路径Ctrl+a将光标移动到命令行行首Ctrl+e将光标移动到命令行行尾Ctrl+f将光标向右移动一个字符Ctrl+b将光标向左移动一个字符Ctrl+k剪切从光标到行尾的字符Ctrl+u剪切从光标到行首的字符Ctrl+w剪切光标前面的一个单词Ctrl+y复制剪切命名剪切的内容Ctrl+c中断正在执行的任务Ctrl+h删除光标前面的一个字符Ctrl+d退出当前命令行Ctrl+r搜索历史命令Ctrl+g退出历史命令搜索Ctrl+l清除屏幕上所有内容在屏幕的最上方开启一个新行Ctrl+s锁定终端使之暂时无法输入内容Ctrl+q退出终端锁定Ctrl+z将正在终端执行的任务停下来放到后台!!执行上一条命令!数字执行数字对应的历史命令!字母执行最近的以字母打头的命令!$ / Esc+.获得上一条命令最后一个参数Esc+b移动到当前单词的开头Esc+f移动到当前单词的结尾
  2. man查阅命令手册的内容说明 手册中的标题功能说明NAME命令的说明和介绍SYNOPSIS使用该命令的基本语法DESCRIPTION使用该命令的详细描述,各个参数的作用,有时候这些信息会出现在OPTIONS中OPTIONS命令相关参数选项的说明EXAMPLES使用该命令的参考例子EXIT STATUS命令结束的退出状态码,通常0表示成功执行SEE ALSO和命令相关的其他命令或信息BUGS和命令相关的缺陷的描述AUTHOR该命令的作者介绍
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用包管理工具
  • 下载解压配置环境变量
  • 源代码构建安装
  • 配置服务
  • 计划任务
  • 网络访问和管理
  • 进程管理
  • 系统诊断
  • Shell编程
  • 相关资源
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档