首页 文章资讯内容详情

mysql 用户管理和权限设置

2026-05-31 5 花语

本文内容纲要:

用户管理

mysql>usemysql;

查看

mysql>selecthost,user,passwordfromuser;

创建

mysql>createuserzx_rootIDENTIFIEDbyxxxxx;//identifiedby会将纯文本密码加密作为散列值存储

修改

mysql>renameuserfengtonewuser;//mysql5之后可以使用,之前需要使用update更新user表

删除

mysql>dropusernewuser;//mysql5之前删除用户时必须先使用revoke删除用户权限,然后删除用户,mysql5之后drop命令可以删除用户的同时删除用户的相关权限

更改密码

mysql>setpasswordforzx_root=password(xxxxxx);

mysql>updatemysql.usersetpassword=password(xxxx)whereuser=otheruser

查看用户权限

mysql>showgrantsforzx_root;

赋予权限

mysql>grantselectondmc_db.*tozx_root;

回收权限

mysql>revokeselectondmc_db.*fromzx_root;//如果权限不存在会报错

上面的命令也可使用多个权限同时赋予和回收,权限之间使用逗号分隔

mysql>grantselect,update,delete,insertondmc_db.*tozx_root;

如果想立即看到结果使用

flushprivileges;

命令更新

设置权限时必须给出一下信息

1,要授予的权限

2,被授予访问权限的数据库或表

3,用户名

grant和revoke可以在几个层次上控制访问权限

1,整个服务器,使用grantALL和revokeALL

2,整个数据库,使用ondatabase.*

3,特点表,使用ondatabase.table

4,特定的列

5,特定的存储过程

user表中host列的值的意义

%匹配所有主机

localhostlocalhost不会被解析成IP地址,直接通过UNIXsocket连接

127.0.0.1会通过TCP/IP协议连接,并且只能在本机访问;

::1::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

grant普通数据用户,查询、插入、更新、删除数据库中所有表数据的权利。

grantselectontestdb.*tocommon_user@’%’

grantinsertontestdb.*tocommon_user@’%’

grantupdateontestdb.*tocommon_user@’%’

grantdeleteontestdb.*tocommon_user@’%’

或者,用一条MySQL命令来替代:

grantselect,insert,update,deleteontestdb.*tocommon_user@’%’

9>.grant数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。

grant创建、修改、删除MySQL数据表结构权限。

grantcreateontestdb.*todeveloper@’192.168.0.%’;

grantalterontestdb.*todeveloper@’192.168.0.%’;

grantdropontestdb.*todeveloper@’192.168.0.%’;

grant操作MySQL外键权限。

grantreferencesontestdb.*todeveloper@’192.168.0.%’;

grant操作MySQL临时表权限。

grantcreatetemporarytablesontestdb.*todeveloper@’192.168.0.%’;

grant操作MySQL索引权限。

grantindexontestdb.*todeveloper@’192.168.0.%’;

grant操作MySQL视图、查看视图源代码权限。

grantcreateviewontestdb.*todeveloper@’192.168.0.%’;

grantshowviewontestdb.*todeveloper@’192.168.0.%’;

grant操作MySQL存储过程、函数权限。

grantcreateroutineontestdb.*todeveloper@’192.168.0.%’;--now,canshowprocedurestatus

grantalterroutineontestdb.*todeveloper@’192.168.0.%’;--now,youcandropaprocedure

grantexecuteontestdb.*todeveloper@’192.168.0.%’;

10>.grant普通DBA管理某个MySQL数据库的权限。

grantallprivilegesontestdbtodba@’localhost’

其中,关键字“privileges”可以省略。

11>.grant高级DBA管理MySQL中所有数据库的权限。

grantallon*.*todba@’localhost’

12>.MySQLgrant权限,分别可以作用在多个层次上。

grant作用在整个MySQL服务器上:

grantselecton*.*todba@localhost;--dba可以查询MySQL中所有数据库中的表。

grantallon*.*todba@localhost;--dba可以管理MySQL中的所有数据库

grant作用在单个数据库上:

grantselectontestdb.*todba@localhost;--dba可以查询testdb中的表。

grant作用在单个数据表上:

grantselect,insert,update,deleteontestdb.orderstodba@localhost;

grant作用在表中的列上:

grantselect(id,se,rank)ontestdb.apache_logtodba@localhost;

grant作用在存储过程、函数上:

grantexecuteonproceduretestdb.pr_addto’dba’@’localhost’

grantexecuteonfunctiontestdb.fn_addto’dba’@’localhost’

注意:修改完权限以后一定要刷新服务,或者重启服务,刷新服务用:FLUSHPRIVILEGES。

权限表

权限 说明 all alter alterroutine 使用alterprocedure和dropprocedure create createroutine 使用create procedure createtemporarytables 使用createtemporarytable create user createview delete drop execute 使用call和存储过程 file 使用selectintooutfile 和loaddatainfile grantoption 可以使用grant和revoke index 可以使用createindex和dropindex insert locktables 锁表 process 使用showfullprocesslist reload 使用flush replicationclient 服务器位置访问 replocationslave 由复制从属使用 select showdatabases showview shutdown 使用mysqladminshutdown来关闭mysql super update usage 无访问权限

本文内容总结:

原文链接:https://www.cnblogs.com/fslnet/p/3143344.html