MySQL 5.7 数据库的忘记 root 登录密码处理

在 /etc/my.cnf 配置文件中添加 skip-grant-tables 绕开 MySQL 5.7 数据库密码验证
echo skip-grant-tables >> /etc/my.cnf

/etc/init.d/mysqld restart

直接使用 mysql 命令登录 MySQL5.7 数据库并修改 root 登录密码
mysql
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘password‘;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘password‘;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

使用新密码 password 登录 MySQL5.7 数据库
mysql -uroot -ppassword

删掉 /etc/my.cnf 里的 skip-grant-tables
sed -i -e ‘/skip-grant-tables/d‘ /etc/my.cnf
/etc/init.d/mysqld restart

相关推荐