CentOS7 使用 yum 快速安装 MySQL8

35 阅读1分钟

安装 MySQL8

一、准备

  1. 卸载已安装的 MySQL
sudo rpm -qa | grep mysql

#如果有已安装的mysql,可以使用下面命令进行卸载
sudo rpm -qa | grep mysql | xargs sudo rpm -e --nodeps 
  1. MySQL 目录检查
sudo find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/share/mysql

#如果有mysql目录,可以使用下面命令删除
sudo find / -name mysql | xargs sudo rm -rf 
  1. 添加DNS
sudo vim /etc/resolv.conf

#添加配置
nameserver 114.114.114.114
nameserver 8.8.8.8

二、安装 MySQL8

  1. 下载 rpm 文件
sudo yum -y localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
  1. 安装 MySQL
sudo yum -y install mysql-community-server
  1. 启动 MySQL
sudo systemctl start mysqld
  1. 查询初始密码
sudo grep 'temporary password' /var/log/mysqld.log 
2022-11-22T11:56:01.773134Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ?*:OgB8.tuc3

如未查询到,则可以重启 MySQL

  1. 登录数据库,修改密码
#登录 
mysql -uroot -p

#修改密码
mysql> alter user user() identified by "root@123.";
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> 
mysql> alter user user() identified by "Root@123.";
Query OK, 0 rows affected (0.00 sec)
  1. 修改root用户远程访问
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> 
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

出现问题记录

安装时出现下面问题:

失败的软件包是:mysql-community-server-8.0.31-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

注:2022 是当前年份