Linux云计算 |【第一阶段】SERVICES-DAY5

主要内容:

源码编译安装、rsync同步操作、inotify实时同步、数据库服务基础

实操前骤:(所需tools.tar.gz与users.sql)

1.两台主机设置SELinnx和关闭防火墙

setenforce 0
systemctl stop firewalld.service     //停止防火墙
systemctl disable firewalld.service

2.真机远程复制tools.tar.gz到虚拟机1

[root@localhost 桌面]# scp /linux-soft/1/tools.tar.gz root@192.168.4.7:/root
root@192.168.4.7's password:
tools.tar.gz                                  100%  766KB   6.1MB/s   00:00    

一、源码编译安装介绍

源码编译安装是指从软件的源代码开始,通过手动编译和安装过程来部署软件的一种方法。这种方法通常用于需要高度定制化或最新版本的软件,或者在特定操作系统或硬件平台上没有预编译二进制包的情况下。

1、源码包编译安装

1)源码编译安装的基本步骤如下:

  1. 获取源代码:通常通过下载软件的源代码压缩包(如.tar.gz或.zip文件)或从版本控制系统(如Git)中克隆代码库。

  2. 解压源代码:将下载的源代码压缩包解压到一个目录中。

  3. 配置编译选项:运行配置脚本(通常是./configure),该脚本会检查系统环境,设置编译选项,并生成Makefile文件。配置脚本通常允许用户通过命令行参数指定安装路径、启用或禁用某些功能等。

  4. 编译源代码:运行make命令,该命令会根据Makefile文件中的指令编译源代码。编译过程可能包括编译源文件、链接库文件等步骤。

  5. 安装软件:运行make install命令,该命令会将编译好的二进制文件、库文件、配置文件等安装到系统中指定的目录(通常是/usr/local或用户指定的路径)。

2)源码包相较于RPM软件包的优劣势:

RPM包 定义:RPM软件包是一种预编译的二进制包,通常用于基于Red Hat的Linux发行版(如Fedora、CentOS、RHEL等)。包含了编译好的可执行文件、库文件、配置文件和安装脚本等。

优势

  • 易于安装和管理:RPM包可以通过简单的命令(如rpm -iyum install)进行安装,且支持依赖关系自动解决。
  • 版本控制:RPM包管理系统可以跟踪已安装的软件包及其版本,方便升级和卸载。
  • 一致性:RPM包在特定发行版上编译,确保与系统库和工具的兼容性。
  • 安全性:RPM包可以进行数字签名验证,确保来源的可靠性。

缺点

  • 定制性有限:由于是预编译的二进制包,用户无法轻易修改编译选项或功能。
  • 版本更新滞后:RPM包通常由发行版维护者提供,可能不会立即提供最新版本的软件。

源码包 定义:包含软件的源代码,通常以压缩文件(如.tar.gz或.zip)的形式分发。用户需要手动编译源代码,生成可执行文件和库文件。

优势

  • 定制化:用户可以根据需要定制编译选项,选择启用或禁用某些功能。
  • 最新版本:可以获取和安装软件的最新版本,而不必等待发行版的更新。
  • 平台适应性:可以在没有预编译二进制包的平台上安装软件。

缺点

  • 复杂性:相比使用预编译的二进制包,源码编译安装过程更为复杂,需要用户具备一定的技术知识。
  • 时间消耗:编译过程可能需要较长时间,特别是对于大型软件项目。
  • 依赖管理:可能需要手动解决软件依赖关系,确保所有必要的库和工具都已安装。

总结

  • RPM软件包 适合追求安装简便、系统一致性和安全性的用户,特别是在企业环境中。
  • 源码包 适合需要高度定制化、追求最新版本或在没有预编译包的平台上安装软件的用户。

2、准备编译环境

开源软件多使用C/C++语言开发,需要 gcc、gcc-c++、make(默认安装)等编译工具

例如:

