本文讲述在 Rocky Linux 8.5 下如何安装 MySQL 5.7 和 PHP 7.4。
LNMP 中的 PHP 用于解析 PHP 程序。架构中,主要是使用 PHP-FPM 进行 PHP 程序与服务器软件之间的通信;而 MySQL 提供数据的高效存储和读取。 PHP 在栈里面是 WordPress 必须唯一的选择,而 MySQL 有个替代方案,那就是 MariaDB,这里只演示 MySQL 的安装。
无论是带 ngx_pagespeed 和 Brotli 的 Nginx 编译安装,还是通过软件包的方式安装 PHP 和 MySQL,都是 LNMP 架构的最核心部分。
前面的几篇文章,也就是 “WordPress系列” 的前六章,完成了 Nginx 及其依赖的编译安装:
[WordPress系列之006] CentOS 7 部署 LNMP 之 OpenResty (Nginx) 带 ngx_pagespeed 和支持 Brotli 的安装
那么完成了本文所有的步骤,LNMP 架构的基本要素也就都已经准备好,接下来是对操作系统和 LNMP 软件层面中对每个要素的优化。
安装 MySQL
因为可用的系统资源少,所以就选了 5.7 社区版本的 MySQL。
而 Rocky Linux 8,因为其里面的软件版本较新,安装 MySQL 5.7 好一点,不过也避免安装 8.0 版本。
Rocky Linux 8.5 安装 MySQL 5.7
Rocky Linux 的默认软件库自带 MySQL 8,不需要额外配置软件源。用过都知道,相对于早期的版本, MySQL 8 正常运行需要的系统资源相对要多。
那么,在当前的 “WordPress系列” 部署的 LNMP 架构中,它的系统环境是低配置的环境, MySQL 8 不适用于这里。
因此,即使是 Rocky Linux 8,也要安装较低版本的 MySQL。这里选用 MySQL 5.7 的版本。
查看当前系统软件库里面的 MySQL 版本
# Rocky Linux 8
dnf list|grep -i "mysql"
[root@host directory]# dnf list|grep -i "mysql"
mysql-server.x86_64 8.0.26-1.module+el8.4.0+652+6de068a7 appstream
说明当前系统软件库中的 MySQL 版本是 8.0,显然这不是想要的版本。
配置 MySQL 5.7 的软件源
在 Rocky Linux 8.5 的命令行下执行以下命令:
在 Vim 编辑器中打开以下的文件(没安装 Vim,那么用 vi 命令):
vi /etc/yum.repos.d/mysql-community.repo
# 按键盘的 ESC 键
# 然后输入法英文状态输入 :set paste
# 按回车,进入粘贴模式,防止格式混乱
# 按 i 进入编辑模式
# 再粘贴以下文本到 Vi
# 至于怎样保存退出文件,请自行百度
应用此软件源需要注意的东西:
- 下面文本中 gpgkey 的值可能随时间消逝而变化,
具体的请参考: https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
-
enabled 表示是否允许安装,这里只允许 5.7 版本。
就复制以下的文本:
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
保存并退出文件。
使软件源的更新生效:
# Rocky Linux 8
# 如果遇到 “Is this ok [y/N]: ”,直接输入 y 然后按回车就行了
dnf clean all
dnf makecache
dnf repolist
禁用 Rocky Linux 8 默认的 MySQL AppStream 流
分别执行以下命令:
# Rocky Linux 8
dnf -y module reset mysql
dnf -y module disable mysql
启用 MySQL 5.7 软件源
# Rocky Linux 8
dnf config-manager --disable mysql-8.0-community
dnf config-manager --enable mysql-5.7-community
## dnf config-manager --enable mysql-connectors-community
## dnf config-manager --enable mysql-tools-community
再次查看当前系统里面的 MySQL 软件版本
# Rocky Linux 8
dnf list|grep -i "mysql"
[root@host directory]# dnf list|grep -i "mysql"
mysql-community-server.x86_64 5.7.37-1.el7 mysql-5.7-community
执行 MySQL 5.7 的安装
# Rocky Linux 8
dnf install -y mysql-community-server
安装完成,查看当前 MySQL 版本
mysql --version
执行结果:
mysql Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using EditLine wrapper
配置 MySQL
- 启动 MySQL:
systemctl start mysqld # 查看启动状态 systemctl status -l mysqld
- 开机启动 MySQL:
systemctl enable mysqld
- 查看 MySQL 5.7 的初始密码(5.6 初始状态下并没有密码):
grep 'A temporary password' /var/log/mysqld.log |tail -1
2022-03-25T10:19:46.170033Z 1 [Note] A temporary password is generated for root@localhost: h:l%NTV4i:La
以上执行结果中最后的部分便是 MySQL 初始密码,即 “h:l%NTV4i:La”。
-
MySQL 设置向导:
如果安装的是 MySQL 5.6,那么刚刚安装完的 MySQL 是没有密码的,首次登录 MySQL 服务端之前需要设置根用户(也就是 root,拥有最高管理权限的 MySQL 用户)密码。
mysql_secure_installation
执行结果:
# 如果是 5.6 版本: enter current password for root (enter for none): [由于是首次设置,直接按回车] # 如果是 5.7 版本: enter current password for root (enter for none): [输入前面的初始密码] ok, successfully used password, moving on... set root password? [y/n] [选 y 设置新的 root 用户密码] new password: re-enter new password: password updated successfully! # 如果是 5.7 版本: # 还会询问: Change the password for root ? ((Press y|Y for Yes, any other key for No) : [直接按回车即可,因为已经完成修改密码] reloading privilege tables.. ... success! remove anonymous users? [y/n] [选 y] ... success! ... 都按自己意愿选择就是了,建议全部选 y。
- 然后使用上一步设置的 root 密码 尝试登录 MySQL:
mysql -uroot -p
enter password: 输入先前设定的密码登录
- 登录成功之后在 MySQL Shell 执行
show variables like '%char%';
获取数据库相关的编码信息:+--------------------------+----------------------------+ | variable_name | value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
由上面的执行结果可以看到有两项编码为 latin1,这显然不支持我们的中文,所以要配置一下 MySQL 使之变成 UTF-8 编码。
-
修改 MySQL 的配置文件以支持中文
追加以下内容到
/etc/my.cnf
文件[mysqld] character-set-server=utf8mb4 [client] default-character-set=utf8mb4
重启 MySQL:
systemctl restart mysqld # 重新登录 MySQL
show variables like '%char%';
这时的结果为:
+--------------------------+----------------------------+ | variable_name | value | +--------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
至此,MySQL 的安装已经完成。
安装 PHP-FPM
PHP-FPM 简介
PHP-FPM,全称 “PHP FastCGI 进程管理器”,它是 PHP 作为 Web 后台编程语言的核心,它代替了 PHP FastCGI 成为 PHP 脚本与 Web 服务器软件进行数据交流的媒介。
安装 PHP(PHP-FPM 包含在内)
选择对应合适的操作系统来安装:
EPEL 国内源版本列表
https://mirrors.tuna.tsinghua.edu.cn/epel/
REMI 国内源版本列表
https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/
- 配置Yum仓库
配置完成,甚至可以安装 PHP 8.0 和 8.1 哦。
# Rocky Linux 8 # 国内源: yum install -y https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-8.noarch.rpm yum install -y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-8.rpm find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s/#baseurl/baseurl/g" find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s/mirrorlist/#mirrorlist/g" find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s@http://rpms.remirepo.net@https://mirrors.tuna.tsinghua.edu.cn/remi@g" find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s@http://cdn.remirepo.net@https://mirrors.tuna.tsinghua.edu.cn/remi@g" find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s@https://rpms.remirepo.net@https://mirrors.tuna.tsinghua.edu.cn/remi@g" find /etc/yum.repos.d/ -name "remi*.repo" | xargs sed -i "s@https://cdn.remirepo.net@https://mirrors.tuna.tsinghua.edu.cn/remi@g" # 境外源: ## yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm ## yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
- 使软件源的更新生效:
# Rocky Linux 8 # 如果遇到 “Is this ok [y/N]: ”,直接输入 y 然后按回车就行了 # 如果此过程中打印有 404 的 URL, # 莫慌,这正常 dnf clean all dnf makecache dnf repolist
- 安装 PHP 7.4
# Rocky Linux 8 dnf -y module enable php:remi-7.4 # 如果询问:“Is this ok [y/N]: ”,直接填入 y dnf install -y php php-fpm php-bcmath php-cli php-common php-dba php-devel php-embedded php-enchant php-gd php-imap php-intl php-ldap php-mbstring php-pecl-mcrypt php-mysqlnd php-odbc php-pdo php-pdo_dblib php-pear.noarch php-pecl-igbinary php-pecl-xdebug php-pgsql php-process php-pspell php-pecl-recode php-snmp php-soap php-tidy php-xml php-xmlrpc php-devel php-pecl-memcached php-pecl-memcache php-pecl-redis5 php-imagick php-gmp php-brotli
- 查看 PHP 的版本:
php --version
执行结果:
PHP 7.4.28 (cli) (built: Feb 15 2022 13:23:10) ( NTS )
- 启动 PHP-FPM:
systemctl start php-fpm
- 查看状态:
systemctl status -l php-fpm
- 设置随系统启动:
systemctl enable php-fpm
后话
本文手把手地完成了 Rocky Linux 8.5 安装 MySQL 5.7 和 PHP 7.4 。
之所以要兼容 Rocky Linux ,是因为 Rocky Linux 作为 RHEL (Red Hat Enterprise Linux)的源码重新编译的下游版本,它完全兼容了红帽(Red Hat)操作系统的软件生态。并且,作为 CentOS 的替代产品,Rocky Linux 有其吸引以前 CentOS 8 用户的天生优势。作者有这个感觉,很可能以后该操作系统(Rocky Linux)会成为主流。
欢迎访问本人的博客和关注微信公众号!
相关文章:
- [WordPress系列之009] 部署 LNMP 之 CentOS 7 安装MySQL 5.7 以及 PHP 7.4
- CentOS 7 通过 Yum 安装最新版本的 MySQL 8 并且做基本的配置
- 更改 MySQL 字符集
- 重置 MariaDB / MySQL 的 root 用户密码
- [MySQL] 解决 MySQL 8.0 启动时出现的 “[Warning] CA certificate /var/lib/mysql/ca.pem is self signed.” 问题
鉴于本人的相关知识储备以及能力有限,本博客的观点和描述如有错漏或是有考虑不周到的地方还请多多包涵,欢迎互相探讨,一起学习,共同进步。
本文章可以转载,但是需要说明来源出处!
本文使用的部分图片来源于网上,若是侵权,请与本文作者联系删除: admin@chengxuzhilu.com