Mysql5.7修改root密碼
禁用root密碼
1、修改 /etc/my.cnf,在 [mysqld] 小節下添加一行:skip-grant-tables=1
這一行配置讓 mysqld 啟動時不對密碼進行驗證
2、重啟 mysqld 服務:systemctl restart mysqld
3、使用 root 用戶登錄到 mysql:mysql -u root
注釋:當看到密碼輸入時直接按return
添加新密碼
1、切換到mysql數據庫,更新 user 表:
use mysql
update user set authentication_string = password('新密碼'), password_expired = 'N', password_last_changed = now() where user = 'root';
在之前的版本中,密碼字段的字段名是 password,5.7版本改為了 authentication_string
恢復使用root密碼
1、退出 mysql,編輯 /etc/my.cnf 文件,刪除 skip-grant-tables=1 的內容
2、重啟 mysqld 服務,再用新密碼登錄即可
注意事項
另外,MySQL 5.7 在初始安裝后會生成隨機初始密碼,並在 /var/log/mysqld.log 中有記錄,可以通過 cat 命令查看,找 password 關鍵字
找到密碼后,在本機以初始密碼登錄,並且(也只能)通過 alter user 'root'@'localhost' identified by 'root' 命令,修改 root 用戶的密碼為 root,然后退出,重新以root用戶和剛設置的密碼進行登錄即可。