[root@svr7 ~]# yum -y install gcc make     //安装软件包
[root@svr7 ~]# rpm -q gcc
gcc-4.8.5-28.el7.x86_64
[root@svr7 ~]# rpm -q make
make-3.82-23.el7.x86_64
[root@svr7 ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

实操了解实现过程:

步骤1:安装开发工具gcc与make

步骤2:tar解包,释放源代码至指定目录

步骤3:./configure 配置,指定安装目录/功能模块等选项

步骤4:make 编译,生成可执行的二进制程序文件

步骤5:make install安装,将编译好的文件复制到安装目录

注意:./configure、make、make install的步骤必须要在释放源代码指定的目录以相对目录方式(当前目录)下进行脚本运行、编译二进制可执行程序、安装


步骤1:安装开gcc、make

[root@svr7 ~]# yum -y install gcc make

 步骤2:tar解包,释放源代码至指定目录(企业默认/usr/local目录为源代码存放目录)

[root@svr7 ~]# tar -xf /root/tools.tar.gz -C /      //释放压缩包到根目录
[root@svr7 ~]# ls /
1.txt  boot  etc   lib    media  mydvd  proc  run   srv  tmp    usr
bin    dev   home  lib64  mnt    opt    root  sbin  sys  tools  var
[root@svr7 ~]# ls /tools/
inotify-tools-3.13.tar.gz  other
 
[root@svr7 ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /usr/local/   //释放指定目录
[root@svr7 ~]# ls /usr/local/
bin  games    inotify-tools-3.13  lib64    sbin   src
etc  include  lib                 libexec  share
[root@svr7 ~]# ls /usr/local/inotify-tools-3.13/
aclocal.m4    config.h.in   COPYING     libinotifytools  man      src
AUTHORS       config.sub    depcomp     ltmain.sh        missing
ChangeLog     configure     INSTALL     Makefile.am      NEWS
config.guess  configure.ac  install-sh  Makefile.in      README

 步骤3:运行./configure 配置脚本,指定安装目录

  • 作用1:检测当前系统是否安装gcc
  • 作用2:指定安装位置与功能
[root@svr7 ~]# cd /usr/local/inotify-tools-3.13/    //必须以当前目录方式运行脚本
[root@svr7 inotify-tools-3.13]# ./configure --prefix=/opt/myrpm   //指定安装目录
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
...

补充:[--prefix=指定安装目录],此步骤不产生相应的目录(无需提前创建,make创建)

步骤4:进行make 编译,生成可执行的二进制程序文件(放在内存中)

[root@svr7 inotify-tools-3.13]# make
make  all-recursive
make[1]: 进入目录“/usr/local/inotify-tools-3.13”
Making all in libinotifytools
make[2]: 进入目录“/usr/local/inotify-tools-3.13/libinotifytools”
...

补充:make编译,将高级语言转化成低级语言 

步骤5:make install 安装,将编译好的文件复制到安装目录

[root@svr7 inotify-tools-3.13]# make install
Making install in libinotifytools
make[1]: 进入目录“/usr/local/inotify-tools-3.13/libinotifytools”
make[2]: 进入目录“/usr/local/inotify-tools-3.13”
...
[root@svr7 inotify-tools-3.13]# ls /opt/
myrpm  rh
[root@svr7 inotify-tools-3.13]# ls /opt/myrpm/
bin  include  lib  share
[root@svr7 inotify-tools-3.13]# ls /opt/myrpm/bin/
inotifywait  inotifywatch

常见报错:未安装gcc开发工具

checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

二、分步源码编译解析及示例

1)下载及解包(inotify-tools软件工具)

  • 使用tar解包,建议释放到/usr/src/目录(/usr/local)
  • 标准位置:/usr/src/软件名-版本号/

[root@svr7 ~]# yum -y install gcc make
[root@svr7 ~]# tar -xf /root/tools.tar.gz -C / 
[root@svr7 ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /usr/local/

2)配置(./configure)

  • 执行源码目录下的configure脚本,通过“./configure --help”可以查看帮助
  • 典型参数:--prefix=指定安装目录    //不生成目录(make进行生成)
  • 作用:检测当前系统是否安装gcc、指定安装位置与功能 
[root@svr7 ~]# cd /usr/local/inotify-tools-3.13/
[root@svr7 inotify-tools-3.13]# ./configure --prefix=/opt/myrpm

3)编译及安装(make、make install)

  • 命令:make    //编译
  • 命令:make install    //安装
