MySQL數(shù)據(jù)庫(kù)用戶(hù)權(quán)限管理
1、用戶(hù)管理
mysql的用戶(hù)信息保存在了mysql.user中:
select * from mysql.user\G *************************** 5. row *************************** Host: localhost User: root Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: Y References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 password_expired: N password_last_changed: 2020-02-05 22:46:27 password_lifetime: NULL account_locked: N Create_role_priv: Y Drop_role_priv: Y Password_reuse_history: NULL Password_reuse_time: NULL Password_require_current: NULL User_attributes: NULL
主要字段:
主機(jī)名和用戶(hù)名共同組成復(fù)合主鍵 Host 主機(jī)名,允許訪(fǎng)問(wèn)的客戶(hù)端,*代表所有客戶(hù)端都可以訪(fǎng)問(wèn) User 用戶(hù)名
1.1、創(chuàng)建用戶(hù)
方式一:直接使用root用戶(hù)在mysql.user中插入記錄(不推薦)
方式二:使用創(chuàng)建用戶(hù)的SQL指令
基本語(yǔ)法:
create user 用戶(hù) identified by 明文密碼 -- 用戶(hù) 用戶(hù)名@主機(jī)地址 -- 主機(jī)地址: '' 或者 %
示例:
create user 'user1'@'%' identified by '123456'; -- 查看mysql.user表中是否存在新用戶(hù) select user, host from mysql.user where user = 'user1'; +-------+------+ | user | host | +-------+------+ | user1 | % | +-------+------+
簡(jiǎn)化版創(chuàng)建用戶(hù),誰(shuí)都可以訪(fǎng)問(wèn),不需要密碼,不安全
create user user2;
1.2、刪除用戶(hù)
user和host具有唯一性
基本語(yǔ)法:
drop user 用戶(hù)名@host;
示例:
mysql> drop user 'user1'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> select user, host from mysql.user where user = 'user1'; Empty set (0.00 sec)
1.3、修改用戶(hù)密碼
需要使用函數(shù)對(duì)密碼進(jìn)行加密password()
方式一:使用專(zhuān)門(mén)的修改密碼指令
基本語(yǔ)法:
set password for 用戶(hù) = password(明文密碼); set password for 'user1'@'%' = password(654321); -- mysql5.7后續(xù)版本,8.0可用 alter user 'user1'@'%' identified by '654321';
方式二:使用更新語(yǔ)法
基本語(yǔ)法:
update mysql.user set password = password(明文密碼) where user = '' and host = ''; update mysql.user set password = password('123456') where user = 'user1' and host = '%'; -- 8.0報(bào)錯(cuò) update mysql.user set authentication_string = password('123456') where user = 'user1' and host = '%';
2、權(quán)限管理
分為三類(lèi):
- 數(shù)據(jù)權(quán)限:增刪改查 select update delete insert
- 結(jié)構(gòu)權(quán)限:結(jié)構(gòu)操作(表操作) create drop
- 管理權(quán)限:權(quán)限管理 create user、grant、revoke, 管理員
2.1、授予權(quán)限 grant
將權(quán)限分配給指定用戶(hù)
基本語(yǔ)法:
grant 權(quán)限列表 on 數(shù)據(jù)庫(kù)/*.表名/* to 用戶(hù)
- 權(quán)限列表 使用逗號(hào)間隔,all privileges 代表全部權(quán)限
- 所有數(shù)據(jù)庫(kù)
*.*
- 某個(gè)數(shù)據(jù)庫(kù):
數(shù)據(jù)庫(kù).*
- 單表:
數(shù)據(jù)庫(kù).表名
-- 分配權(quán)限 不需要刷新,馬上生效 grant select on mydatabase.my_student to 'user1'@'%';
2.2、取消權(quán)限 revoke
基本語(yǔ)法:
revoke 權(quán)限列表 /all privileges on 數(shù)據(jù)庫(kù)/*.表/* from 用戶(hù)
-- 回收權(quán)限,不需要刷新,馬上生效 revoke all privileges on mydatabase.my_student from 'user1'@'%';
2.3、刷新權(quán)限 flush
將操作的具體內(nèi)容同步到對(duì)應(yīng)的表中
基本語(yǔ)法:
flush privileges;
3、密碼丟失的解決方案
如果忘記root用戶(hù)的密碼
# 停止服務(wù) mysql.server stop; # 停止不了可以直接殺死進(jìn)程 ps aux|grep mysql kill <pid> # 重新啟動(dòng)服務(wù),跳過(guò)權(quán)限 mysqld --skip-grant-tables # 直接無(wú)用戶(hù)名登錄 mysql
非常危險(xiǎn),任何客戶(hù)端不需要任何用戶(hù)信息都可以直接登錄,而且是root權(quán)限
修改root密碼:
alter user 'root'@'localhost' identified by '123456';
修改完后,關(guān)閉mysql服務(wù)器,重啟
到此這篇關(guān)于MySQL數(shù)據(jù)庫(kù)用戶(hù)權(quán)限管理的文章就介紹到這了,更多相關(guān)MySQL 權(quán)限管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL用戶(hù)和數(shù)據(jù)權(quán)限管理詳解
- MySQL權(quán)限控制和用戶(hù)與角色管理實(shí)例分析講解
- Navicat配置mysql數(shù)據(jù)庫(kù)用戶(hù)權(quán)限問(wèn)題
- MySQL如何開(kāi)啟用戶(hù)遠(yuǎn)程登錄權(quán)限
- MySQL設(shè)置用戶(hù)權(quán)限的簡(jiǎn)單步驟
- Mysql用戶(hù)創(chuàng)建以及權(quán)限賦予操作的實(shí)現(xiàn)
- MySQL授予用戶(hù)權(quán)限命令詳解
- Mysql用戶(hù)權(quán)限分配實(shí)戰(zhàn)項(xiàng)目詳解
- mysql 添加用戶(hù)并分配select權(quán)限的實(shí)現(xiàn)
相關(guān)文章
mysql8.0.11 winx64安裝配置方法圖文教程(win10)
這篇文章主要為大家詳細(xì)介紹了win10下mysql8.0.11 winx64安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05MySQL 可以用localhost 連接,但不能用IP連接的問(wèn)題解決方法
這篇文章主要介紹了MySQL 可以用localhost 連接,但不能用IP連接的問(wèn)題解決方法的相關(guān)資料,這里提供了解決方案,需要的朋友可以參考下2016-12-12mysql登錄報(bào)錯(cuò)提示:ERROR 1045 (28000)的解決方法
這篇文章主要介紹了mysql登錄報(bào)錯(cuò)提示:ERROR 1045 (28000)的解決方法,詳細(xì)分析了出現(xiàn)MySQL登陸錯(cuò)誤的原因與對(duì)應(yīng)的解決方法,需要的朋友可以參考下2016-04-04mysql如何查詢(xún)當(dāng)前數(shù)據(jù)庫(kù)中不為空的表
這篇文章主要介紹了mysql如何查詢(xún)當(dāng)前數(shù)據(jù)庫(kù)中不為空的表問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10MySQL三大日志(binlog、redo?log和undo?log)圖文詳解
日志是MySQL數(shù)據(jù)庫(kù)的重要組成部分,記錄著數(shù)據(jù)庫(kù)運(yùn)行期間各種狀態(tài)信息,下面這篇文章主要給大家介紹了關(guān)于MySQL三大日志(binlog、redo?log和undo?log)的相關(guān)資料,需要的朋友可以參考下2023-01-01MySQL觸發(fā)器運(yùn)用于遷移和同步數(shù)據(jù)的實(shí)例教程
這篇文章主要介紹了MySQL觸發(fā)器運(yùn)用于遷移和同步數(shù)據(jù)的實(shí)例教程,分別是SQL Server數(shù)據(jù)遷移至MySQL以及同步備份數(shù)據(jù)表記錄的兩個(gè)例子,需要的朋友可以參考下2015-12-12MySQL5.7 mysqldump備份與恢復(fù)的實(shí)現(xiàn)
這篇文章主要介紹了MySQL5.7 mysqldump備份與恢復(fù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11SQL面試題:求時(shí)間差之和(有重復(fù)不計(jì))
這篇文章主要介紹了SQL面試題:求時(shí)間差之和(有重復(fù)不計(jì)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11