MySQL增加/刪除用戶、授權、修改密碼等方法和命令總結

1. MySQL添加用戶
2. MySQL修改用戶密碼
3. 授權和取消權限
4. MySQL刪除用戶

1. MySQL添加用戶
1.1 通過直接操作MySQL的user表新增用戶

// 登錄MySQL
[yongfu]$ mysql -uroot -p
[yongfu]$ password:

// 創建用戶
mysql> use mysql;
mysql> insert into user(Host,User,Password) values('localhost','yongfu_a',password('my_password'));

// 刷新系統權限表
mysql> flush privileges;

//退出後登錄一下
mysql>exit;

[yongfu]$ mysql -uyongfu_a -p
[yongfu]$ password: 輸入密碼
mysql> login success

// 說明:
至此創建mysql的第二個用戶:yongfu_a(第一個用戶是root),他的密碼是my_password。需要刷新系統權限表flush privilege,該用戶才能生效登錄。此時該用戶(yongfu_a)僅有在本機(localhost)使用密碼(my_password)登錄的權 限,還沒有其他權限,需要使用GRANT命令對該用戶進行相應授權。

1.2 通過GRANT授權的方式新增用戶
通過上面1.1的方式添加的用戶是沒有任何權限的,對用戶進行授權使用GRANT命令。
// 以root用戶登錄mysql
[yongfu]$ mysql -uroot -p

// 新增一個新用戶yongfu_b
mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by 'my_password_2';

// 說明:
至此創建了mysql的第三個用戶:yongfu_b,他的密碼是my_password_2。不需要使用flush privilege刷新系統權限表,改用戶立即生效。此時該用戶(yongfu_b)可以在192.168.1.0/24的網段上的任意機器使用密碼 (my_password_2)登錄。同時已經對所有庫的所有表賦予了全部權限。
PS:mysql> grant 權限1,權限2,…權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by ‘連接口令’;

權限1,權限2,…權限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個權限。
當權限1,權限2,…權限n被all privileges或者all代替,表示賦予用戶全部權限。
當數據庫名稱.表名稱被*.*代替,表示賦予用戶操作服務器上所有數據庫所有表的權限。
用戶地址可以是localhost,也可以是ip地址、機器名字、域名。也可以用’%'表示從任何地址連接。
‘連接口令’可以爲空,即無密碼。

// 總結
從上面兩種方法可以看出,使用grant方法新增用戶更方便也更省事。只需要一步就可以完成新增用戶和用戶授權的全部操作。

2. MySQL修改用戶密碼
2.1 直接修改數據庫修改密碼

// 以root用戶登錄mysql
[yongfu]$ mysql -uroot -p

// 在數據庫中修改密碼字段
mysql> use mysql
mysql> update user set Password = password("new_password") where User = "yongfu_a" and Host="localhost";
mysql> flush privileges;

2.2 通過GRANT方法修改密碼
// 其實就是新增用戶那個方法,grant本身是授權、授予的意思。
// 修改用戶yongfu_b的密碼爲 my_password_new
mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by 'my_password_new';

2.3 通過mysqladmin修改密碼
// mysql初始化安裝好後,root用戶默認是沒有密碼的,可以通過以下命令設置密碼(注意:下面命令後面不需要跟分號;否則會把分號也當成密碼的一部分):
[yongfu]$ mysqladmin -u root password "my_password"

// 已經設置過root密碼,想要修改密碼,使用如下命令:
[yongfu]$ mysqladmin -u root -p"my_password" password "my_new_password"

// 修改密碼,需要SUPER權限,我們嘗試用mysqladmin命令修改yongfu_a的密碼,得到如下錯誤
[yongfu]$ mysqladmin -u yongfu_a -p password 'newnew_pass'
mysqladmin: Can't turn off logging; error: 'Access denied; you need the SUPER privilege for this operation'

// 錯誤原因:yongfu_a只有登錄權限,而沒有其他任何權限,而修改密碼需要SUPER權限。
// 以root用戶登錄mysql
[yongfu]$ mysql -uroot -p
// 給yongfu_a賦予SUPER的權限
mysql>  GRANT SUPER on *.* to yongfu_a@'localhost';
mysql> flush privileges;
mysql> exit;

// 再次使用上面的命令,正確輸入原來密碼,就可以將yongfu_a的密碼設置爲newnew_pass
[yongfu]$ mysqladmin -u yongfu_a -p password 'newnew_pass'
Enter password: 輸入原密碼

