操作环境

  • 硬件:华为云服务器
  • 操作系统:Rocky Linux 9.0 64bit
  • 服务器软件:nginx 1.24.0,openssl 3.0.7

前提条件

  • nginx已安装好,并开启SSL

相关说明

为了安装WordPress,需要先安装MySQL和PHP。
一般情况,建议先安装MySQL,再安装PHP。安装PHP请参考PHP安装指南
安装MySQL主要有两种安装方法,一是通过yum安装,另一种是通过源码编译安装。笔者建议优先使用yum安装方式,本文将介绍通过yum安装MySQL8.0.33的详细操作步骤。

操作步骤

1.查看是否已经安装了MySQL

使用指令 rpm -qa|grep -i mysql查看是否已经安装了MySQL软件。

[root@hecs-288529 ~]# rpm -qa|grep -i mysql
mysql80-community-release-el9-1.noarch
mysql-community-common-8.0.33-1.el9.x86_64
mysql-community-client-plugins-8.0.33-1.el9.x86_64
mysql-community-libs-8.0.33-1.el9.x86_64
mysql-community-client-8.0.33-1.el9.x86_64
mysql-community-icu-data-files-8.0.33-1.el9.x86_64
mysql-community-server-8.0.33-1.el9.x86_64
php-mysqlnd-8.2.6-1.el9.remi.x86_64
[root@hecs-288529 ~]#

上面是我的服务器 当前安装的MySQL软件包,如果未安装,则不会有输出内容。如果输出内容显示MySQL的版本较低,可使用指令 yum update mysql升级,想重新安装可先卸载,然后再安装。(具体卸载方式略,可自行百度)

2.检查是否存在MySQL的yum源安装包

yum list mysql

我的Rocky Linux内默认没有MySQL的yum源安装包,没有任何输出。如果有输出,且版本正好是最新版(或自己要安装的版本),则请跳过第3步,直接执行第4步。

3.下载MySQL的yum源安装包

官方网站下载MySQL的yum源安装包(MySQL Yum Repository Setup Packages),根据自己的操作系统版本选择相应的文件下载。

因为我的Rocky Linux是一个免费的,开源的,面向服务器的Linux发行版。它是一个CentOS分支和一个与Red Hat Enterprise Linux(RHEL)兼容的发行版。故选择图中的第一个链接Red Hat Enterprise Linux 9 / Oracle Linux 9 (Architecture Independent), RPM Package下载。
![[Pasted image 20230605223407.png]]

用指令 cd /usr/local/src进入安装包存放目录,再使用以下wget指令下载MySQL的yum源安装包。

wget -i -c http://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

