centos7安装mysql5.7


1、检查 MariaDB 是否安装
yum list installed | grep mariadb

2.卸载全部 MariaDB 相关
yum -y remove mariadb*

3.下载 MySQL 的 YUM 源
进入到要下载到的路径:cd /usr/local/src
下载:wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

4、安装 MySQL 的 YUM 源
rpm -ivh mysql57-community-release-el7-11.noarch.rpm

5、检查 MySQL 的 YUM 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"

显示以下则安装成功
mysql-connectors-community/x86_64 MySQL Connectors Community 203
mysql-tools-community/x86_64 MySQL Tools Community 129
mysql57-community/x86_64 MySQL 5.7 Community Server 504

6、安装 MySQL
yum install -y mysql-community-server

7、启动 MySQL 服务
systemctl start mysqld

8.mysql5.7以下是密码为空直接进入数据库的,但是mysql5.7就需要初始密码
#grep 'temporary password' /var/log/mysqld.log //查看初始密码root@localhost:后即为初始密码
然后执行 mysql -uroot -p使用初始密码登录,必须马上修改新的密码,不然会报如下错误:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

设置新密码:
mysql> alter user 'root'@'localhost' identified by 'newpasswd';
Query OK, 0 rows affected (0.00 sec)

9.后期密码修复方法:
MySQL 5.7 以下版本:UPDATE mysql.user SET Password=PASSWORD('123456') where USER='root';
MySQL 5.7 版本:UPDATE mysql.user SET authentication_string=PASSWORD('123456') where USER='root';
⑤ 刷新权限:flush privileges;
⑥ 退出:exit;

10.报如下错误

mysql-community-server-5.7.37-1.el7.x86_64.rpm 的公钥尚未安装失败的软件包是:mysql-community-server-5.7.37-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥源 "MySQL 5.7 Community Server" 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。失败的软件包是:mysql-community-server-5.7.37-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

执行以下命令解决:

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022