tags: 數據庫 MySQL 用戶管理html
MySQL是一個多用戶數據庫,具備功能強大的訪問控制系統,能夠爲不一樣用戶指定容許的權限。mysql
MySQL用戶能夠分爲普通用戶和root用戶。root用戶是超級管理員,擁有全部權限,包括建立用戶、刪除用戶和修改用戶的密碼等管理權限;普通用戶只有被授予的各類權限。sql
用戶管理包括管理用戶帳戶、權限等。數據庫
MySQL服務器經過權限表來控制用戶對數據庫的訪問,權限表存放在MySQL數據庫中,由MySQL_install_db
腳本初始化。安全
存儲帳戶權限信息表主要有:user
、host
、db
、tables_priv
、columns_priv
、procs_priv
。本節主要介紹這些表的內容和做用。服務器
user
表是MySQL中最重要的一個權限表,記錄容許鏈接到服務器的帳號信息,這裏的權限是全局的。socket
執行mysql> describe mysql.user;
命令獲得以下顯示錶信息:ide
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Host | char(60) | NO | PRI | ||
User | char(32) | NO | PRI | ||
Select_priv | enum('N','Y') | NO | N | ||
Insert_priv | enum('N','Y') | NO | N | ||
Update_priv | enum('N','Y') | NO | N | ||
Delete_priv | enum('N','Y') | NO | N | ||
Create_priv | enum('N','Y') | NO | N | ||
Drop_priv | enum('N','Y') | NO | N | ||
Reload_priv | enum('N','Y') | NO | N | ||
Shutdown_priv | enum('N','Y') | NO | N | ||
Process_priv | enum('N','Y') | NO | N | ||
File_priv | enum('N','Y') | NO | N | ||
Grant_priv | enum('N','Y') | NO | N | ||
References_priv | enum('N','Y') | NO | N | ||
Index_priv | enum('N','Y') | NO | N | ||
Alter_priv | enum('N','Y') | NO | N | ||
Show_db_priv | enum('N','Y') | NO | N | ||
Super_priv | enum('N','Y') | NO | N | ||
Create_tmp_table_priv | enum('N','Y') | NO | N | ||
Lock_tables_priv | enum('N','Y') | NO | N | ||
Execute_priv | enum('N','Y') | NO | N | ||
Repl_slave_priv | enum('N','Y') | NO | N | ||
Repl_client_priv | enum('N','Y') | NO | N | ||
Create_view_priv | enum('N','Y') | NO | N | ||
Show_view_priv | enum('N','Y') | NO | N | ||
Create_routine_priv | enum('N','Y') | NO | N | ||
Alter_routine_priv | enum('N','Y') | NO | N | ||
Create_user_priv | enum('N','Y') | NO | N | ||
Event_priv | enum('N','Y') | NO | N | ||
Trigger_priv | enum('N','Y') | NO | N | ||
Create_tablespace_priv | enum('N','Y') | NO | N | ||
ssl_type | enum('','ANY', 'X509','SPECIFIED') |
NO | |||
ssl_cipher | blob | NO | NULL | ||
x509_issuer | blob | NO | NULL | ||
x509_subject | blob | NO | NULL | ||
max_questions | int(11) unsigned | NO | 0 | ||
max_updates | int(11) unsigned | NO | 0 | ||
max_connections | int(11) unsigned | NO | 0 | ||
max_user_connections | int(11) unsigned | NO | 0 | ||
plugin | char(64) | NO | mysql_native_password | ||
authentication_string | text | YES | NULL | ||
password_expired | enum('N','Y') | NO | N | ||
password_last_changed | timestamp | YES | NULL | ||
password_lifetime | smallint(5) unsigned | YES | NULL | ||
account_locked | enum('N','Y') | NO | N |
45 rows in set (0.00 sec)函數
字段說明:學習
user
表的用戶信息列包括Host
、User
、authentication_string
,其中Host
和User
爲表的聯合主鍵。authentication_sting
爲用戶密碼的哈希值。當一個用戶鏈接時,只有這3個值徹底匹配才被容許。_pri
的都是用戶權限字段,包括了增、刪、改、查等普通權限,還包括了關閉服務器、加載用戶等高級權限。普通權限用於對數據庫實施操做行爲的限制;高級權限用於數據庫管理行爲。user
表中對應的權限是針對全部用戶數據數據庫的。這些字段值的類型爲ENUM
,能夠取值只能爲Y和N。修改權限使用grant
語句和update
語句。
資源控制列
max_questions
- 用戶每小時容許執行的查詢操做次數。max_updates
- 用戶每小時容許執行的更新操做次數。max_connections
- 用戶每小時容許執行的鏈接操做次數。max_user_connections
- 用戶容許同時創建的鏈接次數。db
表和host
表是MySQL數據中很是重要的權限表。db
表中存儲了用戶對某個數據庫的操做權限,決定用戶能從哪一個主機存取那個數據庫。
執行mysql> describe mysql.db;
後顯示結果以下:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Host | char(60) | NO | PRI | ||
Db | char(64) | NO | PRI | ||
User | char(32) | NO | PRI | ||
Select_priv | enum('N','Y') | NO | N | ||
Insert_priv | enum('N','Y') | NO | N | ||
Update_priv | enum('N','Y') | NO | N | ||
Delete_priv | enum('N','Y') | NO | N | ||
Create_priv | enum('N','Y') | NO | N | ||
Drop_priv | enum('N','Y') | NO | N | ||
Grant_priv | enum('N','Y') | NO | N | ||
References_priv | enum('N','Y') | NO | N | ||
Index_priv | enum('N','Y') | NO | N | ||
Alter_priv | enum('N','Y') | NO | N | ||
Create_tmp_table_priv | enum('N','Y') | NO | N | ||
Lock_tables_priv | enum('N','Y') | NO | N | ||
Create_view_priv | enum('N','Y') | NO | N | ||
Show_view_priv | enum('N','Y') | NO | N | ||
Create_routine_priv | enum('N','Y') | NO | N | ||
Alter_routine_priv | enum('N','Y') | NO | N | ||
Execute_priv | enum('N','Y') | NO | N | ||
Event_priv | enum('N','Y') | NO | N | ||
Trigger_priv | enum('N','Y') | NO | N |
22 rows in set (0.04 sec)
host
表中存儲了某個主機對數據庫的操做權限,配合db
權限表對給定主機上的數據庫級操做權限作更細緻的控制,這個表不受grant
和revoke
語句的影響。此表在「Server version: 5.7.20 MySQL Community Server (GPL)」中並未發現。
這2張表分別對具體的表和表中的字段來設置權限。
tables_priv
表信息:
;mysql> describe mysql.tables_priv;
Field | Type | Null | Key | Default | Extra | |
---|---|---|---|---|---|---|
Host | char(60) | NO | PRI | |||
Db | char(64) | NO | PRI | |||
User | char(32) | NO | PRI | |||
Table_name | char(64) | NO | PRI | |||
Grantor | char(93) | NO | MUL | |||
Timestamp | timestamp | NO | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | ||
Table_priv | set('Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'Grant', 'References', 'Index', 'Alter', 'Create View', 'Show view', 'Trigger') |
NO | ||||
Column_priv | set('Select', 'Insert', 'Update', 'References') |
NO |
8 rows in set (0.04 sec)
columns_priv
表信息:
mysql> describe mysql.columns_priv;
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Host | char(60) | NO | PRI | ||
Db | char(64) | NO | PRI | ||
User | char(32) | NO | PRI | ||
Table_name | char(64) | NO | PRI | ||
Column_name | char(64) | NO | PRI | ||
Timestamp | timestamp | NO | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | |
Column_priv | set('Select', 'Insert', 'Update', 'References') |
NO |
7 rows in set (0.02 sec)
procs_priv
表能夠對存儲過程和存儲函數設置操做權限。
mysql> describe mysql.procs_priv;
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Host | char(60) | NO | PRI | ||
Db | char(64) | NO | PRI | ||
User | char(32) | NO | PRI | ||
Routine_name | char(64) | NO | PRI | ||
Routine_type | enum( 'FUNCTION', 'PROCEDURE') |
NO | PRI | NULL | |
Grantor | char(93) | NO | MUL | ||
Proc_priv | set('Execute', 'Alter Routine', 'Grant') |
NO | |||
Timestamp | timestamp | NO | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
rows in set (0.02 sec)
經過MySQL -help
命令能夠查看MySQL命令的幫助信息。MySQL命令的經常使用參數以下:
-h 主機名
:能夠指定主機名稱和IP,若是不指定,默認是localhost。
-P 端口號
:指定服務器的端口號,默認爲3306。-u 用戶名
:指定用戶名。-p密碼
:能夠用使用該參數指定登陸密碼,注意: 密碼與p
之間不能有空格。-e "SQL語句"
:若是指定了該語句,會在登陸後執行SQL語句。數據庫名稱
:能夠在命令的最後指定數據庫名稱。[例1] 用戶root
,密碼爲foo
,從主機localhost
登陸到MySQL服務器,設置test_db
數據庫爲登陸後的默認當前數據庫。
C:\mysql5.7.2\bin>mysql -h localhost -u root -pfoo test_db Enter password: ********* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.7.20 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select database(); +------------+ | database() | +------------+ | test_db | +------------+ 1 row in set (0.00 sec)
[例2] 使用root
用戶,密碼爲foo
,從主機localhost
登陸到MySQL服務器,設置缺省數據庫爲test_db
,並執行語句describe test1; select * from test1;
。
C:\mysql5.7.2\bin>mysql -h localhost -u root -pfoo test_db -e "describe test1; select * from test1" mysql: [Warning] Using a password on the command line interface can be insecure. +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | a1 | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ +------+ | a1 | +------+ | 1 | | 3 | | 4 | +------+
要退出MySQL服務器登陸,請使用quit
。
mysql> quit Bye C:\mysql5.7.2>bin>
要建立新用戶,必須具備相應的權限來執行建立操做。在MySQL數據庫中,有兩種方式建立新用戶,一種是使用create user
或grant
語句;另外一種是直接操做MySQL受權表。
create user
語句建立新用戶create user
語句基本語法格式以下:
CREATE USER user_specification[, user_specification]... user_specification: user@host [IDENTIFIED BY [PASSWORD] 'password' | IDENTIFIED WITH auth_plugin [AS 'auth_sting']]
值得注意的是:若是使用PASSWORD
關鍵字,後面的密碼要使用哈希值字符竄,不然'password'
使用明文密碼。
[例3] 使用create user
建立一個用戶,用戶名是jack
,密碼是pw
,主機名爲localhost
,語句以下:
mysql> create user `jack`@`localhost` identified by 'pw'; Query OK, 0 rows affected (0.24 sec) -- 若是使用password關鍵字則要以下操做: mysql> select password('pw'); +-------------------------------------------+ | password('pw') | +-------------------------------------------+ | *D821809F681A40A6E379B50D0463EFAE20BDD122 | +-------------------------------------------+ 1 row in set, 1 warning (0.03 sec) mysql> create user jack1@localhost identified by password '*D821809F681A40A6E379B50D0463EFAE20BDD122'; Query OK, 0 rows affected, 1 warning (0.00 sec)
grant
語句建立新用戶基本語法以下:
GRANT privileges ON [object_type] priv_level TO user@host [IDENTIFIED BY 'password'] [, user [IDENTIFIED BY 'password']] [WITH GRANT OPTION] object_type: { TABLE | FUNCTION | PROCEDURE } priv_level: { * | *.* | db_name.* | db_name.tbl_name | tbl_name | db_name.routine_name }
說明:WITH GRANT OPTION
子句表示對新創建的用戶賦予GRANT
權限,即該用戶能夠對其餘用戶賦予權限。
[例4] 使用grant
語句建立兩個新用戶jack2
和jack3
,密碼分別爲pw1
和pw2
,並授於他們對全部數據表的select和update權限。
mysql> grant select,update on table *.* to jack2@localhost identified by 'pw1', jack3@localhost identified by 'pw2'; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> show warnings;
Level | Code | Message |
---|---|---|
Warning | 1287 | Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement. |
Warning | 1287 | Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement. |
2 rows in set (0.00 sec) -- 查看警告可知,最好不要用grant語句來添加新用戶,而要用create user語句添加新用戶後再受權。以下所示,則不會出現警告: mysql> create user jack4@localhost identified by 'pw3'; Query OK, 0 rows affected (0.00 sec) mysql> grant select, update on table *.* to jack4@localhost; Query OK, 0 rows affected (0.00 sec)
對於用戶名究竟是使用user@host
,仍是user
呢?請點此進一步查看,簡單來講指定user就是要同時指定用戶和主機,這兩個是一體的。
在MySQL中create user
和grant
語句都是實際上都是對user
表進行操做,但通常不建議這樣作,除非特殊狀況或者極熟悉MySQL
的user
表中的各項設置才能夠。
[例5] 使用insert
建立一個新帳戶
mysql> insert into mysql.user (host,user,authentication_string) -> values('localhost', 'jack5', password('pw')); ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value mysql> show warnings;
Level | Code | Message |
---|---|---|
Warning | 1681 | 'PASSWORD' is deprecated and will be removed in a future release. |
Error | 1364 | Field 'ssl_cipher' doesn't have a default value |
Error | 1364 | Field 'x509_issuer' doesn't have a default value |
Error | 1364 | Field 'x509_subject' doesn't have a default value |
4 rows in set (0.01 sec)
由提示信息能夠看出並未添加成功,須要指定多個相關字段才能添加成功,因此請使用MySQL提供的標準的語句。
drop user
語句刪除用戶基本語法:
drop user user [, user];
[例6] 刪除用戶jack4@localhost。
mysql> drop user jack4@localhost; Query OK, 0 rows affected (0.02 sec)
[例7] 刪除用戶jack3@localhost。
mysql> delete from mysql.user where user='jack3' and host='localhost'; Query OK, 1 row affected (0.16 sec)
mysqladmin
命令在命令行中修改密碼[例8] 將root用戶的密碼修改成"xfoox"
C:\>mysqladmin -u root -h 192.168.1.33 -pfoo password "xfoox" mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
說明: -p
的密碼foo
是緊跟着的,不能有空格;password
後面的密碼要使用雙引號;若是省略掉-p
後面的密碼會要求你輸入原密碼。固然你可使用SSL鏈接服務器,以增長安全性,MySQL開啓SSL安全鏈接的文章請點此訪問
set password
語句修改密碼正常登錄服務器後,使用以下語句修改:
mysql> set password=password("foo"); Query OK, 0 rows affected (0.14 sec)
注意:在最新版本的MySQL8.0中並不支持password函數,只須要直接寫密碼便可。
mysql> update mysql.user set password=password("foo") where user = "root" and host = "localhost"; ERROR 1054 (42S22): Unknown column 'password' in 'field list' mysql> update mysql.user set authentication_string=password("foo") where user = "root" and host = "localhost"; Query OK, 0 rows affected, 1 warning (0.15 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.17 sec)
注意:通常不要使用這種方法,由於每一個版本的密碼存貯字段名可能不一樣,最後要記得使用flush privileges
語句來沖洗權限表,而後纔可使用新密碼登錄。
set password
語句修改mysql> set password for jack@localhost = password('foo'); Query OK, 0 rows affected, 1 warning (0.04 sec) mysql> show warnings; | Warning | 1287 | 'SET PASSWORD FOR <user> = PASSWORD('<plaintext_password>')' is deprecated and will be removed in a future release. Please use SET PASSWORD FOR <user> = '<plaintext_password>' instead | 1 row in set (0.00 sec)
grant usage
語句來修改mysql> grant usage on *.* to jack@localhost identified by 'foo'; Query OK, 0 rows affected, 1 warning (0.12 sec)
說明:警告提示仍然爲將來版本可能不支持該語句來修改用戶的密碼。
同root用戶。
普通用戶正常登錄後,使用以下語句修改:
mysql> set password = 'foo'; Query OK, 0 rows affected (0.07 sec)
mysql
)打開管理員命令窗口,中止本機的MySQL服務,再用參數--skip-grant-tables
來越過權限表的檢查來啓動服務。
注意,此時光標會不斷閃爍,不要關閉此窗口。
若是在MySQL8.0版本中,啓動服務的命令爲:E:\mysql8\bin>mysqld --skip-grant-tables --shared-memory --console
,附加--console
是爲了將信息輸出到控制檯,否則信息會不顯示。
E:\mysql8\bin>mysqld --skip-grant-tables --shared-memory --console 2018-05-14T11:53:03.811523Z 0 [System] [MY-010116] [Server] E:\mysql8\bin\mysqld.exe (mysqld 8.0.11) starting as process 4472 2018-05-14T11:53:06.244140Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2018-05-14T11:53:06.320312Z 0 [System] [MY-010931] [Server] E:\mysql8\bin\mysqld.exe: ready for connections. Version: '8.0.11' socket: '' port: 0 MySQL Community Server - GPL. 2018-05-14T11:53:06.455078Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible' (注:光標會在這裏閃爍) (注:在進行完第3步後,在這裏我按了Ctrl+c終止,下面是按鍵完畢後的輸出:) 2018-05-14T12:02:51.10644^5CZ 0 [System] E:\mysql8\bin> [MY-013105] [Server] E:\mysql8\bin\mysqld.exe: Normal shutdown. 2018-05-14T12:02:53.120117Z 0 [Warning] [MY-010909] [Server] E:\mysql8\bin\mysqld.exe: Forcing close of thread 9 user: 'root'. 2018-05-14T12:02:54.515625Z 0 [System] [MY-010910] [Server] E:\mysql8\bin\mysqld.exe: Shutdown complete (mysqld 8.0.11) MySQL Community Server - GPL.
打開新的命令窗口,使用root用戶登錄,因爲越過了權限表,此時雖然要求輸入密碼,但只須要回車便可,登陸後修改密碼。
C:\mysql -u root -p enter password: (此處直接回車便可) mysql> update mysql.user set authentication_string = password('foo') where user='root' and host='localhost'; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1
注意:在5.7版本之前的密碼是存儲在password
字段裏的,之後的版本存儲在authentication_string
字段裏,而且在MySQL8.0之後,password()
函數也被取消了,採用了caching_sha2_password
的加密驗證方式,因此,只能把密碼字段先用空白字竄替換,即:authentication_string=''
,而後從新登錄,由於密碼爲空白,因此只須要回車便可,而後使用alter user root@localhost identified by 'foo';
語句來更改密碼。
刷新權限
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
最後關閉管理員窗口,退出登陸,便可從新使用新密碼鏈接服務器。
注意:有時關閉了管理員命令窗口,通常MySQL服務仍然會在後臺運行,但有時卻不行。若是不能正常登錄,請查看進程,殺掉後,再使用net start mysql
啓動MySQL服務便可.(MySQL服務名字能夠自定義的,請按本身的狀況處理)