1 环境介绍

系统:CentOS Linux release 7.9.2009 (Core)
MySQL:MySQL 5.7.37

2 安装方式

在Linux 上安装 MySQL 的种方式:

  1. 命令行在线安装
  2. 对应发行版格式包文件离线安装
    1. Red Hat系Linux

image.png

  1. Debian 系Linux
    1. Linux 通用二进制文件安装

image.png
本文所介绍的是第 2 种安装方式:使用红帽系CentOS发行版的.rpm文件进行离线安装。

3 下载MySQL

  1. 打开官网下载地址:https://dev.mysql.com/downloads/mysql/
  2. 选择操作系统和操作系统版本

**Select Operating System:**Red Hat Enterprise Linux / Oracle Linux
**Select OS Version:**Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)

  1. 选择RMP Bundle进行下载(mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar)。

    4 安装MySQL

    4.1 安装前准备

  2. (可选)如果系统之前安装过MySQL,要确保之前安装的MySQL已经完全删除,并且确保旧版本的数据文件、配置文件(/etc/my.cfg、/etc/mysql目录)已经删除,否则再次安装有可能会失败。 ```bash

    查看CentOS系统是否已安装mysql

    [root@localhost /]# rpm -qa|grep mysql

删除历史MySQL的my.cfg配置文件

[root@localhost /]# rm /etc/my.cnf rm: cannot remove ‘/etc/my.cnf’: No such file or directory

删除历史MySQL的mysql文件夹

[root@localhost /]# rm -r /etc/mysql rm: cannot remove ‘/etc/mysql’: No such file or directory

检查mysql组和用户是否存在

[root@localhost /]# cat /etc/group | grep mysql [root@localhost /]# cat /etc/passwd | grep mysql

若存在,删除

userdel mysql #删除 mysql用户 groupdel mysql #删除 mysql用户组

  1. 2. 移除`mariadb`
  2. ```bash
  3. [root@localhost ~]# rpm -qa mariadb*
  4. mariadb-libs-5.5.68-1.el7.x86_64
  5. [root@localhost ~]# yum remove mariadb-libs.x86_64
  6. Loaded plugins: fastestmirror
  7. Resolving Dependencies
  8. --> Running transaction check
  9. ---> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be erased
  10. --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
  11. --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
  12. --> Running transaction check
  13. ---> Package postfix.x86_64 2:2.10.1-9.el7 will be erased
  14. --> Finished Dependency Resolution
  15. Dependencies Resolved
  16. ===============================================================================================================================================================
  17. Package Arch Version Repository Size
  18. ===============================================================================================================================================================
  19. Removing:
  20. mariadb-libs x86_64 1:5.5.68-1.el7 @anaconda 4.4 M
  21. Removing for dependencies:
  22. postfix x86_64 2:2.10.1-9.el7 @anaconda 12 M
  23. Transaction Summary
  24. ===============================================================================================================================================================
  25. Remove 1 Package (+1 Dependent package)
  26. Installed size: 17 M
  27. Is this ok [y/N]: y
  28. Downloading packages:
  29. Running transaction check
  30. Running transaction test
  31. Transaction test succeeded
  32. Running transaction
  33. Erasing : 2:postfix-2.10.1-9.el7.x86_64 1/2
  34. Erasing : 1:mariadb-libs-5.5.68-1.el7.x86_64 2/2
  35. Verifying : 1:mariadb-libs-5.5.68-1.el7.x86_64 1/2
  36. Verifying : 2:postfix-2.10.1-9.el7.x86_64 2/2
  37. Removed:
  38. mariadb-libs.x86_64 1:5.5.68-1.el7
  39. Dependency Removed:
  40. postfix.x86_64 2:2.10.1-9.el7
  41. Complete!
  42. [root@localhost ~]#

4.2 正式安装

注意:安装命令需要**root用户****具有sudo权限的用户**执行

4.2.1 上传文件到服务器

mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar

4.2.2 解压上传的文件

  1. [root@localhost opt]# tar xf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
  2. [root@localhost opt]# ll
  3. total 1066212
  4. -rw-r--r--. 1 ser ser 545894400 Jan 20 14:20 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
  5. -rw-r--r--. 1 7155 31415 26670472 Dec 16 10:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm
  6. -rw-r--r--. 1 7155 31415 318884 Dec 16 10:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm
  7. -rw-r--r--. 1 7155 31415 4122424 Dec 16 10:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
  8. -rw-r--r--. 1 7155 31415 47760004 Dec 16 10:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm
  9. -rw-r--r--. 1 7155 31415 23316972 Dec 16 10:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm
  10. -rw-r--r--. 1 7155 31415 132204460 Dec 16 10:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm
  11. -rw-r--r--. 1 7155 31415 2474780 Dec 16 10:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
  12. -rw-r--r--. 1 7155 31415 1264836 Dec 16 10:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
  13. -rw-r--r--. 1 7155 31415 182274360 Dec 16 10:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm
  14. -rw-r--r--. 1 7155 31415 125477564 Dec 16 10:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm
  15. [root@localhost opt]#

