【深度优先学习法】OpenEuler|【深度优先学习法】OpenEuler 安装配置 MySQL 过程涉及的 Linux 细节大起底

OpenEuler 中安装配置 MySQL 与其他系统有所不同, 需要一些手动的配置. 这里整理了在这个过程中涉及的一些 Linux 基础知识

  • 只想看结果
  • 正文开始
    • 修改配置文件: sed 指令
    • 暂时禁用安全策略:SELinux
    • MySQL 服务的本质:Linux Run Level 及其控制 与 自启动
只想看结果
已验证的方法: OpenEuler上MySQL的部署与使用_albert-rabbit的博客-CSDN博客
正文开始
推荐先了解完整的安装过程,再看下面的内容。因为具体步骤将不会详细说明。
修改配置文件: sed 指令 在安装 MySQL 之前需要禁用 SELinux。SELinux 是 Linux 内核的安全策略, 如不禁用可能因为会访问权限的限制导致最后初始化时如下错误:
[root@host-x-x init.d]# mysqld --defaults-file=/etc/my.cnf –initialize 2021-08-10T07:08:26.745109Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.22) starting as process 2916 2021-08-10T07:08:26.798812Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory. 2021-08-10T07:08:26.798989Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2021-08-10T07:08:26.799066Z 0 [ERROR] [MY-010119] [Server] Aborting 2021-08-10T07:08:26.799407Z 0 [System] [MY-010910] [Server] /usr/local/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.22)Source distribution.

方法为:
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

sed 用于对输入到该指令的文件进行处理并输出, 因此 sed 的参数字符串都是为了描述如何处理传入的文件
要理解 sed 指令, 需要知道, sed 指令的使用包括两个部分:参数 flag 和 动作
参考:Linux sed 命令
  • 参数 flag:
    • -e-f 之后的内容为 描述如何处理文件的字符串. 不同的是使用 -f 时, 传入的是文件。处理后会输出到终端,一般会使用输出重定向的写入到一个文件里
    • -i 直接对文件本身进行修改
  • 动作: 文件处理方法字符串的语法, (和 vim 的语法很像?
    • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
      $ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串

    • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
      [root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number' 1 root:x:0:0:root:/root:/bin/bash No 2-5 number 6 sync:x:5:0:sync:/sbin:/bin/sync .....(后面省略).....

    • d :删除
      [root@www ~]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .....(后面省略).....

    • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
    • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
      nl /etc/passwd | sed '/root/p' # 搜索'root'并展示 1root:x:0:0:root:/root:/bin/bash 1root:x:0:0:root:/root:/bin/bash 2daemon:x:1:1:daemon:/usr/sbin:/bin/sh 3bin:x:2:2:bin:/bin:/bin/sh 4sys:x:3:3:sys:/dev:/bin/sh 5sync:x:4:65534:sync:/bin:/bin/sync ....下面忽略

    • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
      sed 's/要被取代的字串/新的字串/g'

暂时禁用安全策略:SELinux 参考:理解Linux下的SELinux
简而言之, SELinux 是 一个 Mandatory Access Control (MAC) 的实现, 建立在 LSM (Linux Security Module) 基础上, 参考了 Flask 的架构设计.
  • MAC: 进程的权限控制并不是简单的 谁可以做什么, 而是检查 进程 和 其请求的资源 的 context , 进而给出相应的安全策略.
  • Flask是一种灵活的操作系统安全架构,并且在Fluke research operating system中得到了实现。Flask的主要特点是把安全策略执行代码和安全策略决策代码,划分成了两个组件。
【深度优先学习法】OpenEuler|【深度优先学习法】OpenEuler 安装配置 MySQL 过程涉及的 Linux 细节大起底
文章图片

  • LSM 在内核数据结构中增加了安全字段,并且在重要的内核代码(系统调用)中增加了hook。可以在hook中注册回调函数对安全字段进行管理,以及执行接入控制。
【深度优先学习法】OpenEuler|【深度优先学习法】OpenEuler 安装配置 MySQL 过程涉及的 Linux 细节大起底
文章图片

SELinux 有 三个运行状态:
  • Disable: 禁用SELinux,不会给任何新资源打Label,如果重新启用的话,将会给资源重新打上Lable,过程会比较缓慢。
  • Permissive:如果违反安全策略,并不会真正的执行拒绝操作,替代的方式是记录一条log信息。
  • Enforcing: 默认模式,SELinux的正常状态,会实际禁用违反策略的操作
分别使用 getenforcesetenforce [0|1] 可以查看和设置 SeLinux 的运行状态.
[root@host-xxx-xxx-xxx-xxx ~]# setenforce 0 [root@host-xxx-xxx-xxx-xxx ~]# getenforce Permissive [root@host-xxx-xxx-xxx-xxx ~]# setenforce 1 [root@host-xxx-xxx-xxx-xxx ~]# getenforce Enforcing [root@host-xxx-xxx-xxx-xxx ~]# setenforce --help usage:setenforce [ Enforcing | Permissive | 1 | 0 ]

MySQL 服务的本质:Linux Run Level 及其控制 与 自启动 MySQL 最终将作为一个 "服务" 在系统中运行, 并在 开机后 自动启动, 那么什么是 linux 中的 服务? 如何实现自启动?
参考
在 Linux 中, /etc/init.d/ (或 /etc/rc.d/init.d) 目录下有启动脚本, 一般称之为 服务.
另一方面, Linux 将系统的运行生命周期抽象为 7 个状态:
  • 0 – System halt i.e the system can be safely powered off with no activity.
  • 1 – Single user mode.
  • 2 – Multiple user mode with no NFS(network file system).
  • 3 – Multiple user mode under the command line interface and not under the graphical user interface.
  • 4 – User-definable.
  • 5 – Multiple user mode under GUI (graphical user interface) and this is the standard runlevel for most of the LINUX based systems.
  • 6 – Reboot which is used to restart the system.
/etc/rc.d 下有7个名为 rcN.d 的目录,对应系统的7个运行级别,其中存储的是一些指向链接文件(类似 windows 中的快捷方式)
[root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d init.drc0.drc1.drc2.drc3.drc4.drc5.drc6.drc.local [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc2.d S64mysql [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc4.d S64mysql [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc5.d S64mysql [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc6.d K36mysql [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc0.d K36mysql [root@host-xxx-xxx-xxx-xxx ~]# ls /etc/rc.d/rc1.d K36mysql

【【深度优先学习法】OpenEuler|【深度优先学习法】OpenEuler 安装配置 MySQL 过程涉及的 Linux 细节大起底】文件的命名方式为:[K|S]+[优先权重]+[服务名], 其中 K 标志终结服务, S 表示开始服务. 这里意为在 2, 3, 4, 5 状态时开启 MySQL 服务, 其他状态关闭

    推荐阅读