centos 8 安装 mysql 8,每一步操作清晰可见

327 阅读9分钟

这是我参与8月更文挑战的第7天,活动详情查看:8月更文挑战

正文

1. 准备 mysql rpm 包

mysql-community-client-plugins-8.0.24-1.el8.x86_64.rpm

mysql-community-libs-8.0.24-1.el8.x86_64.rpm

mysql-community-common-8.0.24-1.el8.x86_64.rpm

mysql-community-client-8.0.24-1.el8.x86_64.rpm

mysql-community-server-8.0.24-1.el8.x86_64.rpm

2. 安装 mysql-community-client

[root@node2 hadoop]# ll
总用量 70092
-rwxr-xr-x. 1 hadoop hadoop 14052636 6月  19 19:28 mysql-community-client-8.0.24-1.el8.x86_64.rpm
-rwxr-xr-x. 1 hadoop hadoop   100356 6月  19 19:41 mysql-community-client-plugins-8.0.24-1.el8.x86_64.rpm
-rwxr-xr-x. 1 hadoop hadoop   632772 6月  19 19:28 mysql-community-common-8.0.24-1.el8.x86_64.rpm
-rwxr-xr-x. 1 hadoop hadoop  1497724 6月  19 19:37 mysql-community-libs-8.0.24-1.el8.x86_64.rpm
-rwxr-xr-x. 1 hadoop hadoop 55483196 3月  23 19:55 mysql-community-server-8.0.24-1.el8.x86_64.rpm
[root@node2 hadoop]# rpm -ivh mysql-community-common-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-common-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
        软件包 mysql-community-common-8.0.24-1.el8.x86_64 已经安装
[root@node2 hadoop]# rpm -ivh mysql-community-client-plugins-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-client-plugins-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-plugins-8.################################# [100%]
[root@node2 hadoop]# rpm -ivh mysql-community-libs-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-libs-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-8.0.24-1.el8################################# [100%]
[root@node2 hadoop]# rpm -ivh mysql-community-client-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-client-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-8.0.24-1.e################################# [100%]
[root@node2 hadoop]# rpm -ivh mysql-community-server-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-server-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
        /usr/bin/perl 被 mysql-community-server-8.0.24-1.el8.x86_64 需要
        net-tools 被 mysql-community-server-8.0.24-1.el8.x86_64 需要

需要先安装 perl 和 net-tools

3. 安装 perl

[root@node2 hadoop]# yum -y install perl
上次元数据过期检查:1:34:21 前,执行于 2021年06月19日 星期六 18时09分31秒。
依赖关系解决。
====================================================================================================================================================================================
 软件包                                             架构                         版本                                                         仓库                             大小
====================================================================================================================================================================================
安装:
 perl                                               x86_64                       4:5.26.3-419.el8                                             appstream                        73 k
安装依赖关系:
 dwz                                                x86_64                       0.12-9.el8                                                   appstream                       109 k
 efi-srpm-macros                                    noarch                       3-3.el8                                                      appstream                        22 k
 ghc-srpm-macros                                    noarch                       1.4.2-7.el8                                                  appstream                       9.3 k
 go-srpm-macros                                     noarch                       2-17.el8                                                     appstream                        13 k
 make                                               x86_64                       1:4.2.1-10.el8                                               baseos                          498 k

## 省略
......

完毕!

4. 安装 net-tools

[root@node2 hadoop]# yum -y install net-tools
上次元数据过期检查:1:43:32 前,执行于 2021年06月19日 星期六 18时09分31秒。
依赖关系解决。
====================================================================================================================================================================================
 软件包                                   架构                                  版本                                                    仓库                                   大小
====================================================================================================================================================================================
安装:
 net-tools                                x86_64                                2.0-0.52.20160912git.el8                                baseos                                322 k

事务概要
====================================================================================================================================================================================
安装  1 软件包

总下载:322 k
安装大小:942 k
下载软件包:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm                                                                                                       827 kB/s | 322 kB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                 80 kB/s | 322 kB     00:04     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                                                                                     1/1 
  安装    : net-tools-2.0-0.52.20160912git.el8.x86_64                                                                                                                           1/1 
  运行脚本: net-tools-2.0-0.52.20160912git.el8.x86_64                                                                                                                           1/1 
  验证    : net-tools-2.0-0.52.20160912git.el8.x86_64                                                                                                                           1/1 