关于各个.rpm文件的说明:
RPM Packages for MySQL Community Edition

包名 说明
mysql-community-server 数据库服务器和相关工具
mysql-community-client MySQL 客户端应用程序和工具
mysql-community-common 服务器和客户端的公共库文件
mysql-community-devel MySQL 数据库客户端应用程序的开发头文件和库文件
mysql-community-libs MySQL 数据库客户端应用程序的共享库
mysql-community-libs-compat MySQL数据库客户端应用程序的共享兼容库
mysql-community-embedded MySQL 嵌入式库文件
mysql-community-embedded-devel MySQL 嵌入式库的开发头文件和库文件
mysql-community-test MySQL 服务器测试套件

4.2.3 离线安装.rpm文件

  1. [root@localhost opt]# ll
  2. total 1066212
  3. -rw-r--r--. 1 ser ser 545894400 Jan 20 14:20 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
  4. -rw-r--r--. 1 7155 31415 26670472 Dec 16 10:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm
  5. -rw-r--r--. 1 7155 31415 318884 Dec 16 10:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm
  6. -rw-r--r--. 1 7155 31415 4122424 Dec 16 10:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
  7. -rw-r--r--. 1 7155 31415 47760004 Dec 16 10:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm
  8. -rw-r--r--. 1 7155 31415 23316972 Dec 16 10:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm
  9. -rw-r--r--. 1 7155 31415 132204460 Dec 16 10:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm
  10. -rw-r--r--. 1 7155 31415 2474780 Dec 16 10:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
  11. -rw-r--r--. 1 7155 31415 1264836 Dec 16 10:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
  12. -rw-r--r--. 1 7155 31415 182274360 Dec 16 10:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm
  13. -rw-r--r--. 1 7155 31415 125477564 Dec 16 10:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm
  14. [root@localhost opt]# sudo yum install mysql-community-client-5.7.37-1.el7.x86_64.rpm mysql-community-common-5.7.37-1.el7.x86_64.rpm mysql-community-libs-5.7.37-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm mysql-community-server-5.7.37-1.el7.x86_64.rpm
  15. Loaded plugins: fastestmirror
  16. Examining mysql-community-client-5.7.37-1.el7.x86_64.rpm: mysql-community-client-5.7.37-1.el7.x86_64
  17. Marking mysql-community-client-5.7.37-1.el7.x86_64.rpm to be installed
  18. Examining mysql-community-common-5.7.37-1.el7.x86_64.rpm: mysql-community-common-5.7.37-1.el7.x86_64
  19. Marking mysql-community-common-5.7.37-1.el7.x86_64.rpm to be installed
  20. Examining mysql-community-libs-5.7.37-1.el7.x86_64.rpm: mysql-community-libs-5.7.37-1.el7.x86_64
  21. Marking mysql-community-libs-5.7.37-1.el7.x86_64.rpm to be installed
  22. Examining mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm: mysql-community-libs-compat-5.7.37-1.el7.x86_64
  23. Marking mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm to be installed
  24. Examining mysql-community-server-5.7.37-1.el7.x86_64.rpm: mysql-community-server-5.7.37-1.el7.x86_64
  25. Marking mysql-community-server-5.7.37-1.el7.x86_64.rpm to be installed
  26. Resolving Dependencies
  27. --> Running transaction check
  28. ---> Package mysql-community-client.x86_64 0:5.7.37-1.el7 will be installed
  29. ---> Package mysql-community-common.x86_64 0:5.7.37-1.el7 will be installed
  30. ---> Package mysql-community-libs.x86_64 0:5.7.37-1.el7 will be installed
  31. ---> Package mysql-community-libs-compat.x86_64 0:5.7.37-1.el7 will be installed
  32. ---> Package mysql-community-server.x86_64 0:5.7.37-1.el7 will be installed
  33. --> Finished Dependency Resolution
  34. Dependencies Resolved
  35. ===============================================================================================================================================================
  36. Package Arch Version Repository Size
  37. ===============================================================================================================================================================
  38. Installing:
  39. mysql-community-client x86_64 5.7.37-1.el7 /mysql-community-client-5.7.37-1.el7.x86_64 102 M
  40. mysql-community-common x86_64 5.7.37-1.el7 /mysql-community-common-5.7.37-1.el7.x86_64 2.8 M
  41. mysql-community-libs x86_64 5.7.37-1.el7 /mysql-community-libs-5.7.37-1.el7.x86_64 9.5 M
  42. mysql-community-libs-compat x86_64 5.7.37-1.el7 /mysql-community-libs-compat-5.7.37-1.el7.x86_64 6.0 M
  43. mysql-community-server x86_64 5.7.37-1.el7 /mysql-community-server-5.7.37-1.el7.x86_64 764 M
  44. Transaction Summary
  45. ===============================================================================================================================================================
  46. Install 5 Packages
  47. Total size: 884 M
  48. Installed size: 884 M
  49. Is this ok [y/d/N]: y
  50. Downloading packages:
  51. Running transaction check
  52. Running transaction test
  53. Transaction test succeeded
  54. Running transaction
  55. Installing : mysql-community-common-5.7.37-1.el7.x86_64 1/5
  56. Installing : mysql-community-libs-5.7.37-1.el7.x86_64 2/5
  57. Installing : mysql-community-client-5.7.37-1.el7.x86_64 3/5
  58. Installing : mysql-community-server-5.7.37-1.el7.x86_64 4/5
  59. Installing : mysql-community-libs-compat-5.7.37-1.el7.x86_64 5/5
  60. Verifying : mysql-community-server-5.7.37-1.el7.x86_64 1/5
  61. Verifying : mysql-community-libs-5.7.37-1.el7.x86_64 2/5
  62. Verifying : mysql-community-common-5.7.37-1.el7.x86_64 3/5
  63. Verifying : mysql-community-libs-compat-5.7.37-1.el7.x86_64 4/5
  64. Verifying : mysql-community-client-5.7.37-1.el7.x86_64 5/5
  65. Installed:
  66. mysql-community-client.x86_64 0:5.7.37-1.el7 mysql-community-common.x86_64 0:5.7.37-1.el7 mysql-community-libs.x86_64 0:5.7.37-1.el7
  67. mysql-community-libs-compat.x86_64 0:5.7.37-1.el7 mysql-community-server.x86_64 0:5.7.37-1.el7
  68. Complete!
  69. [root@localhost opt]#