[root@svr7 inotify-tools-3.13]# make && make install

4)结果验证

  • 查看安装目录,了解程序的使用方法;
  • 运行软件包提供的程序/服务
[root@svr7 inotify-tools-3.13]# ls /opt/myrpm/
bin  include  lib  share

三、Rsync基本使用

rsync 是一种快速、多功能的远程(和本地)文件复制工具。它可以在不同主机之间同步文件和目录,无论是通过本地网络还是通过互联网。rsync 以其高效的数据传输和增量传输特性而闻名,它只传输文件中发生变化的部分,而不是整个文件,从而大大减少了传输时间和带宽消耗。

 官网:http://rsync.samba.org/

1、主要特性

  1. 增量传输rsync 会检查文件的变化,只传输变化的部分,而不是整个文件。
  2. 高效传输:通过使用压缩和校验和,rsync 可以减少传输的数据量。
  3. 支持多种传输模式:可以通过 SSH、rsync 守护进程等方式进行传输。
  4. 保留文件属性rsync 可以保留文件的权限、时间戳、软链接等属性。
  5. 灵活的过滤规则:可以使用 include 和 exclude 规则来控制哪些文件和目录需要同步。

复制与同步的差异:

① 复制(cp):完全拷贝源到目标

② 同步(rsync):增量拷贝,只传输变化过的数据

2、Rsync基本用法

格式:rsync [选项...] 源目录 目标目录

例如,要在本地同步两个目录:

rsync -av /source/directory/ /destination/directory/

要在两台远程主机之间同步文件,可以使用 SSH:

rsync -avz /source/directory/ user@remotehost:/destination/directory/

常用选项:

  • -a:归档模式,表示递归传输并保持文件属性(相当于 -rlptgoD)。
  • -v:详细模式,显示传输过程中的详细信息。
  • -z:压缩文件数据在传输过程中。
  • -P:显示传输进度,并在传输中断后可以继续传输。
  • --delete:删除目标目录中源目录没有的文件。
  • -n:测试同步过程,不做实际修改

注意:使用 [-av] 的前后顺序尽量不能颠倒,否则无法进行[Tab]补全命令;

本地目录同步(目录后+[ / ],只同步目录下的数据)

  • 格式:rsync [选项...] 本地源目录 本地目标目录      //同步整个文件夹
  • 格式:rsync [选项...] 本地源目录/ 本地目标目录     //只同步目录下的数据

补充:rsync同步会对源目录进行检验,第一次同步将源目标目录下的所有数据全部拷贝至目标目录;第二次同步将源目录的变化数据拷贝到目标目录