已安装:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                                                                                                                         

完毕!

5. 安装 mysql-community-server

[root@node2 hadoop]# rpm -ivh mysql-community-server-8.0.24-1.el8.x86_64.rpm 
警告:mysql-community-server-8.0.24-1.el8.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-8.0.24-1.e################################# [100%]
[/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.

6. 查看 mysql 是否安装成功

[root@node2 hadoop]# rpm -qa | grep -i mysql
mysql-community-libs-8.0.24-1.el8.x86_64
mysql-community-client-plugins-8.0.24-1.el8.x86_64
mysql-community-client-8.0.24-1.el8.x86_64
mysql-community-server-8.0.24-1.el8.x86_64
mysql-community-common-8.0.24-1.el8.x86_64
[root@node2 hadoop]# mysqladmin --version
mysqladmin  Ver 8.0.24 for Linux on x86_64 (MySQL Community Server - GPL)

7. 初始化 mysql

[root@node2 hadoop]# mysqld --initialize //创建数据文件目录和mysql系统数据库 产生随机root密码

8. 启动 mysql 服务

[root@node2 hadoop]# systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.

又出错了 -_-||

解决方案

这是因为/var/lib/mysql目录权限不够

[root@node2 hadoop]# cd /var/lib/mysql
[root@node2 mysql]# ll
总用量 167348
-rw-r-----. 1 root  root        56 6月  19 20:00  auto.cnf
-rw-r-----. 1 mysql mysql        0 6月  19 20:01  binlog.index
-rw-------. 1 root  root      1676 6月  19 20:00  ca-key.pem
-rw-r--r--. 1 root  root      1112 6月  19 20:00  ca.pem
-rw-r--r--. 1 root  root      1112 6月  19 20:00  client-cert.pem
-rw-------. 1 root  root      1676 6月  19 20:00  client-key.pem
-rw-r-----. 1 root  root    196608 6月  19 20:00 '#ib_16384_0.dblwr'
-rw-r-----. 1 root  root   8585216 6月  19 20:00 '#ib_16384_1.dblwr'
-rw-r-----. 1 root  root      3595 6月  19 20:00  ib_buffer_pool
-rw-r-----. 1 root  root  12582912 6月  19 20:00  ibdata1
-rw-r-----. 1 root  root  50331648 6月  19 20:00  ib_logfile0
-rw-r-----. 1 root  root  50331648 6月  19 20:00  ib_logfile1
drwxr-x---. 2 root  root         6 6月  19 20:00 '#innodb_temp'
drwxr-x---. 2 root  root         6 6月  19 20:00  mysql
-rw-r-----. 1 root  root  15728640 6月  19 20:00  mysql.ibd
drwxr-x---. 2 root  root      8192 6月  19 20:00  performance_schema
-rw-------. 1 root  root      1676 6月  19 20:00  private_key.pem
-rw-r--r--. 1 root  root       452 6月  19 20:00  public_key.pem
-rw-r--r--. 1 root  root      1112 6月  19 20:00  server-cert.pem
-rw-------. 1 root  root      1676 6月  19 20:00  server-key.pem
-rw-r-----. 1 root  root  16777216 6月  19 20:00  undo_001
-rw-r-----. 1 root  root  16777216 6月  19 20:00  undo_002

修改权限并启动 mysql

[root@node2 ~]# setenforce 0 
[root@node2 ~]# chown -R mysql:mysql /var/lib/mysql
[root@node2 ~]# chmod -R 777 /var/lib/mysql
[root@node2 ~]# systemctl start mysqld.service
[root@node2 ~]# ps -ef |grep mysql
mysql      26627       1  4 23:57 ?        00:00:00 /usr/sbin/mysqld
root       26671   10438  0 23:57 pts/0    00:00:00 grep --color=auto mysql
[root@node2 ~]# cd /var/lib/mysql
[root@node2 mysql]# ll
总用量 190916
-rw-r-----. 1 mysql mysql       56 6月  19 23:57  auto.cnf
-rw-r-----. 1 mysql mysql      156 6月  19 23:57  binlog.000001
-rw-r-----. 1 mysql mysql       16 6月  19 23:57  binlog.index
-rwxrwxrwx. 1 mysql mysql     1680 6月  19 23:50  ca-key.pem
-rwxrwxrwx. 1 mysql mysql     1112 6月  19 23:50  ca.pem
-rwxrwxrwx. 1 mysql mysql     1112 6月  19 23:50  client-cert.pem
-rwxrwxrwx. 1 mysql mysql     1676 6月  19 23:50  client-key.pem
-rwxrwxrwx. 1 mysql mysql   196608 6月  19 23:57 '#ib_16384_0.dblwr'
-rwxrwxrwx. 1 mysql mysql  8585216 6月  19 23:50 '#ib_16384_1.dblwr'
-rwxrwxrwx. 1 mysql mysql     6059 6月  19 23:50  ib_buffer_pool
-rwxrwxrwx. 1 mysql mysql 12582912 6月  19 23:57  ibdata1
-rwxrwxrwx. 1 mysql mysql 50331648 6月  19 23:57  ib_logfile0
-rwxrwxrwx. 1 mysql mysql 50331648 6月  19 23:50  ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 6月  19 23:57  ibtmp1
drwxrwxrwx. 2 mysql mysql      187 6月  19 23:57 '#innodb_temp'
drwxrwxrwx. 2 mysql mysql      143 6月  19 23:50  mysql
-rwxrwxrwx. 1 mysql mysql 27262976 6月  19 23:57  mysql.ibd
srwxrwxrwx. 1 mysql mysql        0 6月  19 23:57  mysql.sock
-rw-------. 1 mysql mysql        6 6月  19 23:57  mysql.sock.lock
drwxrwxrwx. 2 mysql mysql     8192 6月  19 23:50  performance_schema
-rwxrwxrwx. 1 mysql mysql     1676 6月  19 23:50  private_key.pem
-rwxrwxrwx. 1 mysql mysql      452 6月  19 23:50  public_key.pem
-rwxrwxrwx. 1 mysql mysql     1112 6月  19 23:50  server-cert.pem
-rwxrwxrwx. 1 mysql mysql     1680 6月  19 23:50  server-key.pem
drwxrwxrwx. 2 mysql mysql       28 6月  19 23:50  sys
-rwxrwxrwx. 1 mysql mysql 16777216 6月  19 23:57  undo_001
-rwxrwxrwx. 1 mysql mysql 16777216 6月  19 23:57  undo_002
[root@node2 mysql]# 

注意事项

  1. setenforce 0 用来解决 [InnoDB] Operating system error number 13 in a file operation. 这个问题的
  2. 目录权限必须设置成 777,只设置成 755 也会出错

成功解决

[root@node2 ~]# systemctl start mysqld.service
[root@node2 ~]# 

9. 验证

[root@node2 ~]# ps -ef |grep mysql
mysql      26627       1  4 23:57 ?        00:00:00 /usr/sbin/mysqld
root       26671   10438  0 23:57 pts/0    00:00:00 grep --color=auto mysql
[root@node2 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-06-19 23:57:02 CST; 11min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 26603 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 26627 (mysqld)
   Status: "Server is operational"
    Tasks: 37 (limit: 17656)
   Memory: 340.4M
   CGroup: /system.slice/mysqld.service
           └─26627 /usr/sbin/mysqld

6月 19 23:57:01 node2 systemd[1]: Starting MySQL Server...
6月 19 23:57:02 node2 systemd[1]: Started MySQL Server.

10. 查看初始化随机生成的密码

[root@node2 ~]# cat /var/log/mysqld.log | grep password
2021-06-19T15:50:38.842799Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: k6#gu=!gdJgg

11. 安全设置

[root@node2 ~]# mysql_secure_installation

Securing the MySQL server deployment.

# 输入第 10 步中查看到的密码
Enter password for user root: 

# 重设密码
The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

# 密码强度,注意生产环境选择 2,如果只是个人测试选择 0
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.

Estimated strength of the password: 100 

# 确认密码强度后还得重输一次密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

# 是否删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

# 禁止 root 远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

# 删除测试数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

# 重新加载权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@node2 ~]# 

12. mysql 登录

# 使用上面修改后的密码登录
[root@node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

到此,MySQL 8 就在 Cent OS 8 系统上面安装成功了。