注意:版本5.7.37以.rpm方式安装后会创建 /etc/my.cnf 文件老旧的版本在安装后可能会创建 /usr/my.cnf 文件,强烈建议安装后检查是否有该文件。如果有的话,将它迁移到 /etc/my.cnf 然后删除 /usr/my.cnf。

4.2.4 启动并登录MySQL

  1. MySQL 安装完成后并不会自动启动,使用以下命令启动

    1. [root@localhost opt]# service mysqld start
    2. Redirecting to /bin/systemctl start mysqld.service
    3. [root@localhost opt]#
  2. 启动完成后,超级管理员 root 账号被创建,使用以下命令查看 root 的初始化密码

    1. [root@localhost opt]# grep 'temporary password' /var/log/mysqld.log
    2. 2022-01-20T07:30:03.825755Z 1 [Note] A temporary password is generated for root@localhost: 1fYrNW)Itxfq
    3. [root@localhost opt]#
  3. 登录root用户

    1. [root@localhost opt]# mysql -u root -p
    2. Enter password:
  4. 修改root密码

    1. mysql> alter user 'root'@'localhost' identified by 'MyNewPassword!';
    2. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    ERROR 1819 (HY000) 原因:MySQL 5.7.37 默认启用了validate_password插件,该插件会校验密码强度,修改密码时校验不通过就会报ERR0R 1819 (HY00)。
    对安全性要求高的环境,建议设置高强度的密码。
    对于个人学习使用(无重要资料),密码校验太麻烦了,可以在/etc/my.conf配置文件中将密码校验插件关闭:

    1. # /etc/my.conf
    2. # 配置修改后需重启MySQL服务
    3. [mysqld]
    4. # other config...
    5. validate_password=OFF

    4.2.5 允许root远程登录

    仅自己学习使用 ```bash [root@localhost opt]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g.

mysql> SELECT User, Password, Host FROM user; ERROR 1046 (3D000): No database selected mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

Database changed mysql> select user, host from user; +———————-+—————-+ | user | host | +———————-+—————-+ | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +———————-+—————-+ 3 rows in set (0.00 sec)

mysql> grant all privileges on . to ‘root’@’%’ identified by ‘123456’ with grant option; Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user, host from user; +———————-+—————-+ | user | host | +———————-+—————-+ | root | % | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +———————-+—————-+ 4 rows in set (0.00 sec)

mysql> exit Bye [root@localhost opt]#

```

5 参考链接

  1. 【CentOS7安装MySQL的几种方法】:https://blog.csdn.net/qq_43928088/article/details/107383077
  2. 【Mysql 5.7 中如何禁用 密码检测插件(validate password plugin)】:https://blog.csdn.net/rainbow702/article/details/58128914
  3. 【2.5.5 Installing MySQL on Linux Using RPM Packages from Oracle】:https://dev.mysql.com/doc/refman/5.7/en/linux-installation-rpm.html