1 环境介绍
系统:CentOS Linux release 7.9.2009 (Core)
MySQL:MySQL 5.7.37
2 安装方式
在Linux 上安装 MySQL 的种方式:
- 命令行在线安装
- 对应发行版格式包文件离线安装
- Red Hat系Linux

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

本文所介绍的是第 2 种安装方式:使用红帽系CentOS发行版的.rpm文件进行离线安装。
3 下载MySQL
- 打开官网下载地址:https://dev.mysql.com/downloads/mysql/
- 选择操作系统和操作系统版本
**Select Operating System:**Red Hat Enterprise Linux / Oracle Linux**Select OS Version:**Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
选择
RMP Bundle进行下载(mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar)。4 安装MySQL
4.1 安装前准备
(可选)如果系统之前安装过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用户组
2. 移除`mariadb````bash[root@localhost ~]# rpm -qa mariadb*mariadb-libs-5.5.68-1.el7.x86_64[root@localhost ~]# yum remove mariadb-libs.x86_64Loaded plugins: fastestmirrorResolving Dependencies--> Running transaction check---> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be erased--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64--> Running transaction check---> Package postfix.x86_64 2:2.10.1-9.el7 will be erased--> Finished Dependency ResolutionDependencies Resolved===============================================================================================================================================================Package Arch Version Repository Size===============================================================================================================================================================Removing:mariadb-libs x86_64 1:5.5.68-1.el7 @anaconda 4.4 MRemoving for dependencies:postfix x86_64 2:2.10.1-9.el7 @anaconda 12 MTransaction Summary===============================================================================================================================================================Remove 1 Package (+1 Dependent package)Installed size: 17 MIs this ok [y/N]: yDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transactionErasing : 2:postfix-2.10.1-9.el7.x86_64 1/2Erasing : 1:mariadb-libs-5.5.68-1.el7.x86_64 2/2Verifying : 1:mariadb-libs-5.5.68-1.el7.x86_64 1/2Verifying : 2:postfix-2.10.1-9.el7.x86_64 2/2Removed:mariadb-libs.x86_64 1:5.5.68-1.el7Dependency Removed:postfix.x86_64 2:2.10.1-9.el7Complete![root@localhost ~]#
4.2 正式安装
注意:安装命令需要**root用户**或**具有sudo权限的用户**执行
4.2.1 上传文件到服务器
mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
4.2.2 解压上传的文件
[root@localhost opt]# tar xf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar[root@localhost opt]# lltotal 1066212-rw-r--r--. 1 ser ser 545894400 Jan 20 14:20 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar-rw-r--r--. 1 7155 31415 26670472 Dec 16 10:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 318884 Dec 16 10:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 4122424 Dec 16 10:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 47760004 Dec 16 10:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 23316972 Dec 16 10:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 132204460 Dec 16 10:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 2474780 Dec 16 10:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 1264836 Dec 16 10:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 182274360 Dec 16 10:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 125477564 Dec 16 10:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm[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文件
[root@localhost opt]# lltotal 1066212-rw-r--r--. 1 ser ser 545894400 Jan 20 14:20 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar-rw-r--r--. 1 7155 31415 26670472 Dec 16 10:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 318884 Dec 16 10:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 4122424 Dec 16 10:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 47760004 Dec 16 10:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 23316972 Dec 16 10:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 132204460 Dec 16 10:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 2474780 Dec 16 10:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 1264836 Dec 16 10:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 182274360 Dec 16 10:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm-rw-r--r--. 1 7155 31415 125477564 Dec 16 10:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm[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.rpmLoaded plugins: fastestmirrorExamining mysql-community-client-5.7.37-1.el7.x86_64.rpm: mysql-community-client-5.7.37-1.el7.x86_64Marking mysql-community-client-5.7.37-1.el7.x86_64.rpm to be installedExamining mysql-community-common-5.7.37-1.el7.x86_64.rpm: mysql-community-common-5.7.37-1.el7.x86_64Marking mysql-community-common-5.7.37-1.el7.x86_64.rpm to be installedExamining mysql-community-libs-5.7.37-1.el7.x86_64.rpm: mysql-community-libs-5.7.37-1.el7.x86_64Marking mysql-community-libs-5.7.37-1.el7.x86_64.rpm to be installedExamining mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm: mysql-community-libs-compat-5.7.37-1.el7.x86_64Marking mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm to be installedExamining mysql-community-server-5.7.37-1.el7.x86_64.rpm: mysql-community-server-5.7.37-1.el7.x86_64Marking mysql-community-server-5.7.37-1.el7.x86_64.rpm to be installedResolving Dependencies--> Running transaction check---> Package mysql-community-client.x86_64 0:5.7.37-1.el7 will be installed---> Package mysql-community-common.x86_64 0:5.7.37-1.el7 will be installed---> Package mysql-community-libs.x86_64 0:5.7.37-1.el7 will be installed---> Package mysql-community-libs-compat.x86_64 0:5.7.37-1.el7 will be installed---> Package mysql-community-server.x86_64 0:5.7.37-1.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved===============================================================================================================================================================Package Arch Version Repository Size===============================================================================================================================================================Installing:mysql-community-client x86_64 5.7.37-1.el7 /mysql-community-client-5.7.37-1.el7.x86_64 102 Mmysql-community-common x86_64 5.7.37-1.el7 /mysql-community-common-5.7.37-1.el7.x86_64 2.8 Mmysql-community-libs x86_64 5.7.37-1.el7 /mysql-community-libs-5.7.37-1.el7.x86_64 9.5 Mmysql-community-libs-compat x86_64 5.7.37-1.el7 /mysql-community-libs-compat-5.7.37-1.el7.x86_64 6.0 Mmysql-community-server x86_64 5.7.37-1.el7 /mysql-community-server-5.7.37-1.el7.x86_64 764 MTransaction Summary===============================================================================================================================================================Install 5 PackagesTotal size: 884 MInstalled size: 884 MIs this ok [y/d/N]: yDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : mysql-community-common-5.7.37-1.el7.x86_64 1/5Installing : mysql-community-libs-5.7.37-1.el7.x86_64 2/5Installing : mysql-community-client-5.7.37-1.el7.x86_64 3/5Installing : mysql-community-server-5.7.37-1.el7.x86_64 4/5Installing : mysql-community-libs-compat-5.7.37-1.el7.x86_64 5/5Verifying : mysql-community-server-5.7.37-1.el7.x86_64 1/5Verifying : mysql-community-libs-5.7.37-1.el7.x86_64 2/5Verifying : mysql-community-common-5.7.37-1.el7.x86_64 3/5Verifying : mysql-community-libs-compat-5.7.37-1.el7.x86_64 4/5Verifying : mysql-community-client-5.7.37-1.el7.x86_64 5/5Installed: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.el7mysql-community-libs-compat.x86_64 0:5.7.37-1.el7 mysql-community-server.x86_64 0:5.7.37-1.el7Complete![root@localhost opt]#
注意:版本5.7.37以.rpm方式安装后会创建 /etc/my.cnf 文件。老旧的版本在安装后可能会创建 /usr/my.cnf 文件,强烈建议安装后检查是否有该文件。如果有的话,将它迁移到 /etc/my.cnf 然后删除 /usr/my.cnf。
4.2.4 启动并登录MySQL
MySQL 安装完成后并不会自动启动,使用以下命令启动
[root@localhost opt]# service mysqld startRedirecting to /bin/systemctl start mysqld.service[root@localhost opt]#
启动完成后,超级管理员
root账号被创建,使用以下命令查看root的初始化密码[root@localhost opt]# grep 'temporary password' /var/log/mysqld.log2022-01-20T07:30:03.825755Z 1 [Note] A temporary password is generated for root@localhost: 1fYrNW)Itxfq[root@localhost opt]#
登录
root用户[root@localhost opt]# mysql -u root -pEnter password:
修改
root密码mysql> alter user 'root'@'localhost' identified by 'MyNewPassword!';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配置文件中将密码校验插件关闭:# /etc/my.conf# 配置修改后需重启MySQL服务[mysqld]# other config...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 参考链接
- 【CentOS7安装MySQL的几种方法】:https://blog.csdn.net/qq_43928088/article/details/107383077
- 【Mysql 5.7 中如何禁用 密码检测插件(validate password plugin)】:https://blog.csdn.net/rainbow702/article/details/58128914
- 【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