注意:不加选项 [--delete ,只会针对源目录的数据,即使目标目录有其它数据也不会操作;


例如:同步目录本身方式进行同步 [源目录]

[root@svr7 ~]# mkdir /mydir /todir
[root@svr7 ~]# cp /etc/passwd /mydir/
[root@svr7 ~]# touch /mydir/1.txt
[root@svr7 ~]# mkdir /mydir/ABC
[root@svr7 ~]# ls /mydir/      //准备测试需要的目录、文件
1.txt  ABC  passwd
[root@svr7 ~]# rsync -av /mydir /todir    //不带[/],则同步目录本身
sending incremental file list
mydir/
mydir/1.txt
mydir/passwd
mydir/ABC/
sent 2,552 bytes  received 66 bytes  5,236.00 bytes/sec
total size is 2,330  speedup is 0.89

[root@svr7 ~]# ls /todir/     //查看目标目录同步内容
mydir

例如:同步目录内容方式进行同步 [源目录/]

[root@svr7 ~]# rsync -av /mydir/ /todir     //带[/],则同步目录内容
sending incremental file list
./
1.txt
passwd
ABC/
sent 2,540 bytes  received 65 bytes  5,210.00 bytes/sec
total size is 2,330  speedup is 0.89

[root@svr7 ~]# ls /todir/      //查看目标目录同步内容
1.txt  ABC  mydir  passwd

例如:增加文件,进行同步

[root@svr7 ~]# touch /mydir/2.txt      //源目录创建新文件
[root@svr7 ~]# rsync -av /mydir/ /todir/
sending incremental file list
./
2.txt      //只同步变化数据
sent 188 bytes  received 39 bytes  454.00 bytes/sec
total size is 2,330  speedup is 10.26

[root@svr7 ~]# ls /todir/       //查看目标目录同步内容
1.txt  2.txt  ABC  mydir  passwd

例如:修改文件内容,进行同步

[root@svr7 ~]# echo 123 > /mydir/1.txt    //源目录文件发生数据修改
[root@svr7 ~]# rsync -av /mydir/ /todir/
sending incremental file list
1.txt      //只同步变化数据
sent 193 bytes  received 36 bytes  458.00 bytes/sec
total size is 2,334  speedup is 10.19

[root@svr7 ~]# ls /todir/     //查看目标目录同步内容
1.txt  2.txt  ABC  mydir  passwd

例如:源目录与目标目录保持内容一致,进行同步 [--delete]

[root@svr7 ~]# ls /mydir/     //源目录内容
1.txt  2.txt  ABC  passwd
[root@svr7 ~]# ls /todir/      //目标目录内容
1.txt  2.txt  ABC  mydir  passwd
[root@svr7 ~]# rsync -av --delete /mydir/ /todir/  //同步并删除目标目录多余的文档
sending incremental file list
deleting mydir/ABC/
deleting mydir/passwd
deleting mydir/1.txt
deleting mydir/
sent 142 bytes  received 68 bytes  420.00 bytes/sec
total size is 2,334  speedup is 11.11

[root@svr7 ~]# ls /mydir/    //查看源目录同步内容
1.txt  2.txt  ABC  passwd
[root@svr7 ~]# ls /todir/    //查看目标目录同步内容
1.txt  2.txt  ABC  passwd

例如:测试同步过程,不做实际修改 [-n]

[root@svr7 ~]# touch /mydir/c.txt
[root@svr7 ~]# rsync -n -av --delete /mydir/ /todir/
sending incremental file list
./
c.txt
sent 166 bytes  received 23 bytes  378.00 bytes/sec
total size is 2,334  speedup is 12.35 (DRY RUN)

[root@svr7 ~]# ls /todir/    //查看目标目录同步内容
1.txt  2.txt  ABC  passwd

四、Rsync+SSH远程同步(rsync负责同步,ssh负责远程)

用法及服务端要求

  • 开启sshd服务并提供授权的用户、密码

列出SSH服务端资源

  • 命令:rsync user@host:远程目录/

1、与远程的SSH目录保持同步

  • 语法(下行):rsync [...] user@host:远程目录 本地目录
  • 语法(上行):rsync [...] 本地目录 user@host:远程目录

注意:该方式远程同步,需要密码验证

例如1:远程同步(上行)

主机1:

[root@svr7 ~]# rsync -av --delete /mydir/ root@192.168.4.207:/opt
root@192.168.4.207's password:
sending incremental file list
deleting rh/
./
1.txt
2.txt
passwd
ABC/
 
sent 2,615 bytes  received 87 bytes  163.76 bytes/sec
total size is 2,334  speedup is 0.86

主机2:

[root@pc207 ~]# ls /opt/
1.txt  2.txt  ABC  passwd

例如2:远程同步(上行),增加文件

主机1:

[root@svr7 ~]# touch /mydir/c.txt
[root@svr7 ~]# rsync -av --delete /mydir/ root@192.168.4.207:/opt
root@192.168.4.207's password:
sending incremental file list
./
c.txt
 
sent 211 bytes  received 39 bytes  17.24 bytes/sec
total size is 2,334  speedup is 9.34

主机2:

[root@pc207 ~]# ls /opt/
1.txt  2.txt  ABC  c.txt  passwd

例如3:远程同步(下载)

主机2:

[root@pc207 ~]# echo 123 > /mnt/abc.txt

主机1:

[root@svr7 ~]# rsync -av --delete root@192.168.4.207:/mnt/ /opt/
root@192.168.4.207's password:
receiving incremental file list
deleting rh/
deleting myrpm/share/man/man1/inotifywatch.1
...
./
abc.txt

sent 46 bytes  received 116 bytes  12.00 bytes/sec
total size is 4  speedup is 0.02
[root@svr7 ~]# ls /opt/
abc.txt
[root@svr7 ~]# cat /opt/abc.txt
123

注意:进行远程同步下载时,若加 [-delete] 选项,会将/opt目录下的内容与/mnt目录内容保持一致,所以会对原来在/opt目录下的所有文件全部删除;

2、实现SSH无密码验证(公钥与私钥)

部署公钥与私钥,配对具有唯一性,且每次生成公私钥不一样;(RSA加密算法生成)

  • ① 公钥public(锁),对应生成文件id_rsa.pub(公钥)
  • ② 私钥private(钥匙),对应生成文件id_rsa(私钥)
  1. 生成公私钥命令:ssh-keygen
  2. 传递公钥命令:ssh-copy-id user@host    //传递目标主机
  3. 公私钥存放目录:/root/.ssh/
  4. 目标主机存放公钥文件:/root/.ssh/authorized_keys    //每一行记录代表一条公钥

补充:公钥和私钥由远程管理的主机生成,而被远程管理的主机负责接受公钥;


步骤1:生成公钥与私钥

[root@svr7 ~]# ssh-keygen    //生成公钥与私钥(回车即可)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   公私钥保存目录
Enter passphrase (empty for no passphrase):   //公私钥的保险柜密码,不输入为空
Enter same passphrase again:     //确认密码为空
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fnqPfuarIHeeLqDRE6heKLIT+GSjd1cXwjZ8iWd/hkI root@svr7.tedu.cn
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|     . o . .     |
|    . . B E      |
|.  o . oSB o .   |
|= * o +.. o o o  |
|.X o o.++.o. o   |
|+ + o .o.*.oo    |
| o . .  .=B*+.   |
+----[SHA256]-----+
[root@svr7 ~]# ls /root/.ssh/    //查看公私钥存放目录
id_rsa  id_rsa.pub  known_hosts

解释:id_rsa(私钥)、id_rsa.pub(公钥)、known_hosts(记录曾经远程管理过的机器)

补充:在已生成公私钥的情况下,再次对同一主机进行生成,则将原有的公私钥对进行覆盖;

补充:本机可以把公钥对不同的目标主机进行提供,但私钥无法提供给目标主机;其次不同的目标主机都可以接收本机的公钥,当目标主机接受本机的公钥后,本机则可以进行SSH无密码访问目标主机;

步骤2:将公钥传递给主机2(相当于复制)

[root@svr7 ~]# ssh-copy-id root@192.168.4.207    //传递公钥给主机2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.4.207's password:
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'root@192.168.4.207'"
and check to make sure that only the key(s) you wanted were added.

步骤3:验证SSH无密码验证

主机2:

[root@pc207 ~]# ls /root/.ssh/     //查看目标主机的存放公钥文件
authorized_keys    //传递后的公钥文件改名,内容一样

主机1:

[root@svr7 ~]# ssh 192.168.4.207
Last login: Mon Mar 22 08:38:47 2021 from 192.168.4.254   //无需密码认证
[root@pc207 ~]# exit
 
[root@svr7 ~]# rsync -av --delete /mydir/ root@192.168.4.207:/opt  //无需密码验证
sending incremental file list
 
sent 165 bytes  received 13 bytes  15.48 bytes/sec
total size is 2,334  speedup is 13.11

补充:生产环境下,如果发现/root/.ssh/目录下有authorized_keys文件,说明本机有无密码验证;(删除authorized_keys或dd某一行记录)

五、部署监控环境

同步的实时性:

① 按照固定周期定期同步

  • 时间间隔不好固定,同步不及时或资源浪费;
  • 实时性较差;

② Linux内核的inotify机制

  • 提供事件响应式的文件系统通知机制;
  • 安装inotify-tools控制工具可调用此机制实现监控;

1)安装inotify-tools工具

  • 下载地址:http://download.sf.net/inotify-tools/
  • 标准的源码、编译安装即可(/usr/local/inotify-tools-3.13/bin/目录下)

2)inotifywait监控基本用法(inotifywait报告)

格式:inotifywait [选项] 目标文件夹

常用命令选项:

  • [-m]   持续监控(捕获一个事件后不退出)
  • [-r]   递归监控、包括子目录及文件(捕获一个事件后退出)
  • [-q]   减少屏幕输出信息
  • [-e]   指定监视的modfiy、move、create、delete、attrib等事件类别(不加则所有事件都监控)

3)持续跟踪指定文件夹的变化

  1. 要求1:目标文件夹:/opt
  2. 要求2:当文档出现监视的事件时,会立即给出相应提示

例如:

[root@svr7 ~]# mkdir /opt/4.txt
[root@svr7 ~]# /opt/myrpm/bin/inotifywait -rq /opt/     //捕获一个事件则退出
/opt/ CREATE,ISDIR 4.txt

例如:

[root@svr7 ~]# mkdir /opt/4.txt
[root@svr7 ~]# mkdir /opt/5.txt
[root@svr7 ~]# /opt/myrpm/bin/inotifywait -mrq /opt/
/opt/ CREATE,ISDIR 4.txt
/opt/ OPEN,ISDIR 4.txt
/opt/ CLOSE_NOWRITE,CLOSE,ISDIR 4.txt
/opt/ CREATE,ISDIR 5.txt
/opt/ OPEN,ISDIR 5.txt
/opt/ CLOSE_NOWRITE,CLOSE,ISDIR 5.txt