[root@hecs-288529 ~]# cd /usr/local/src
[root@hecs-288529 src]# wget -i -c http://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
--2023-05-21 12:51:56--  http://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 69.192.12.46
Connecting to dev.mysql.com (dev.mysql.com)|69.192.12.46|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm [following]
--2023-05-21 12:51:57--  https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|69.192.12.46|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm [following]
--2023-05-21 12:51:57--  https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.46.196.230
Connecting to repo.mysql.com (repo.mysql.com)|23.46.196.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10534 (10K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el9-1.noarch.rpm’

mysql80-community-release-el9-1.noarch 100%[=========================================================================>]  10.29K  --.-KB/s    in 0s

2023-05-21 12:52:01 (174 MB/s) - ‘mysql80-community-release-el9-1.noarch.rpm’ saved [10534/10534]

-c: No such file or directory
No URLs found in -c.
FINISHED --2023-05-21 12:52:01--
Total wall clock time: 5.2s
Downloaded: 1 files, 10K in 0s (174 MB/s)
[root@hecs-288529 src]#

4. 安装mysql源

具体指令如下:

yum -y install mysql80-community-release-el9-1.noarch.rpm

[root@hecs-288529 src]# yum -y install mysql80-community-release-el9-1.noarch.rpm
mysql80-community-release-el9-1.noarch.rpm
[2023-05-21 13:37:44]  
Last metadata expiration check: 2:50:08 ago on Sun 21 May 2023 10:47:36 AM CST.
[2023-05-21 13:37:44]  Dependencies resolved.
[2023-05-21 13:37:44]  =========================================================================================================================================================
[2023-05-21 13:37:45]   Package                                          Architecture                  Version                        Repository                           Size
[2023-05-21 13:37:45]  =========================================================================================================================================================
[2023-05-21 13:37:45]  Installing:
[2023-05-21 13:37:45]   mysql80-community-release                        noarch                        el9-1                          @commandline                         10 k
[2023-05-21 13:37:45]  
[2023-05-21 13:37:45]  Transaction Summary
[2023-05-21 13:37:45]  =========================================================================================================================================================
[2023-05-21 13:37:45]  Install  1 Package
[2023-05-21 13:37:45]  
[2023-05-21 13:37:45]  Total size: 10 k
[2023-05-21 13:37:45]  Installed size: 5.7 k
[2023-05-21 13:37:45]  Downloading Packages:
[2023-05-21 13:37:45]  Running transaction check
[2023-05-21 13:37:45]  Transaction check succeeded.
[2023-05-21 13:37:45]  Running transaction test
[2023-05-21 13:37:45]  Transaction test succeeded.
[2023-05-21 13:37:45]  Running transaction
[2023-05-21 13:37:45]  
  Preparing        :                                                                                                                                 1/1 
[2023-05-21 13:37:45]  
  Installing       : mysql80-community-release-el9-1.noarch [                                                                                      ] 1/1
  Installing       : mysql80-community-release-el9-1.noarch [===========================================                                           ] 1/1
  Installing       : mysql80-community-release-el9-1.noarch [=========================================================                             ] 1/1
  Installing       : mysql80-community-release-el9-1.noarch [=======================================================================               ] 1/1
  Installing       : mysql80-community-release-el9-1.noarch [====================================================================================  ] 1/1
  Installing       : mysql80-community-release-el9-1.noarch                                                                                          1/1 
[2023-05-21 13:37:45]  
  Verifying        : mysql80-community-release-el9-1.noarch                                                                                          1/1 
[2023-05-21 13:37:45]  
[2023-05-21 13:37:45]  Installed:
[2023-05-21 13:37:45]    mysql80-community-release-el9-1.noarch                                                                                                                 
[2023-05-21 13:37:45]  
[2023-05-21 13:37:45]  Complete!

5.检查安装源是否安装成功

执行成功后会在/etc/yum.repos.d/目录下生成三个repo文件:

  • mysql-community-debuginfo.repo
  • mysql-community.repo
  • mysql-community-source.repo
[root@hecs-288529 tmp]# cd /etc/yum.repos.d/
[root@hecs-288529 yum.repos.d]# ls -l
total 68
-rw-r--r-- 1 root root 1525 Feb 14 15:29 epel-next.repo
-rw-r--r-- 1 root root 1621 Apr 15 05:22 epel-next-testing.repo
-rw-r--r-- 1 root root 1459 Feb 14 15:29 epel.repo
-rw-r--r-- 1 root root 1552 Apr 15 05:22 epel-testing.repo
-rw-r--r-- 1 root root  951 Jul  6  2022 mysql-community-debuginfo.repo
-rw-r--r-- 1 root root  827 Jul  6  2022 mysql-community.repo
-rw-r--r-- 1 root root  871 Jul  6  2022 mysql-community-source.repo
-rw-r--r-- 1 root root 1825 Jan  5 21:49 remi-modular.repo
-rw-r--r-- 1 root root 1448 Jan  5 21:49 remi.repo
-rw-r--r-- 1 root root  810 Jan  5 21:49 remi-safe.repo
-rw-r--r-- 1 root root 6640 Feb 14 15:27 rocky-addons.repo
-rw-r--r-- 1 root root  412 Feb 14 15:27 rocky-devel.repo
-rw-r--r-- 1 root root 1161 Apr 27 13:35 rocky-devel.repo.rpmnew
-rw-r--r-- 1 root root 2412 Feb 14 15:27 rocky-extras.repo
-rw-r--r-- 1 root root 2379 Apr 27 13:35 rocky-extras.repo.rpmnew
-rw-r--r-- 1 root root 3432 Feb 14 15:27 rocky.repo
[root@hecs-288529 yum.repos.d]#

6.查看MySQL相关资源

具体指令: yum repolist enabled | grep "mysql.*-community.*"

[root@hecs-288529 yum.repos.d]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community MySQL Connectors Community
mysql-tools-community      MySQL Tools Community
mysql80-community          MySQL 8.0 Community Server
[root@hecs-288529 yum.repos.d]#

7.用yum安装MySQL软件

yum -y install mysql-community-server

[2023-05-21 13:37:45]  [root@hecs-288529 src]# yum -y install mysql-community-server
yum -y install mysql-community-server
[2023-05-21 13:38:17]  
......
[2023-05-21 13:39:51]  
[2023-05-21 13:39:51]  Installed:
[2023-05-21 13:39:51]    libaio-0.3.111-13.el9.x86_64                 mysql-community-client-8.0.33-1.el9.x86_64           mysql-community-client-plugins-8.0.33-1.el9.x86_64  
[2023-05-21 13:39:51]    mysql-community-common-8.0.33-1.el9.x86_64   mysql-community-icu-data-files-8.0.33-1.el9.x86_64   mysql-community-libs-8.0.33-1.el9.x86_64            
[2023-05-21 13:39:51]    mysql-community-server-8.0.33-1.el9.x86_64  
[2023-05-21 13:39:51]  
[2023-05-21 13:39:51]  Complete!

8.启动MySQL服务

启动服务: systemctl start mysqld
查看状态: systemctl status mysqld

[root@hecs-288529 ~]# systemctl start mysqld
[root@hecs-288529 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: active (running) since Wed 2023-05-21 13:40:08 CST; 0 days ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 679 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 1423 (mysqld)
     Status: "Server is operational"
      Tasks: 46 (limit: 23205)
     Memory: 695.7M
        CPU: 25min 32.531s
     CGroup: /system.slice/mysqld.service
             └─1423 /usr/sbin/mysqld

May 21 13:40:01 hecs-288529 systemd[1]: Starting MySQL Server...
May 21 13:40:08 hecs-288529 systemd[1]: Started MySQL Server.
[root@hecs-288529 ~]#

9.查看MySQL超级管理员账号的密码

MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中:

查看指令: grep 'temporary password' /var/log/mysqld.log

[root@hecs-288529 ~]# grep 'temporary password' /var/log/mysqld.log
2023-05-21T05:40:23.986098Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: C8FK-Tqj5e3z

密码如上述指令输出信息所示,当前的初始密码是 C8FK-Tqj5e3z。(说明:该密码已做改动处理,且实际密码早已经修改,此处明码表示仅作为示例。)

10.修改MySQL超级管理员账号的密码

登录指令: mysql -u root -p
输入上一步骤中查看到的密码

修改指令如下:
use mysql;
update user set user.Host='%' where user.User='root';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password-123456';
flush privileges;

第二条指令是修改root的登录主机为 %,表示不受限制。

第三条指令是修改root的登录密码为 root-password-123456,此处因示例使用了简单密码。注意需符合密码控制策略的长度和复杂度要求,建议不要随意修改密码控制策略,降低密码难度。

第四条指令flush privileges 是将当前user和privilige表中的用户信息/权限设置从mysql库中提取到内存里。 当MySQL的用户数据和权限有修改后,希望在”不重启MySQL服务”的情况下直接生效,那么就需要执行这个命令。

[root@hecs-288529 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12873
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password-123456';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

11.windows客户端安装MySQLWorkBench8.0(可选)

为了方便使用图形界面操作维护MySQL数据库服务器,可以到MySQL官网下载并安装MySQLWorkBench8.0。

安装完成以后,运行MySQLWorkBench8.0,点击主界面 MYSQL Connections后面的 +,新建server connections配置文件,在弹出的配置窗口界面输入Hostname、Port、Username、Password四个参数的值,点击 Test Connection按钮,即可测试远程连接MySQL服务器。
如出现报错“无法连接到服务器”,一般是因为云服务器防火墙设置或云服务器的安全组配置问题,可参考[[Linux server MySQL远程访问问题]]。