溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql中怎么添加用戶并授權(quán)

發(fā)布時(shí)間:2021-07-13 16:26:27 來源:億速云 閱讀:171 作者:Leah 欄目:編程語言

mysql中怎么添加用戶并授權(quán),針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

查詢所有用戶

方式1:

mysql> select host, user, password from mysql.user;               -- 5.7版本之前的
mysql> select host, user, authentication_string from mysql.user;  -- 5.7版本之后的,包括5.7

方式2:

mysql> select distinct concat('User: ''',user,'''@''',host,''';') as query from mysql.user;

查詢用戶權(quán)限

all表示所有權(quán)限

select表示只查權(quán)限

update表示只改權(quán)限

delete表示只刪權(quán)限等。

(在線學(xué)習(xí)視頻教程推薦:mysql視頻教程)

方式1:

mysql> show grants for "user"@"host";
mysql> show grants for "root"@"localhost";

方式2:

mysql> select * from mysql.user where user='root'\G;

添加授權(quán)用戶(新創(chuàng)建的用戶,默認(rèn)情況下是沒有任何權(quán)限的):使用root用戶登錄數(shù)據(jù)庫

命令格式如下:

mysql> create user "用戶名"@"IP地址" identified by "密碼";
mysql> create user "haidon" identified by "123456";       -- 此時(shí)密碼為123456,host值為%。
mysql> create user "haidon"@"%" identified by "123456";   -- 此時(shí)密碼為123456

分配用戶權(quán)限(給用戶授權(quán))

命令格式如下:

mysql> grant 權(quán)限類型 on 數(shù)據(jù)庫名.表名 to '用戶名'@'ip地址' identified by '用戶密碼' with grant option;

常用的權(quán)限類型有以下幾種:

all privileges:所有權(quán)限。
select:讀取權(quán)限。
create:創(chuàng)建權(quán)限。
delete:刪除權(quán)限。
update:更新權(quán)限。
drop:刪除數(shù)據(jù)庫、數(shù)據(jù)表權(quán)限。

允許訪問所有數(shù)據(jù)庫下的所有表

mysql> grant all privileges on *.* to '用戶名'@'指定ip' identified by '用戶密碼' ;

允許訪問指定數(shù)據(jù)庫下的所有表

mysql> grant all privileges on test.* to '用戶名'@'指定ip' identified by '用戶密碼' ;

允許訪問指定數(shù)據(jù)庫下的指定表

mysql> grant all privileges on test.test to '用戶名'@'指定ip' identified by '用戶密碼' ;
 
mysql> grant all privileges on tornado.* to 'haidon'@'%' identified by '123456';

收回用戶權(quán)限(使用root用戶操作)

mysql> revoke select on tornado.* from "haidon"@"%";
mysql> revoke all on tornado.* from "haidon"@"%";

刪除授權(quán)用戶

mysql> drop user "haidon"@"%";                      -- 刪除方法1
mysql> delete from mysql.user where user="haidon";  -- 刪除方法2

刷新權(quán)限

mysql> flush privileges;

關(guān)于mysql中怎么添加用戶并授權(quán)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI