一、在线安装

yum localinstall 安装本地包,且可以解决依赖安装

yum localinstall https://repo.mysql.com/mysql80-community-release-el7-2.noarch.rpm
yum install mysql-community-server -y

二、下载本地安装

到官网下载对应的安装包 https://repo.mysql.com/

下载地址,注意下载对应的 el7,就是CentOS7版本

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar

1. 解压

tar xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar

2. 安装依赖

sudo yum install perl -y
sudo yum install net-tools -y

3. 安装MySQL

查看是否有安装过

rpm -qa|grep mariadb
rpm -qa|grep mysql

开始安装

sudo rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm 

sudo rpm -ivh mysql-community-devel-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm 
sudo rpm -ivh mysql-community-test-8.0.21-1.el7.x86_64.rpm

如果有依赖问题,可以尝试如:

sudo yum localinstall -y  mysql-community-test-8.0.21-1.el7.x86_64.rpm

三、配置启动

1. 修改权限

sudo chmod -R 777 /var/lib/mysql/

2. 初始化

会初始化mysql的数据目录,会往mysql目录创建必要的文件,为root用户生成一个临时密码

sudo mysqld --initialize
sudo chmod -R 777 /var/lib/mysql/*

3. 启动

本地安装时候使用mysqld

systemctl restart  mysqld # 老版本命令:service mysqld start

在线安装时候使用mysql

systemctl restart  mysql # 老版本命令:service mysql start

查看临时密码,如果有多个密码,选择最新的

$ sudo grep 'temporary password' /var/log/mysqld.log
2020-10-18T11:49:47.681801Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <ag?hk;_u8XF

4. 登录

复制前面的密码输入

mysql -uroot -p

5. 修改ROOT密码

$ alter user user() identified by "abc123456";
Query OK, 0 rows affected (0.02 sec)

6. 远程登录设置

修改配置文件

$ vim /etc/my.cnf
[mysqld]
character_set_server = utf8
bind-address = 0.0.0.0  # 任何地址都可以连接mysql

登录mysql配置

mysql> use mysql;

mysql> UPDATE user SET host='%' WHERE user='root';

mysql> FLUSH PRIVILEGES;

设置完记得重启

systemctl restart  mysqld

7. 防火墙设置

查看防火墙是否开启

$ systemctl status firewalld
$ systemctl start firewalld # 开启他
$ systemctl stop firewalld # 关闭他

如果开启防火墙的话,需要设置

$ firewall-cmd --zone=public --add-port=3306/tcp --permanent # --zone代表设置的空间,--permanent 永久生效
$ firewall-cmd --reload # 应用设置