1. 程式人生 > >mysql 5.7 使用者操作

mysql 5.7 使用者操作

mysql> use mysql;

mysql> selecthost,user from user;

檢視結果是不是root使用者僅允許本地(localhost)登入,下面這個截圖就是這種情況.

是的話,就要修改它的host為%,表示任意IP地址都可以登入.

\

mysql> update user set host = '%' where user = 'root';

執行完後可能提示error.再mysql> select host,user from user;檢視下吧.

root對應的host成了%,表示可以任意IP地址登入了.

\

mysql> flush privileges;

把快取flush掉.在使用update語句修改使用者記錄後,需要FLUSH語句告訴伺服器過載授權表.

一、修改密碼

mysql -u root -p 

update mysql.user setauthentication_string=password(“新密碼”) where User=”test” and Host=”localhost”; 

flush privileges;

mysql5.7以後mysql.user表中沒有了password欄位,而是使用authentication_string來代替。

二、刪除使用者

mysql -u root -p 

Delete FROM mysql.user Where User="使用者名稱" and Host=”localhost”; 

flush privileges; 

建立使用者

命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password';

刪除賬戶及許可權:

drop user 使用者名稱@’%’;

drop user 使用者名稱@ localhost;

三、為使用者授權

授權格式:grant 許可權 on 資料庫.* to 使用者名稱@登入主機 identified by “密碼”;

eg: grant all privileges on *.* to 'root'@'192.168.218.128' identified by 'hello' with grant option;

flush privileges; //要重新整理許可權

授權test使用者擁有testDB資料庫的所有許可權:

grant all privileges on testDB.* to “test”@”localhost” identified by “1234”; 
flush privileges; #刷新系統許可權表

指定部分許可權給使用者:

grant select,update on testDB.* to “test”@”localhost” identified by “1234”; 
flush privileges; #刷新系統許可權表

四、顯示當前使用者資訊

select user();