1、背景描述
-
在真实业务场景下,Linux服务器一般位于内网,所以无法直接访问互联网资源;
-
特别是安装数据库的Linux服务器,在网络方面的管控只会更加严格;
-
因此,需要提前下载好相关资源,再传输到内网Linux服务器进行安装;
2、下载Mysql的安装包
下载地址:https://dev.mysql.com/downloads/mysql/
注意:官网的下载速度可能很慢,有任务时间要求的须尽快下载,或者使用其他可靠的下载源
2.1、选择对应Linux版本的安装包

2.2、以游客的身份直接下载安装包

3、安装Mysql
3.1、传输安装包到Linux

3.2、校验安装包

在下载或传输过程中,安装包可能会损坏,因此要校验其md5是否与官方的(见2.1节的下载截图)保持一致
md5sum 安装包名称
3.3、查询并卸载系统自带的Mariadb

rpm -qa | grep mariadb
rpm -e --nodeps 查询到的名称
3.4、查询并卸载系统老旧版本的Mysql

rpm -qa | grep mysql
本例中没有查找到旧版本的Mysql(因为没安装过),所以不用卸载
rpm -e --nodeps 查询到的名称
3.5、确保系统已安装libaio

一般Linux系统默认就已安装,除非被手动卸载了
rpm -qa | grep libaio
3.6、解压安装包

tar -xvf 安装包名称
3.7、按顺序执行rpm包安装
安装时,注意实际rpm包版本号的差异;
此外,还应尽可能使用root用户来执行安装命令,以避免各种权限不足问题;

rpm -ivh mysql-community-common-5.7.43-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.43-1.el7.x86_64.rpm

rpm -ivh mysql-community-devel-5.7.43-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-compat-5.7.43-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.43-1.el7.x86_64.rpm

rpm -ivh mysql-community-server-5.7.43-1.el7.x86_64.rpm
3.8、可能出现的报错及处理

安装Mysql的server包时,可能会报错;这是安装Linux时,选择了"最小安装"环境导致缺少了服务器方面的相关软件依赖;
如果是自己安装的Linux虚拟机,可以重装Linux,基本环境选择"基础设施服务器"即可,参考该博文3.6节;
如果是第三方(例如信息中心)提供的Linux,可以尝试联系其重新分配具备类似"基础设施服务器"环境的Linux;
如果必须使用该Linux环境安装Mysql,须离线安装perl和离线安装net-tools后,再继续安装Mysql的server包;
警告:mysql-community-server-5.7.43-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
/usr/bin/perl 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
net-tools 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
perl(Getopt::Long) 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
perl(strict) 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
4、启动Mysql
4.1、查看Mysql服务是否启动

systemctl status mysqld
4.2、启动Mysql服务

systemctl start mysqld
4.3、核查Mysql服务是否启动

4.4、设置开机自启动

systemctl enable mysqld
设置开机自启动mysqld服务后,还需要重新加载systemd服务,以使配置生效
systemctl daemon-reload
5、调整MySQL登录配置
5.1、查看随机初始密码

MySQL安装成功后,会自动为root账号生成一个随机的初始密码,并存储在/var/log/mysqld.log文件中,本例的密码为QloZfk).h6t,
cat /var/log/mysqld.log | grep password
5.2、使用初始密码登录

mysql -uroot -p
5.3、修改root账号的密码

@后面的localhost表示本地机器,即允许本机的连接
SET PASSWORD FOR 'root'@'localhost'= "Rain@123";
5.4、授权root账号远程访问

@后面的%表示任意地址,即允许来自任何ip的连接
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Rain@123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
5.5、退出MySQL

exit
6、调整Linux防火墙

MySQL默认使用的端口是3306,想远程访问MySQL数据库,Linux防火墙的3306端口要对外开放
firewall-cmd --add-port=3306/tcp --permanent
增加开放端口后,需要重新加载防火墙配置
firewall-cmd --reload
本文参考
https://www.cnblogs.com/lemon-feng/p/11233227.html