六、配置实时同步

1)inotify与rsync的结合

基本思路:利用while循环来反复检查单次监控结果(书写shell脚本)

脚本:可以运行一个文件,实现某种功能;例如:useradd zhangsan

死循环:使用while循环(条件成立则重复执行)

格式:

     while  条件

     do

          重复执行的操作

     Done

注释:只要条件成立,则执行do和done之间的“重复执行的操作”

2)编写同步脚本

[root@svr7 ~]# vim /etc/rsync.sh    //编写脚本
while /opt/myrpm/bin/inotifywait -rqq /mydir
do
  rsync -a --delete /mydir/ root@192.168.4.207:/opt
done
[root@svr7 ~]# ls -l /etc/rsync.sh
-rw-r--r--. 1 root root 105 3月  22 16:01 /etc/rsync.sh
[root@svr7 ~]# chmod a+x /etc/rsync.sh    //赋予执行权限
[root@svr7 ~]# ls -l /etc/rsync.sh
-rwxr-xr-x. 1 root root 105 3月  22 16:01 /etc/rsync.sh
[root@svr7 ~]# /etc/rsync.sh &     //运行脚本程序并放入后台
[1] 2943
[root@svr7 ~]# jobs -l
[1]+  2943 运行中               /etc/rsync.sh &
[root@svr7 ~]# kill 2943      //停止脚本

3)验证实时同步效果

七、数据库服务基础(数据库管理系统)

  • DB数据库:一批数据的集合,主流的数据库多用来存放关系型表格数据;

(关系型数据:以二维表格记录大量实体的属性信息)

  • DBMS数据库管理系统:用来操作和管理;

Mariadb的基本使用:

1.Linux系统的管理指令不能使用

2.所有的数据库系统指令都必须以[ ; ]结尾

3.数据库系统的指令大部分不支持tab补全


1、部署Maridb数据库服务器

RHEL7中的MariDB软件包:

- 提供服务端有关的系统程序:mariadb-server

- 提供客户端及管理工具:mariadb

例如:

[root@svr7 ~]# yum -y install mariadb-server mariadb    //安装软件包
[root@svr7 ~]# systemctl restart mariadb      //重启服务
[root@svr7 ~]# netstat -anptu | grep :3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      5955/mysqld     

2、访问Mariadb数据库

使用mysql命令,登录交互界面,实现数据库管理操作;

格式:mysql [-u用户名] [-p[密码]]

例如:

[root@svr7 ~]# mysql -uroot     //进入数据库系统(默认免密码)
MariaDB [(none)]>
MariaDB [(none)]> show databases;    //列出数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> quit
Bye

3、数据库的增删查

