MySQL用戶
首先要知道,MySQL用戶和系統用戶不同,前者用於MySQL數據庫的訪問登錄,而後者則是負責系統的訪問登錄,互不相關。
MySQL用戶的創建,是根據用戶名和客戶端主機IP來定義賬戶,MySQL將創建的用戶賬戶存儲在mysql庫的user表。
MySQL提供的特權
注:只列舉了常用
選項 | 作用 |
---|---|
ALL[PRIVILEGES] | 所有特權 |
ALTER | 允許使用ALTER TABLE語句更改表的結構 |
CREATE | 允許使用創建新庫和表 |
CREATE USER | 允許使用創,刪,改用戶的語句 |
DELETE | 允許刪除表中記錄 |
DROP | 允許刪除庫,表,視圖 |
EVENT | 允許使用事務,並且可以在事務中增刪改查 |
GRANT OPTION | 允許授予自己擁有的特權或從其他用戶撤消特權 |
INDEX | 允許使用創建或刪除索引的語句 |
INSERT | 允許在表中插入數據 |
RELOAD | 允許使用FLUSH語句,flush-logs等 |
REPLICATION CLIENT | 允許使用的SHOW MASTER STATUS,SHOW SLAVE STATUS和SHOW BINARY LOGS語句。將此特權授予從屬服務器用於將其作爲主服務器連接到當前服務器的帳戶 |
REPLICATION SLAVE | 啓用該帳戶已作出對數據庫的主服務器上,使用請求更新 SHOW SLAVE HOSTS, SHOW RELAYLOG EVENTS和 SHOW BINLOG EVENTS 語句 |
LOCK TABLES | 允許使用顯式LOCK TABLES語句來鎖定您具有SELECT特權的表。這包括使用寫鎖,這可以防止其他會話讀取鎖定的表 |
SELECT | 允許使用查詢語句 |
TRIGGER | 啓用觸發器功能,具有此的特權才能爲該表創建,刪除,執行或顯示觸發器 |
UPDATE | 允許修改表中記錄 |
USAGE | 無特權 |
創建MySQL用戶
語法:
create user ‘用戶’@‘主機IP’ identified by '密碼’
注:identified by '密碼’可以省略,表示用戶無密碼
例如:創建一個名爲user的用戶,範圍是本地,密碼爲password。
mysql> create user 'user'@'localhost' identified by 'password';
對於主機範圍,若不是本地,可以指定具體的IP地址或地址範圍。例如:192.168.1.%爲192.168.1.0網段,192.168.1.1爲具體的主機,不爲範圍內的主機,即使用戶密碼正確也無法登錄。
使用其他主機,通過user用戶登錄
[root@linux ~]# mysql -uuser -h 192.168.1.124 -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user'@'192.168.1.125' (using password: YES)
-h:MySQL服務器IP地址
可以看到拒絕訪問,就是因爲主機範圍設置爲了localhost,而在主機即可直接登錄。
用戶授權
語法:
grant 權限1 權限2… on 庫名.表名 to ‘用戶’@'主機範圍’
爲user用戶授予查詢數據的權限
mysql> grant select on *.* to 'user'@'localhost';
*.*:代表所有庫下的所有表
授權完成後,刷新一下權限即可立即生效
mysql> flush privileges;
注:授權或撤權後,都需要刷新權限,才能立即生效
查詢權限
通過查詢mysql庫下的user表,即可查詢用戶所擁有的權限。
例如:查詢user用戶權限
mysql> select * from mysql.user where user='user'\G
*************************** 1. row ***************************
Host: localhost
User: user
Select_priv: Y
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: N
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
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: caching_sha2_password
authentication_string: $A$005$rXDKtJkm?,=57xd
FrgO8MdSqm.esH01ZmBGmc5/0tU3ibNWo1PLZhc5Q3c4
password_expired: N
password_last_changed: 2020-05-29 15:35:38
password_lifetime: NULL
account_locked: N
Create_role_priv: N
Drop_role_priv: N
Password_reuse_history: NULL
Password_reuse_time: NULL
Password_require_current: NULL
其中Select_priv項爲Y,表示權限授予成功。
用戶撤權
語法:
revoke 權限1 權限2… on 庫名.表名 from ‘用戶名’@'主機範圍’
將user用戶的select權限撤銷
mysql> revoke select on *.* from 'user'@'localhost';
刷新權限
mysql> flush privileges;
查看用戶權限
mysql> select * from mysql.user where user='user'\G
*************************** 1. row ***************************
Host: localhost
User: user
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: N
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
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: caching_sha2_password
authentication_string: $A$005$rXDKtJkm?,=57xd
FrgO8MdSqm.esH01ZmBGmc5/0tU3ibNWo1PLZhc5Q3c4
password_expired: N
password_last_changed: 2020-05-29 15:35:38
password_lifetime: NULL
account_locked: N
Create_role_priv: N
Drop_role_priv: N
Password_reuse_history: NULL
Password_reuse_time: NULL
Password_require_current: NULL
其中Select_priv項已經改爲了N
修改用戶密碼
方式1
語法:
alter user ‘用戶名’@‘主機範圍’ identified by ‘密碼’
方式2
語法:
myqladmin -u用戶 -p密碼 password “密碼”
這兩種方式都可爲用戶修改密碼,任選一種即可
例如:爲user用戶修改密碼,將密碼改爲test
mysqladmin -uuser -p123456 password "test"
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.
以上密碼就修改完成了,通過新密碼即可登錄
[root@linux ~]# mysql -user -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ser'@'localhost' (using password: YES)
[root@linux ~]# mysql -uuser -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 8.0.13 Source distribution
Copyright (c) 2000, 2018, 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>
刪除用戶
語法:
drop user ‘用戶’@'主機範圍’
例如:將user用戶刪除
mysql> drop user 'user'@'localhost';
再次查看,用戶已經消失
mysql> select * from mysql.user where user='user'\G
Empty set (0.00 sec)