1.在官网上下载mysql对应的CentOS7的rpm包

wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm

CentOS7安装mysql5.7_mysql

2.

rpm -ivh mysql80-community-release-el7-1.noarch.rpm

CentOS7安装mysql5.7_mysql_02

CentOS7安装mysql5.7_mysql_03

CentOS7安装mysql5.7_mysql_04编辑

3.

yum repolist all | grep mysql

CentOS7安装mysql5.7_mysql_05

CentOS7安装mysql5.7_mysql_06

CentOS7安装mysql5.7_mysql_07编辑

4.

cd /etc/yum.repos.d

CentOS7安装mysql5.7_mysql_08

CentOS7安装mysql5.7_mysql_09

CentOS7安装mysql5.7_mysql_10编辑

5. 

vi mysql-community.repo

CentOS7安装mysql5.7_mysql_11

CentOS7安装mysql5.7_mysql_12

CentOS7安装mysql5.7_mysql_13编辑

6.

yum repolist enabled | grep mysql

CentOS7安装mysql5.7_mysql_14

CentOS7安装mysql5.7_mysql_15

CentOS7安装mysql5.7_mysql_16编辑

7.

yum install -y mysql-community-server

CentOS7安装mysql5.7_mysql_17

CentOS7安装mysql5.7_mysql_18

CentOS7安装mysql5.7_mysql_19编辑

这里可能会报错如下:

You can remove cached packages by executing 'yum clean packages'.
Error: GPG check FAILED

CentOS7安装mysql5.7_mysql_20

这由于源key错误导致的dnf或者yum(软件包管理器)安装软件失败。

在指令后面加一个命令–nogpgcheck 就解决了

yum install -y mysql-community-server  --nogpgcheck

CentOS7安装mysql5.7_mysql_21

还可能会报错如下:

Error: Transaction test error:
file /etc/my.cnf from install of mysql-community-server-5.7.37-1.el7.x86_64 conflicts with file from package mariadb-common-3:10.3.9-8.ky10.x86_64

CentOS7安装mysql5.7_mysql_22

这里直接将冲突的删除之后重新执行install操作

yum -y remove mariadb-common-3:10.3.9-8.ky10.x86_64

CentOS7安装mysql5.7_mysql_23

8.启动mysql

systemctl start mysqld.service

CentOS7安装mysql5.7_mysql_24

CentOS7安装mysql5.7_mysql_25

CentOS7安装mysql5.7_mysql_26编辑

9.第一次登录要先去找初始密码(可能会报错):

CentOS7安装mysql5.7_mysql_27

CentOS7安装mysql5.7_mysql_28编辑

CentOS7安装mysql5.7_mysql_29

CentOS7安装mysql5.7_mysql_30编辑

10.重启mysql并登录修改密码:

systemctl restart mysqld.service

CentOS7安装mysql5.7_mysql_31

mysql -uroot -p

CentOS7安装mysql5.7_mysql_32

CentOS7安装mysql5.7_mysql_33

CentOS7安装mysql5.7_mysql_34编辑

use mysql

update user set authentication_string=password('lfrgch') where user = 'root';

flush privileges;

CentOS7安装mysql5.7_mysql_35

CentOS7安装mysql5.7_mysql_36

CentOS7安装mysql5.7_mysql_37编辑

将之前对/etc/my.cnf的修改改回来。

重新登录之后报错可以参考以下操作:

set global validate_password_policy = 0;

set global validate_password_mixed_case_count = 0;

set global validate_password_number_count = 3;

set global validate_password_special_char_count = 0;

set global validate_password_length = 3;

alter user user() identified by 'lfrgch';

CentOS7安装mysql5.7_mysql_38

CentOS7安装mysql5.7_mysql_39

CentOS7安装mysql5.7_mysql_40编辑

之后再次登录mysql就可以正常登录了。

11.开启mysql的远程权限

然后我们再开启mysql的远程权限,这样我们就可以从windows连接到虚拟机的mysql了。

grant all privileges on *.* to root@"%" identified by 'lfrgch';

CentOS7安装mysql5.7_mysql_41

CentOS7安装mysql5.7_mysql_42

CentOS7安装mysql5.7_mysql_43编辑

*.*的意思是任何库的任何表,这句话的意思是root这个用户从任何机器过来都可以访问任何库的任何表。

写完这句话我们还要刷新一下:

flush privileges;

CentOS7安装mysql5.7_mysql_44

如果设置完以上的远程权限,发现在navicat上依然无法连接到这个mysql,则可以考虑一下是不是Centos的防火墙没有开放3306端口,导致被拦截。如果需要开放3306端口则可以参考我的另一篇文章如下:

​linux防火墙常用操作https://blog.csdn.net/qq_37050372/article/details/123330118​

12.设置mysql不区分大小写

linux安装完mysql默认是不区分大小写的,需要额外设置

vi /etc/my.cnf

CentOS7安装mysql5.7_mysql_45

在该文件中添加:

 lower_case_table_names=1

然后重启Mysql

systemctl restart mysqld

CentOS7安装mysql5.7_mysql_46