1. 程式人生 > 實用技巧 >Mysql—新增使用者並授權

Mysql—新增使用者並授權

查詢所有使用者
-- 方式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;
查詢使用者許可權:all表示所有許可權,select表示只查許可權,update表示只改許可權,delete表示只刪許可權等。
-- 方式1
mysql> show grants for "user"@"host";
mysql> show grants for "root"@"localhost";

-- 方式2
mysql> select * from mysql.user where user='root'\G;
新增授權使用者(新建立的使用者,預設情況下是沒有任何許可權的):使用root使用者登入資料庫

命令格式如下:
mysql> create user "使用者名稱"@"IP地址" identified by "密碼";

mysql> create user "haidon" identified by "123456";       -- 此時密碼為123456,host值為%。
mysql> create user "haidon"@"%" identified by "123456";   -- 此時密碼為123456
分配使用者許可權(給使用者授權)

命令格式如下:
mysql> grant 許可權型別 on 資料庫名.表名 to '使用者名稱'@'ip地址' identified by '使用者密碼' with grant option;

常用的許可權型別有以下幾種:
all privileges:所有許可權。
select:讀取許可權。
create:建立許可權。
delete:刪除許可權。
update:更新許可權。
drop:刪除資料庫、資料表許可權。

-- 允許訪問所有資料庫下的所有表
mysql> grant all privileges on *.* to '使用者名稱'@'指定ip' identified by '使用者密碼' ;

-- 允許訪問指定資料庫下的所有表
mysql> grant all privileges on test.* to '使用者名稱'@'指定ip' identified by '使用者密碼' ;

-- 允許訪問指定資料庫下的指定表
mysql> grant all privileges on test.test to '使用者名稱'@'指定ip' identified by '使用者密碼' ;

mysql> grant all privileges on tornado.* to 'haidon'@'%' identified by '123456';
收回使用者許可權(使用root使用者操作)
mysql> revoke select on tornado.* from "haidon"@"%";
mysql> revoke all on tornado.* from "haidon"@"%";
刪除授權使用者
mysql> drop user "haidon"@"%";                      -- 刪除方法1
mysql> delete from mysql.user where user="haidon";  -- 刪除方法2
重新整理許可權
mysql> flush privileges; 

 https://www.cnblogs.com/sidesky/p/10650354.html

https://www.cnblogs.com/zhangjianqiang/p/10019809.html