圈小蛙

CentOS下Mysql8安装及忘记密码后如何重置密码

一、Mysql8安装、初始化及设置密码

1. Mysql8安装命令

rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
yum-config-manager --enable mysql80-community
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum update
yum -y install mysql-community-server mysql-community-client
systemctl start mysqld

2. 初始化命令

mysqld --initialize #初始化有密码root账户
mysqld --initialize-insecure #初始化无密码root账户

3. 设置root密码

mysql -u root -p #有密码root账户登陆
mysql -u root --skip-password #无密码root账户登陆
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword@qxwa'; #修改root密码

二、Mysql8忘记密码后重新设置密码

1. 修改配置文件,设置忽略密码登陆

修改mysql配置文件my.cnf
在[mysqld]下添加:skip-grant-tables

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables

然后重新启动mysql:systemctl restart mysqld

2. 修改root密码为空密码

登录数据库:mysql -u root -p,提示输入密码时直接回车。
选择数据库:use mysql,执行语句将密码置空:update user set authentication_string = '' where user = 'root';

3. 再次修改配置文件,设置密码登陆

注释或者删掉第一步的配置:skip-grant-tables
再次重新启动mysql:systemctl restart mysqld

4. 设置新密码

登录数据库:mysql -u root -p,提示输入密码时直接回车。
选择数据库:use mysql,设置新密码:ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword@qxwa';
执行:FLUSH PRIVILEGES;

至此,新密码重置成功,请注意,mysql8的一些语句与之前版本不一样。

三、安装时报错提示The GPG keys不正确

安装过程中出错,提示:

The GPG keys listed for the "MySQL 8.0 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.

要修复此错误,请导入 MySQL 的新 GPG 密钥:

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

导入新密钥后,更新软件包:

yum update
Exit mobile version