---
title: "MySQL 8.0 root密码忘了怎么办"
author: "BucketLi"
date: "2019-05-16T20:02:21Z"
tags: []
categories: ["数据库"]
slug: "mysql-8-0-root"
draft: false
lastmod: "2019-05-16T20:04:04Z"
id: 2441026
url: "https://bucketli.iteye.com/blog/2441026"
---

普通账号密码忘了，可以通过 root 账号重置，但是如果 root 账号都忘了，怎么办呢？在你能够完全掌控 MySQL 的机器，并且不完全重装的前提下，几步搞定，这边 tips 下。

1. 先关闭 MySQL 服务。
2. 找到实际使用的 my.cnf 文件


> 在 [mysqld] 一节加上 skip-grant-tables


3. 重启 MySQL 
4. MySQL 机器本地使用 sock 登录，比如 


> ./mysql -S xxxxx/mysql.sock


5. 将root密码置空


> use mysql
> update user set authentication_string ='' where user='root';


6. 修改 my.cnf 去除  skip-grant-tables 并重启 MySQL
7. 使用空密码登陆


>  ./mysql -S xxxx/mysql.sock -uroot


8. 修改root密码为正常密码


> alter user 'root'@'localhost' identified by 'xxxxx';


9. done