// 注意事項
儘量不要使用這種方式設置密碼,因爲shell會記錄你輸入的命令的歷史情況,這樣駭客或者別的人很容易的就可以從你的shell命令歷史中知道你的mysql密碼。

3. 授權和取消權限
3.1 授權
mysql使用GRANT命令對用戶進行授權。前面我們已經數次接觸到GRANT命令,用它添加了一個用戶,也爲用戶修改了密碼,同時爲yongfu_a用戶授予了SUPER權限。其實GRANT設計的目的本身只是爲了授權的。
// 首先我們查看 yongfu_a 用戶有哪些權限(用root登錄,並use mysql
mysql> select * from user where User='yongfu_a' \G;  
*************************** 1. row ***************************
                 Host: localhost
                 User: yongfu_a
             Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: N
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0

// 從上面可以看出,yongfu_a 用戶只有前面授權的SUPER權限。
// 怎麼給yongfu_a用戶授予增刪改查的權限?使用如下命令:
mysql> GRANT SELECT,UPDATE,INSERT,DELETE on *.* to yongfu_a@'localhost';              
Query OK, 0 rows affected (0.00 sec)

// 再查看下權限情況:
mysql> select * from user where User='yongfu_a' \G;                     
*************************** 1. row ***************************
                 Host: localhost
                 User: yongfu_a
             Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0

// 這時已經被授予了增刪改查的權限了,同時發現SUPER權限還是保留的。
// 其實GRANT是不會取消權限的,取消權限使用REVOKE命令。

3.2 取消授權
// REVOKE語句的語法格式如下:
// 其中privileges是要取消的權限,user是要被取消權限的用戶名, 特別注意user前的關鍵字變爲了from。
REVOKE privileges (columns) on what from user ;

// 取消 yongfu_a用戶的增刪改查權限
mysql> REVOKE SELECT,UPDATE,INSERT,DELETE on *.* from yongfu_a@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user where User='yongfu_a' \G;                        
*************************** 1. row ***************************
                 Host: localhost
                 User: yongfu_a
             Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: N
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0

REVOKE語句用於取消用戶的權限,而不可以刪除用戶。

4. 刪除用戶
取消了所有的權限,用戶仍然可以連接到服務器。要想徹底的刪除用戶,必須使用DELETE語句將該用戶的記錄從mysql數據庫中的user表中刪除。
// 使用DELETE刪除用戶yongfu_a,代碼如下:
mysql> use mysql
Database changed
mysql> delete from user where user='yongfu_a' and host='localhost' ;
mysql>flush privileges ;
Query OK, 1 row affected (0.02 sec)
其中,delete用於刪除用戶,flush告訴服務器重新加載授權表。

增加:所有權限列表:

Table 13.1. Permissible Privileges for GRANT and REVOKE

Privilege Meaning
ALL [PRIVILEGES] Grant all privileges at specified access level except GRANT OPTION
ALTER Enable use of ALTER TABLE
ALTER ROUTINE Enable stored routines to be altered or dropped
CREATE Enable database and table creation
CREATE ROUTINE Enable stored routine creation
CREATE TEMPORARY TABLES Enable use of CREATE TEMPORARY TABLE
CREATE USER Enable use of CREATE USERDROP USERRENAME USER, and REVOKE ALL PRIVILEGES
CREATE VIEW Enable views to be created or altered
DELETE Enable use of DELETE
DROP Enable databases, tables, and views to be dropped
EVENT Enable use of events for the Event Scheduler
EXECUTE Enable the user to execute stored routines
FILE Enable the user to cause the server to read or write files
GRANT OPTION Enable privileges to be granted to or removed from other accounts
INDEX Enable indexes to be created or dropped
INSERT Enable use of INSERT
LOCK TABLES Enable use of LOCK TABLES on tables for which you have the SELECT privilege
PROCESS Enable the user to see all processes with SHOW PROCESSLIST
REFERENCES Not implemented
RELOAD Enable use of FLUSH operations
REPLICATION CLIENT Enable the user to ask where master or slave servers are
REPLICATION SLAVE Enable replication slaves to read binary log events from the master
SELECT Enable use of SELECT
SHOW DATABASES Enable SHOW DATABASES to show all databases
SHOW VIEW Enable use of SHOW CREATE VIEW
SHUTDOWN Enable use of mysqladmin shutdown
SUPER Enable use of other administrative operations such as CHANGE MASTER TOKILLPURGE BINARY LOGSSET GLOBAL, and mysqladmin debug command
TRIGGER Enable trigger operations
UPDATE Enable use of UPDATE
USAGE Synonym for no privileges
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章