MySQL數(shù)據(jù)庫(kù)用戶權(quán)限管理
1、用戶管理
mysql的用戶信息保存在了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ī)名和用戶名共同組成復(fù)合主鍵 Host 主機(jī)名,允許訪問的客戶端,*代表所有客戶端都可以訪問 User 用戶名
1.1、創(chuàng)建用戶
方式一:直接使用root用戶在mysql.user中插入記錄(不推薦)
方式二:使用創(chuàng)建用戶的SQL指令
基本語法:
create user 用戶 identified by 明文密碼 -- 用戶 用戶名@主機(jī)地址 -- 主機(jī)地址: '' 或者 %
示例:
create user 'user1'@'%' identified by '123456'; -- 查看mysql.user表中是否存在新用戶 select user, host from mysql.user where user = 'user1'; +-------+------+ | user | host | +-------+------+ | user1 | % | +-------+------+
簡(jiǎn)化版創(chuàng)建用戶,誰都可以訪問,不需要密碼,不安全
create user user2;
1.2、刪除用戶
user和host具有唯一性
基本語法:
drop user 用戶名@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、修改用戶密碼
需要使用函數(shù)對(duì)密碼進(jìn)行加密password()
方式一:使用專門的修改密碼指令
基本語法:
set password for 用戶 = password(明文密碼); set password for 'user1'@'%' = password(654321); -- mysql5.7后續(xù)版本,8.0可用 alter user 'user1'@'%' identified by '654321';
方式二:使用更新語法
基本語法:
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)限管理
分為三類:
- 數(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)限分配給指定用戶
基本語法:
grant 權(quán)限列表 on 數(shù)據(jù)庫(kù)/*.表名/* to 用戶
- 權(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
基本語法:
revoke 權(quán)限列表 /all privileges on 數(shù)據(jù)庫(kù)/*.表/* from 用戶
-- 回收權(quán)限,不需要刷新,馬上生效 revoke all privileges on mydatabase.my_student from 'user1'@'%';
2.3、刷新權(quán)限 flush
將操作的具體內(nèi)容同步到對(duì)應(yīng)的表中
基本語法:
flush privileges;
3、密碼丟失的解決方案
如果忘記root用戶的密碼
# 停止服務(wù) mysql.server stop; # 停止不了可以直接殺死進(jìn)程 ps aux|grep mysql kill <pid> # 重新啟動(dòng)服務(wù),跳過權(quán)限 mysqld --skip-grant-tables # 直接無用戶名登錄 mysql
非常危險(xiǎn),任何客戶端不需要任何用戶信息都可以直接登錄,而且是root權(quán)限
修改root密碼:
alter user 'root'@'localhost' identified by '123456';
修改完后,關(guān)閉mysql服務(wù)器,重啟
到此這篇關(guān)于MySQL數(shù)據(jù)庫(kù)用戶權(quán)限管理的文章就介紹到這了,更多相關(guān)MySQL 權(quán)限管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL用戶和數(shù)據(jù)權(quán)限管理詳解
- MySQL權(quán)限控制和用戶與角色管理實(shí)例分析講解
- Navicat配置mysql數(shù)據(jù)庫(kù)用戶權(quán)限問題
- MySQL如何開啟用戶遠(yuǎn)程登錄權(quán)限
- MySQL設(shè)置用戶權(quán)限的簡(jiǎn)單步驟
- Mysql用戶創(chuàng)建以及權(quán)限賦予操作的實(shí)現(xiàn)
- MySQL授予用戶權(quán)限命令詳解
- Mysql用戶權(quán)限分配實(shí)戰(zhàn)項(xiàng)目詳解
- mysql 添加用戶并分配select權(quán)限的實(shí)現(xiàn)
相關(guān)文章
選擇MySQL數(shù)據(jù)庫(kù)的命令以及PHP腳本下的操作方法
這篇文章主要介紹了選擇MySQL數(shù)據(jù)庫(kù)的命令以及PHP腳本下的操作方法,此外文中還對(duì)MySQL的基本數(shù)據(jù)類型作了介紹,需要的朋友可以參考下2015-11-11
在Mac系統(tǒng)上配置MySQL以及Squel Pro
給大家講述一下如何在MAC蘋果系統(tǒng)上配置MYSQL數(shù)據(jù)庫(kù)以及Squel Pro的方法。2017-11-11
window環(huán)境配置Mysql 5.7.21 windowx64.zip免安裝版教程詳解
這篇文章主要介紹了window環(huán)境配置Mysql 5.7.21 windowx64.zip免安裝版教程詳解,需要的朋友可以參考下2018-02-02
MySQL8.0移除傳統(tǒng)的.frm文件原因及解讀
MySQL 8.0移除傳統(tǒng)的.frm文件,采用基于InnoDB的事務(wù)型數(shù)據(jù)字典,主要解決了元數(shù)據(jù)不一致、性能優(yōu)化、架構(gòu)簡(jiǎn)化、增強(qiáng)功能支持、兼容性與升級(jí)問題,這一變革提高了數(shù)據(jù)庫(kù)的可靠性和性能,為未來的高級(jí)功能奠定了基礎(chǔ)2025-03-03
mysql5.5 master-slave(Replication)主從配置
在主機(jī)master中對(duì)test數(shù)據(jù)庫(kù)進(jìn)行sql操作,再查看從機(jī)test數(shù)據(jù)庫(kù)是否產(chǎn)生同步。2011-07-07
MySQL數(shù)據(jù)備份之mysqldump的使用方法
mysqldump常用于MySQL數(shù)據(jù)庫(kù)邏輯備份,這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)備份之mysqldump使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11
DROP TABLE在不同數(shù)據(jù)庫(kù)中的寫法整理
這篇文章主要介紹了DROP TABLE在不同數(shù)據(jù)庫(kù)中的寫法整理的相關(guān)資料,需要的朋友可以參考下2017-04-04
mysql-canal-rabbitmq 安裝部署超詳細(xì)教程
這篇文章主要介紹了mysql-canal-rabbitmq 安裝部署超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

