1、更新:sudo yum update
2、添加MySQL Yum存储库:sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
3、安装MySQL : sudo yum install mysql-server
若安装过程中会报错:
Failing package is: mysql-community-client-8.0.39-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决方案1:
这是秘钥过期了,我们只需要在命令输入如下命令更新一下秘钥:rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
再次安装mysql服务器:sudo yum install mysql-server
解决方案2:
强制绕过 GPG 校验,但这不推荐用于生产环境:sudo yum install mysql-server --nogpgcheck
4、启动MySQL服务并设置它在系统启动时自动启动:
sudo systemctl start mysqld
sudo systemctl enable mysqld
5、修改密码
5.1、找到初始密码:
在首次启动MySQL后,MySQL会生成一个初始密码并存储在日志文件中。您可以使用以下命令找到密码:
sudo grep 'temporary password' /var/log/mysqld.log
5.2使用临时密码登录到MySQL:
mysql -u root -p
输入临时密码
5.3登录后,首先更改root用户的密码。使用以下SQL命令:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
6、设置允许远程访问:
显式创建:CREATE USER 'root'@'%' IDENTIFIED BY '用户密码';
授予权限:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
修改密码:ALTER USER 'root'@'%' IDENTIFIED BY '用户密码';
刷新权限:FLUSH PRIVILEGES;