MariaDB [(none)]> 交互命令:

- [ show databases; ]   列出数据库

- [ create database 数据库名; ]   创建数据库

- [ drop database 数据库名; ]   删除数据库

- [ use 数据库名; ]   使用/选择数据库

- [ show tables; ]   列出库里有哪些表

- exit 和 quit  都能退出

例如:创建数据库,数据库名为nsd01

[root@svr7 ~]# mysql -uroot
MariaDB [(none)]> create database nsd01;     //创建数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;     //列出数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nsd01              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> drop database nsd01;    //删除数据库
Query OK, 0 rows affected (0.00 sec)

例如:切换到mysql数据库并查看数据库表格

[root@svr7 ~]# mysql
MariaDB [(none)]> use mysql;     //切换到mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
MariaDB [mysql]> show tables;     //查看当前库中所有表格
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)
 
MariaDB [mysql]> use test;     //切换到test数据库
Database changed
MariaDB [test]> exit     //退出数据库管理系统
Bye

4、为数据库管理系统的管理员设置密码

 命令:mysqladmin [-u用户名] password '新密码'     //[-u]指的用户为数据库管理员

  • ① Linux系统管理员: 对于Linux系统有最高权限,名字为root,能够登陆Linux系统的用户信息,用/etc/passwd进行储存;
  • ② 数据库系统管理员:对于数据库系统有最高权限,名字为root,能够登陆数据系统的用户信息,用mysql库中user表进行储存;

补充:由于系统当前为root用户,进入数据库管理系统时,默认会用数据库管理员身份root登录,无需密码验证;

例如:设置数据库管理系统的管理员密码,交互式登录

[root@svr7 ~]# mysqladmin -uroot password '123456'    //设置管理员密码
[root@svr7 ~]# mysql -u root -p     //交互式登录
Enter password:        //输入密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
...
MariaDB [(none)]> exit
Bye

例如:非交互式登录

[root@svr7 ~]# mysql -uroot -p123456   //非交互式登录
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
...
MariaDB [(none)]> exit
Bye

5、已知旧密码的情况下,修改新密码

命令:mysqladmin [-u用户名] [-p[旧密码]]

例如:

[root@svr7 ~]# mysqladmin -uroot -p123456 password '321'  //修改密码
[root@svr7 ~]# mysql -u root -p321
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
...
MariaDB [(none)]> exit
Bye

 常见报错:因为数据库管理员设置密码,在输入密码错误及没有输入密码情况下会有报错

[root@svr7 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@svr7 ~]# mysql -u root -p65432
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

6、恢复数据到数据库中   

步骤1:创建nsd20数据库

[root@svr7 ~]# mysql -u root -p321
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
...
MariaDB [(none)]> create database nsd20;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nsd20              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

步骤2:真机拷贝users.sql文件到主机1的/root目录下

[root@localhost 桌面]# scp /root/users.sql root@192.168.4.7:/root
root@192.168.4.7's password:
users.sql                                     100% 2634     2.5MB/s   00:00

步骤3:将users.sql数据库文件导入nsd20数据库中

[root@svr7 ~]# mysql -u root -p321 nsd20 < /root/users.sql  //将数据导入数据库
[root@svr7 ~]# mysql -u root -p321
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
...
MariaDB [(none)]> use nsd20;
Database changed
MariaDB [nsd20]> show tables;
+-----------------+
| Tables_in_nsd20 |
+-----------------+
| base            |
| location        |
+-----------------+
2 rows in set (0.00 sec)

7、表记录基本操作

基本操作:增(insert)、删(delete)、改(update)、查(select)

- 表字段表记录

MariaDB [(none)] > 交互指令

- 格式:select * from [数据库.]表名;

- 格式:select 字段1,字段2... from [数据库.]表名;

- 格式:select ... Where 字段1=’值’ and|or 字段2=’值’


— 查(select)

格式: select 表字段,表字段... from库名.表名;

例如:

