很多時(shí)候,在數(shù)據(jù)庫(kù)中忘記用戶密碼還能找上root賬號(hào)修改,但一旦忘記了root密碼時(shí),那又該怎么辦呢?
以下是需要修改root密碼的過(guò)程步驟:
1、修改 /etc/my.cnf,在 [mysqld] 小節(jié)下添加一行:
skip-grant-tables=1
這一行配置讓 mysqld 啟動(dòng)時(shí)不對(duì)密碼進(jìn)行驗(yàn)證
2、重啟 mysqld 服務(wù):systemctl restart mysqld
3、使用 root 用戶登錄到 mysql:mysql -u root
4、切換到mysql數(shù)據(jù)庫(kù),更新 user 表:
update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
在之前的版本中,密碼字段的字段名是 password,5.7版本改為了 authentication_string
5、退出 mysql,編輯 /etc/my.cnf 文件,刪除 skip-grant-tables=1 的內(nèi)容
6、重啟 mysqld 服務(wù),再用新密碼登錄即可