CentOS Stream 9 从安装到维护的全过程

47 阅读5分钟

本文介绍 CentOS Stream 9 从安装到维护的完整流程。之前攥写过一份 CentOS7 从安装到维护的全过程。之所以使用 Stream 9 版本,是为了适应更新版本的 NodeJS。

1. 系统

宿主系统: Windows10
虚拟系统: Centos Stream 9
虚拟机软件: VirtualBox 7.1.6
内存大小: 4GB
显存大小: 64MB
存储格式: VHD
存储明细: 动态分配存储
网卡一: NAT
网卡二: Host-Only
共享文件夹自动挂载:
挂载点: /root/share
安装方式: 最小化安装
安装语言: us-en
时区: 亚洲/上海

2. XShell 连接

在系统安装完成后,执行 ip addr 获取当前系统服务地址,即可用该地址 + 账号密码登录 XShell。

image.png

3. 更新 yum 下载源

备份系统默认 yum 下载源:

# 备份 centos.repo
mv /etc/yum.repos.d/centos.repo /etc/yum.repos.d/centos.repo.backup

# 备份 centos-addons.repo
mv /etc/yum.repos.d/centos-addons.repo /etc/yum.repos.d/centos-addons.repo.backup

创建新的 centos.repo

vi /etc/yum.repos.d/centos.repo

输入以下内容:

[baseos]
name=CentOS Stream $releasever - BaseOS
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[baseos-debug]
name=CentOS Stream $releasever - BaseOS - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[baseos-source]
name=CentOS Stream $releasever - BaseOS - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream]
name=CentOS Stream $releasever - AppStream
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[appstream-debug]
name=CentOS Stream $releasever - AppStream - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream-source]
name=CentOS Stream $releasever - AppStream - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb]
name=CentOS Stream $releasever - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[crb-debug]
name=CentOS Stream $releasever - CRB - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb-source]
name=CentOS Stream $releasever - CRB - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

创建新的 centos-addons.repo

vi /etc/yum.repos.d/centos-addons.repo

输入以下内容:

[highavailability]
name=CentOS Stream $releasever - HighAvailability
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[highavailability-debug]
name=CentOS Stream $releasever - HighAvailability - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[highavailability-source]
name=CentOS Stream $releasever - HighAvailability - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv]
name=CentOS Stream $releasever - NFV
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[nfv-debug]
name=CentOS Stream $releasever - NFV - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv-source]
name=CentOS Stream $releasever - NFV - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/NFV/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt]
name=CentOS Stream $releasever - RT
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[rt-debug]
name=CentOS Stream $releasever - RT - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt-source]
name=CentOS Stream $releasever - RT - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/RT/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage]
name=CentOS Stream $releasever - ResilientStorage
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[resilientstorage-debug]
name=CentOS Stream $releasever - ResilientStorage - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage-source]
name=CentOS Stream $releasever - ResilientStorage - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[extras-common]
name=CentOS Stream $releasever - Extras packages
baseurl=https://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/$basearch/extras-common/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[extras-common-source]
name=CentOS Stream $releasever - Extras packages - Source
baseurl=https://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/source/extras-common/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

然后,清除 yum 缓存并更新:

# 清除缓存
yum clean all

# 重新生成缓存
yum makecache

# 更新 yum
yum update

4. 共享文件夹

在设置中的 存储-光驱 选中 VBoxGuestAdditions.iso 后,重新启动虚拟机后挂载 CDROM

# 创建目录
mkdir /mnt/cdrom

# 挂载光驱到该目录
mount /dev/cdrom /mnt/cdrom

# 跳到该目录中
pushd /mnt/cdrom

# 安装增强功能
./VBoxLinuxAdditions.run

不管增强功能安装成功与否,去到系统窗口选择 设备 -> 提升增强功能(Upgrade Guest Additions...) 并点击。点击后没有反应是正常的。

然后就到了设置永久自动挂载这一步:

# 打开开机执行脚本
vi /etc/rc.local

在 rc.local 的最后一行加入下面的内容:

# share 是共享文件夹名称
# /root/share 是挂载点
mount -t vboxsf share /root/share

执行 :wq 保存 rc.local 并退出后,为 rc.local 添加执行权限:

chmod +x /etc/rc.local

重启后即可发现 share 已自动挂载好了。

5. 安装 nvm

安装 nvm 的本质,是为了安装 node 以及 npm

  1. 查看 NVM 最新版本:Releases · nvm-sh/nvm,这里以 v0.40.2 版本为例

  2. 下载 nvm 安装脚本并执行安装

    cd /usr/local/src
    
    # 把 v0.40.2 请替换成你想要的版本
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
    
  3. 重新加载 ~/.bashrc 文件,使得安装后的配置立即生效。即让 nvm 命令在当前终端立即可用

    source ~/.bashrc
    
  4. 以下是 nvm 常用命令

    # 查看所有可用 node 版本
    nvm ls-remote
    
    # 安装需要的 node 版本
    nvm install v18.20.4
    
    # 查看所有已安装 node 版本
    nvm list
    
    # 切换当前 node 为某个已安装版本
    nvm use v18.20.4
    
  5. 安装 node 完成后,就可以执行以下命令查看 node 和 npm 版本

    # 查看 node 版本
    node -v
    
    # 查看 npm 版本
    npm -v
    
  6. 修改 npm 的源为阿里云的源

    vi ~/.npmrc
    

    .npmrc 文件输入以下配置并保存:

    registry=https://registry.npmmirror.com/
    

    查看修改源是否成功:

    npm config get registry
    

6. 安装 MySQL

首先,确保系统是最新的

dnf update -y

然后下载 MySQL 并安装

# 下载 rpm 安装包
dnf install -y https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

# 禁用系统默认的 MySQL 模块
dnf module disable mysql -y

# 安装 MySQL,并且忽略 gpg 检查
dnf install -y mysql-community-server-8.0.32 --nogpgcheck

自启动与启动

# 设置开机自启动
systemctl enable mysqld

# 启动
systemctl start mysqld

启动后获取初始密码:

grep 'temporary password' /var/log/mysqld.log

使用初始密码登录 MySQL 并修改密码

# 打开 MySQL 终端(须输入初始密码)
mysql -u root -p

然后修改初始密码为 123456。我这里修改为 123456 只是为了方便开发,你的服务器密码千万不要做以下修改。你的服务器数据库密码,必须是复杂的不易被破解的密码!

-- 首先使用初始密码通过修改用户验证
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的初始密码';

-- 然后修改密码验证策略为低
SET GLOBAL validate_password.policy = LOW;

-- 再修改密码验证长度为 6
SET GLOBAL validate_password.length = 6;

-- 接着把密码修改为 123456
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

-- 刷新配置,退出后就可以使用 123456 作为 MySQL 的 root 用户登录密码了
FLUSH PRIVILEGES;