[root@svr7 ~]# mysql -uroot
MariaDB [(none)]> use nsd20;
MariaDB [nsd20]> show tables;
+-----------------+
| Tables_in_nsd20 |
+-----------------+
| base            |
| location        |
+-----------------+
2 rows in set (0.00 sec)
MariaDB [nsd20]> select * from base;     //查看base所有表字段内容
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
+------+---------+------------+
5 rows in set (0.00 sec)
 
MariaDB [nsd20]> select * from location;    //查看location所有表字段内容
+------+-----------+
| id   | city      |
+------+-----------+
|    1 | Beijing   |
|    2 | Paris     |
|    3 | Sunnyvale |
|    4 | Berlin    |
|    5 | Sunnyvale |
+------+-----------+
5 rows in set (0.00 sec)

例如:在user库中查看nsd20数据库的base表内容;

MariaDB [nsd20]> use test;
MariaDB [test]> select * from nsd20.base;
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
+------+---------+------------+
5 rows in set (0.00 sec)

例如:根据字段查找

MariaDB [test]> use nsd20;
MariaDB [nsd20]> select id,name from base;
+------+---------+
| id   | name    |
+------+---------+
|    1 | Tom     |
|    2 | Barbara |
|    3 | James   |
|    4 | Smith   |
|    5 | Barbara |
+------+---------+
5 rows in set (0.00 sec)

例如:根据条件查找

MariaDB [nsd20]> select * from base where password='456';
+------+---------+----------+
| id   | name    | password |
+------+---------+----------+
|    2 | Barbara | 456      |
+------+---------+----------+
1 row in set (0.00 sec)
 
MariaDB [nsd20]> select * from base where id='4';
+------+-------+----------+
| id   | name  | password |
+------+-------+----------+
|    4 | Smith | tarena   |
+------+-------+----------+
1 row in set (0.00 sec)
 
MariaDB [nsd20]> select * from base where id='4' and password='123';
Empty set (0.00 sec)
 
MariaDB [nsd20]> select * from base where id='4' or password='123';
+------+-------+----------+
| id   | name  | password |
+------+-------+----------+
|    1 | Tom   | 123      |
|    4 | Smith | tarena   |
+------+-------+----------+

— 增(insert)

格式:insert 表名 values (‘值’,‘值’,‘值’);

例如:增加记录信息

MariaDB [nsd20]> insert base values('10','dc','789');
Query OK, 1 row affected (0.04 sec)
 
MariaDB [nsd20]> insert base values('11','aj','333');
Query OK, 1 row affected (0.01 sec)
 
MariaDB [nsd20]> select * from base;
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
|   10 | dc      | 789        |
|   11 | aj      | 333        |
+------+---------+------------+
7 rows in set (0.00 sec)

— 改(update)

格式:update 表名 set 表字段=’新值’ where 表字段=’值’;

例如:修改记录信息

MariaDB [nsd20]> update base set password='888' where id='11';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
MariaDB [nsd20]> update base set password='250' where name='dc';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
MariaDB [nsd20]> select * from base;
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
|   10 | dc      | 250        |
|   11 | aj      | 888        |
+------+---------+------------+
7 rows in set (0.00 sec)

— 删(delete)

例如:根据条件进行删除

MariaDB [nsd20]> delete from base where id='10';
Query OK, 1 row affected (0.02 sec)
 
MariaDB [nsd20]> use test;
MariaDB [test]> delete from nsd20.base where id='11';
Query OK, 1 row affected (0.02 sec)
 
MariaDB [test]> select * from nsd20.base;
+------+---------+------------+
| id   | name    | password   |
+------+---------+------------+
|    1 | Tom     | 123        |
|    2 | Barbara | 456        |
|    3 | James   | solicitous |
|    4 | Smith   | tarena     |
|    5 | Barbara | pwd123     |
+------+---------+------------+
5 rows in set (0.00 sec)

扩展:rsync常用选项

小结:

本篇章节为 【第一阶段】SERVICES-DAY5 的学习笔记,可以初步了解到 源码编译安装、rsync同步操作、inotify实时同步、数据库服务基础。


Tip:毕竟两个人的智慧大于一个人的智慧,如果你不理解本章节的内容或需要相关笔记、视频,可私信小安,请不要害羞和回避,可以向他人请教,花点时间直到你真正的理解

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值