Skip to content

Commit 0da68f5

Browse files
committed
更新了Linux和数据库文档
1 parent 243c42c commit 0da68f5

File tree

2 files changed

+477
-199
lines changed

2 files changed

+477
-199
lines changed

Day31-35/玩转Linux操作系统.md

+235-44
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,45 @@ Linux系统的命令通常都是如下所示的格式:
655655
#### 磁盘管理
656656
657657
1. 列出文件系统的磁盘使用状况 - **df**。
658+
659+
```Shell
660+
661+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# df -h
662+
Filesystem Size Used Avail Use% Mounted on
663+
/dev/vda1 40G 5.0G 33G 14% /
664+
devtmpfs 486M 0 486M 0% /dev
665+
tmpfs 497M 0 497M 0% /dev/shm
666+
tmpfs 497M 356K 496M 1% /run
667+
tmpfs 497M 0 497M 0% /sys/fs/cgroup
668+
tmpfs 100M 0 100M 0% /run/user/0
669+
```
670+
658671
2. 磁盘分区表操作 - **fdisk**。
672+
673+
```Shell
674+
675+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# fdisk -l
676+
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
677+
Units = sectors of 1 * 512 = 512 bytes
678+
Sector size (logical/physical): 512 bytes / 512 bytes
679+
I/O size (minimum/optimal): 512 bytes / 512 bytes
680+
Disk label type: dos
681+
Disk identifier: 0x000a42f4
682+
Device Boot Start End Blocks Id System
683+
/dev/vda1 * 2048 83884031 41940992 83 Linux
684+
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
685+
Units = sectors of 1 * 512 = 512 bytes
686+
Sector size (logical/physical): 512 bytes / 512 bytes
687+
I/O size (minimum/optimal): 512 bytes / 512 bytes
688+
```
689+
659690
3. 格式化文件系统 - **mkfs**。
691+
660692
4. 文件系统检查 - **fsck**。
693+
661694
5. 挂载/卸载 - **mount** / **umount**。
662695
663-
### 编辑器vim
696+
### 编辑器 - vim
664697
665698
1. 启动vim。可以通过`vi`或`vim`命令来启动vim,启动时可以指定文件名来打开一个文件,如果没有指定文件名,也可以在保存的时候指定文件名。
666699
@@ -759,27 +792,19 @@ Linux系统的命令通常都是如下所示的格式:
759792
760793
![](./res/vim-macro.png)
761794
762-
### 环境变量
763-
764-
1. HOME
765-
2. SHELL
766-
3. HISTSIZE
767-
4. RANDOM
768-
5. PATH
769-
770795
### 软件安装和配置
771796
772797
#### 使用包管理工具
773798
774-
1. yum - Yellowdog Updater Modified。
799+
1. **yum** - Yellowdog Updater Modified。
775800
- `yum search`:搜索软件包,例如`yum search nginx`。
776801
- `yum list installed`:列出已经安装的软件包,例如`yum list installed | grep zlib`。
777802
- `yum install`:安装软件包,例如`yum install nginx`。
778803
- `yum remove`:删除软件包,例如`yum remove nginx`。
779804
- `yum update`:更新软件包,例如`yum update`可以更新所有软件包,而`yum update tar只会更新tar。
780805
- `yum check-update`:检查有哪些可以更新的软件包。
781806
- `yum info`:显示软件包的相关信息,例如`yum info nginx`。
782-
2. rpm - Redhat Package Manager。
807+
2. **rpm** - Redhat Package Manager。
783808
- 安装软件包:`rpm -ivh <packagename>.rpm`。
784809
- 移除软件包:`rpm -e <packagename>`。
785810
- 查询软件包:`rpm -qa`,例如可以用`rpm -qa | grep mysql`来检查是否安装了MySQL相关的软件包。
@@ -823,6 +848,14 @@ Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, P
823848
nginx version: nginx/1.12.2
824849
```
825850
851+
移除Nginx。
852+
853+
```Shell
854+
855+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# nginx -s stop
856+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y remove nginx
857+
```
858+
826859
下面以MySQL为例,演示如何使用rpm安装软件。要安装MySQL需要先到[MySQL官方网站](https://www.mysql.com/)下载对应的[RPM文件](https://dev.mysql.com/downloads/mysql/),当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品,在MySQL被收购后,MySQL的作者重新制作了一个MySQL的分支MariaDB,可以通过yum进行安装。如果要安装MySQL需要先通过yum删除`mariadb-libs`这个可能会跟MySQL底层库冲突的库,然后还需要安装一个名为`libaio`的依赖库。
827860
828861
```Shell
@@ -834,26 +867,19 @@ mysql-community-libs-5.7.22-1.el7.x86_64.rpm
834867
mysql-community-server-5.7.22-1.el7.x86_64.rpm
835868
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y remove mariadb-libs
836869
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y install libaio
837-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm
838-
warning: mysql-community-common-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
839-
Preparing... ################################# [100%]
840-
package mysql-community-common-5.7.22-1.el7.x86_64 is already installed
841-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
842-
warning: mysql-community-libs-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
843-
Preparing... ################################# [100%]
844-
package mysql-community-libs-5.7.22-1.el7.x86_64 is already installed
845-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm
870+
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# ls | xargs rpm -ivh
846871
warning: mysql-community-client-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
847872
Preparing... ################################# [100%]
848-
package mysql-community-client-5.7.22-1.el7.x86_64 is already installed
849-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm
850-
warning: mysql-community-server-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
851-
Preparing... ################################# [100%]
852-
package mysql-community-server-5.7.22-1.el7.x86_64 is already installed
853-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# mysqld --version
854-
mysqld Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL))
855-
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# mysql --version
856-
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
873+
...
874+
```
875+
876+
> 说明:由于MySQL和[MariaDB](https://mariadb.org/)的底层依赖库是有冲突的,所以上面我们首先用`yum`移除了名为mariadb-libs的依赖库并安装了名为libaio的依赖库。由于我们将安装MySQL所需的rpm文件放在一个独立的目录中,所以可以通过`ls`命令查看到安装文件并用`xargs`将`ls`的输出作为参数交给`rpm -ivh`来进行安装。关于MySQL和MariaDB之间的关系,可以阅读[维基百科](https://zh.wikipedia.org/wiki/MariaDB)上关于MariaDB的介绍。
877+
878+
移除安装的MySQL。
879+
880+
```Shell
881+
882+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rpm -qa | grep mysql | xargs rpm -e
857883
```
858884
859885
#### 下载解压配置环境变量
@@ -957,15 +983,6 @@ build environment:
957983
redis-cli 3.2.12
958984
```
959985
960-
### 进程和性能
961-
962-
1. top
963-
2. pmap
964-
3. sar
965-
4. free
966-
5. iostat
967-
6. gprof
968-
969986
### 配置服务
970987
971988
1. 启动服务。
@@ -1014,16 +1031,45 @@ build environment:
10141031
10151032
```Shell
10161033
1017-
1034+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# crontab -e
1035+
* * * * * echo "hello, world!" >> /root/hello.txt
1036+
59 23 * * * rm -f /root/*.log
10181037
```
1038+
> 说明:输入`crontab -e`命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出`hello, world!`;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用谷歌或百度搜索一下,也可以使用Cron表达式在线生成器来生成Cron表达式。
10191039
10201040
2. crontab相关文件。
10211041
10221042
```Shell
10231043
1024-
1044+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cd /etc
1045+
[root@iZwz97tbgo9lkabnat2lo8Z etc]# ls -l | grep cron
1046+
-rw-------. 1 root root 541 Aug 3 2017 anacrontab
1047+
drwxr-xr-x. 2 root root 4096 Mar 27 11:56 cron.d
1048+
drwxr-xr-x. 2 root root 4096 Mar 27 11:51 cron.daily
1049+
-rw-------. 1 root root 0 Aug 3 2017 cron.deny
1050+
drwxr-xr-x. 2 root root 4096 Mar 27 11:50 cron.hourly
1051+
drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.monthly
1052+
-rw-r--r-- 1 root root 493 Jun 23 15:09 crontab
1053+
drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.weekly
1054+
[root@iZwz97tbgo9lkabnat2lo8Z etc]# vim crontab
1055+
1 SHELL=/bin/bash
1056+
2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
1057+
3 MAILTO=root
1058+
4
1059+
5 # For details see man 4 crontabs
1060+
6
1061+
7 # Example of job definition:
1062+
8 # .---------------- minute (0 - 59)
1063+
9 # | .------------- hour (0 - 23)
1064+
10 # | | .---------- day of month (1 - 31)
1065+
11 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
1066+
12 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
1067+
13 # | | | | |
1068+
14 # * * * * * user-name command to be executed
10251069
```
10261070
1071+
通过修改`/etc`目录下的crontab文件也能够定制计划任务。
1072+
10271073
### 网络访问和管理
10281074
10291075
1. 通过网络获取资源 - **wget**。
@@ -1116,13 +1162,158 @@ build environment:
11161162

11171163
- `bye`/`exit`/`quit`:退出sftp。
11181164

1165+
### 进程管理
1166+
1167+
1. **ps** - 查询进程。
1168+
1169+
```Shell
1170+
1171+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ps -ef
1172+
UID PID PPID C STIME TTY TIME CMD
1173+
root 1 0 0 Jun23 ? 00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
1174+
root 2 0 0 Jun23 ? 00:00:00 [kthreadd]
1175+
...
1176+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ps -ef | grep mysqld
1177+
root 4943 4581 0 22:45 pts/0 00:00:00 grep --color=auto mysqld
1178+
mysql 25257 1 0 Jun25 ? 00:00:39 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
1179+
```
1180+
1181+
2. **kill** - 终止进程。
1182+
1183+
```Shell
1184+
1185+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# kill 1234
1186+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# kill -9 1234
1187+
```
1188+
1189+
例子:用一条命令强制终止正在运行的Redis进程。
1190+
1191+
```Shell
1192+
1193+
ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill
1194+
```
1195+
1196+
3. 将进程置于后台运行。
1197+
1198+
- `Ctrl+Z`
1199+
- `&`
1200+
1201+
```Shell
1202+
1203+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mongod &
1204+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# redis-server
1205+
...
1206+
^Z
1207+
[4]+ Stopped redis-server
1208+
```
1209+
1210+
4. **jobs** - 查询后台进程。
1211+
1212+
```Shell
1213+
1214+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# jobs
1215+
[2] Running mongod &
1216+
[3]- Stopped cat
1217+
[4]+ Stopped redis-server
1218+
```
1219+
1220+
5. **bg** - 让进程在后台继续运行。
1221+
1222+
```Shell
1223+
1224+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# bg %4
1225+
[4]+ redis-server &
1226+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# jobs
1227+
[2] Running mongod &
1228+
[3]+ Stopped cat
1229+
[4]- Running redis-server &
1230+
```
1231+
1232+
6. **fg** - 将后台进程置于前台。
1233+
1234+
```Shell
1235+
1236+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# fg %4
1237+
redis-server
1238+
^C5554:signal-handler (1530025281) Received SIGINT scheduling shutdown...
1239+
5554:M 26 Jun 23:01:21.413 # User requested shutdown...
1240+
5554:M 26 Jun 23:01:21.413 * Saving the final RDB snapshot before exiting.
1241+
5554:M 26 Jun 23:01:21.415 * DB saved on disk
1242+
5554:M 26 Jun 23:01:21.415 # Redis is now ready to exit, bye bye...
1243+
```
1244+
1245+
> 说明:置于前台的进程可以使用`Ctrl+C`来终止它。
1246+
1247+
7. **top** - 进程监控。
1248+
1249+
```Shell
1250+
1251+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# top
1252+
top - 23:04:23 up 3 days, 14:10, 1 user, load average: 0.00, 0.01, 0.05
1253+
Tasks: 65 total, 1 running, 64 sleeping, 0 stopped, 0 zombie
1254+
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
1255+
KiB Mem : 1016168 total, 191060 free, 324700 used, 500408 buff/cache
1256+
KiB Swap: 0 total, 0 free, 0 used. 530944 avail Mem
1257+
...
1258+
```
1259+
1260+
### 系统性能
1261+
1262+
1. 查看系统活动信息 - **sar**
1263+
1264+
2. 查看内存使用情况 - **free**
1265+
1266+
```Shell
1267+
1268+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# free
1269+
total used free shared buff/cache available
1270+
Mem: 1016168 323924 190452 356 501792 531800
1271+
Swap: 0 0 0
1272+
```
1273+
1274+
3. 查看进程使用内存状况 - **pmap**
1275+
1276+
```Shell
1277+
1278+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ps
1279+
PID TTY TIME CMD
1280+
4581 pts/0 00:00:00 bash
1281+
5664 pts/0 00:00:00 ps
1282+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# pmap 4581
1283+
4581: -bash
1284+
0000000000400000 884K r-x-- bash
1285+
00000000006dc000 4K r---- bash
1286+
00000000006dd000 36K rw--- bash
1287+
00000000006e6000 24K rw--- [ anon ]
1288+
0000000001de0000 400K rw--- [ anon ]
1289+
00007f82fe805000 48K r-x-- libnss_files-2.17.so
1290+
00007f82fe811000 2044K ----- libnss_files-2.17.so
1291+
...
1292+
```
1293+
1294+
4. 报告设备CPU和I/O统计信息 - **iostat**
1295+
1296+
```Shell
1297+
1298+
[root@iZwz97tbgo9lkabnat2lo8Z ~]# iostat
1299+
Linux 3.10.0-693.11.1.el7.x86_64 (iZwz97tbgo9lkabnat2lo8Z) 06/26/2018 _x86_64_ (1 CPU)
1300+
avg-cpu: %user %nice %system %iowait %steal %idle
1301+
0.79 0.00 0.20 0.04 0.00 98.97
1302+
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
1303+
vda 0.85 6.78 21.32 2106565 6623024
1304+
vdb 0.00 0.01 0.00 2088 0
1305+
```
11191306

11201307
### Shell和Shell编程
11211308

1309+
#### 环境变量
1310+
1311+
1. HOME
1312+
2. SHELL
1313+
3. HISTSIZE
1314+
4. RANDOM
1315+
5. PATH
1316+
11221317

11231318

1124-
### 其他内容
11251319

1126-
1. awk
1127-
2. sed
1128-
3. xargs

0 commit comments

Comments
 (0)