1. 程式人生 > 資料庫 >mysql 8.0.16 winx64及Linux修改root使用者密碼 的方法

mysql 8.0.16 winx64及Linux修改root使用者密碼 的方法

連線資料庫等基礎操作請自行解決哈,本篇是重點記錄如何改密碼。

一、查詢使用者密碼:

查詢使用者密碼命令:

select host,user,authentication_string from mysql.user ;

host:允許使用者登入的ip‘位置'%表示可以遠端;

user:當前資料庫的使用者名稱;

authentication_string:使用者密碼(後面有提到此欄位);

二、 設定(或修改)使用者密碼:

預設root密碼為空的話 ,下面使用navicat就無法連線(之前我裝的5.7好像還可以),所以這裡需要修改root的密碼。

此乃關鍵一步。為此被坑了好長時間,後來查閱很多才知道在mysql 5.7.9以後廢棄了password欄位和password()函式;

authentication_string:欄位表示使用者密碼。

三、修改root密碼的步驟:

一、如果當前root使用者authentication_string欄位下有內容,可先將其設定為空,不然直接進行二步驟。

update user set authentication_string='' where user='root';#密碼設定為空

二、使用ALTER修改root使用者密碼,方法為 ALTER user 'root'@'localhost' IDENTIFIED BY '新密碼'。如下:

alter user 'root'@'%' identified with mysql_native_password by 'xxxx';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'mypwd#2019';
或者 alter user 'root'@'localhost' identified with mysql_native_password by 'xxxx';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypwd#2019'

提示:

root@後面是user表的Host欄位的內容,新安裝預設是localhost,因為在這增加了遠端訪問,所以將localhost手動改成了%。

改完之後可執行:flush privileges;( 重新載入許可權表 )

flush privileges;

注意:mysql8.0之後的版本,下面方法已經不適用。切記!!!

UPDATE user SET password=PASSWORD("新密碼") WHERE user='使用者名稱';

以上文字如有不妥之處,還請大家詳細指正並留言,方便今後大家共同成長;

在此也希望本篇部落格能夠對大家有所幫助!