我用到的數據庫是mysql8
環境是centos7
- 關閉mysql服務
service mysqld stop
- 修改mysql為免密碼登陸
vi /etc/my.cof
在最下面加上skip-grant-tables
- 啟動mysql服務,並登陸
service mysqld start
mysql -u root -p密碼為空,直接回車
- 查看mysql的用戶及密碼
use mysql;
select host, user, authentication_string, plugin from user;將authentication_string字段設置為空,就是把密碼設置為空
update user set authentication_string='' where user='root';
- 退出mysql,找到(vi)剛才在
/etc/mycnf
最后加的哪一行(dd)刪除 - 重啟mysql服務,(因為此時是免密碼的模式,所以要重啟下)
service mysqld stop
service mysqld start
- 再次使用root登陸,此時不是免密碼登陸的而是密碼為空
mysql -u root -p
passwd 直接回車就可以登陸
- 修改root用戶的密碼
ALTER user 'root'@'localhost' IDENTIFIED BY 'Mynameis7#'
- 設置成功,此時使用mysql -u root -p ,然后輸入密碼就可以登陸,保險起見可以重啟一下數據庫。