首页 > 编程知识 正文

mysql数据库用户权限管理(mysql 用户权限)

时间:2023-12-10 13:48:25 阅读:314106 作者:WCTB

本文目录一览:

如何给mysql用户分配权限

1,Mysql下创建新的用户

语法:

1.create user 用户名 identified by '密码';

例:create user xiaogang identified by '123456';

新创建的用户,默认情况下是没有任何权限的。

2. 如何给用户分配权限

语法:

1.grant 权限 on 数据库.数据表 to '用户' @ '主机名';

例:给 xiaogang 分配所有的权限

grant all on *.* to 'xiaogang'@'%';

这个时候 xiaogang 就拥有了 所有权限了

3 如何更精准的控制用户的权限呢?

1.grant 权限 on 数据库.数据表 to '用户' @ '主机名';

例:让 xiaogang 有查询 tmp 数据库 tmp1 表的权限;

grant select on temp.temp1 to 'xiaogang'@'%'; //这个时候 xiaogang 就具有查询temp小的temp1的权限了。

如何设置mysql用户的权限

1、创建新用户

通过root用户登录之后创建

grant all privileges on *.* to testuser@localhost identified by "123456" ;//创建新用户,用户名为testuser,密码为123456 ;

grant all privileges on *.* to testuser@localhost identified by "123456" ;//设置用户testuser,可以在本地访问mysql

grant all privileges on *.* to testuser@"%" identified by "123456" ; //设置用户testuser,可以在远程访问mysql

flush privileges ;//mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效

2、设置用户访问数据库权限

grant all privileges on test_db.* to testuser@localhost identified by "123456" ;//设置用户testuser,只能访问数据库test_db,其他数据库均不能访问 ;

grant all privileges on *.* to testuser@localhost identified by "123456" ;//设置用户testuser,可以访问mysql上的所有数据库 ;

grant all privileges on test_db.user_infor to testuser@localhost identified by "123456" ;//设置用户testuser,只能访问数据库test_db的表user_infor,数据库中的其他表均不能访问 ;

3、设置用户操作权限

grant all privileges on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION ;//设置用户testuser,拥有所有的操作权限,也就是管理员 ;

grant select on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION ;//设置用户testuser,只拥有【查询】操作权限 ;

grant select,insert on *.* to testuser@localhost identified by "123456" ;//设置用户testuser,只拥有【查询插入】操作权限 ;

grant select,insert,update,delete on *.* to testuser@localhost identified by "123456" ;//设置用户testuser,只拥有【查询插入】操作权限 ;

REVOKE select,insert ON what FROM testuser//取消用户testuser的【查询插入】操作权限 ;

MySQL的权限有哪些

MySQL各种权限(共27个)

(以下操作都是以root身份登陆进行grant授权,以p1@localhost身份登陆执行各种命令。)

1. usage

连接(登陆)权限,建立一个用户,就会自动授予其usage权限(默认授予)。

mysql grant usage on *.* to ‘p1′@’localhost’ identified by ‘123′;

该权限只能用于数据库登陆,不能执行任何操作;且usage权限不能被回收,也即REVOKE用户并不能删除用户。

2. select

必须有select的权限,才可以使用select table

mysql grant select on pyt.* to ‘p1′@’localhost’;

mysql select * from shop;

3. create

必须有create的权限,才可以使用create table

mysql grant create on pyt.* to ‘p1′@’localhost’;

4. create routine

必须具有create routine的权限,才可以使用{create |alter|drop} {procedure|function}

mysql grant create routine on pyt.* to ‘p1′@’localhost’;

当授予create routine时,自动授予EXECUTE, ALTER ROUTINE权限给它的创建者:

mysql show grants for ‘p1′@’localhost’;

+—————————————————————————+

Grants for p1@localhost

+————————————————————————–+

| GRANT USAGE ON *.* TO ‘p1′@’localhost’ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257′ |

| GRANT SELECT, CREATE, CREATE ROUTINE ON `pyt`.* TO ‘p1′@’localhost’|

| GRANT EXECUTE, ALTER ROUTINE ON PROCEDURE `pyt`.`pro_shop1` TO ‘p1′@’localhost’ |

+————————————————————————————-+

5. create temporary tables(注意这里是tables,不是table)

必须有create temporary tables的权限,才可以使用create temporary tables.

mysql grant create temporary tables on pyt.* to ‘p1′@’localhost’;

[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt

mysql create temporary table tt1(id int);

6. create view

必须有create view的权限,才可以使用create view

mysql grant create view on pyt.* to ‘p1′@’localhost’;

mysql create view v_shop as select price from shop;

7. create user

要使用CREATE USER,必须拥有mysql数据库的全局CREATE USER权限,或拥有INSERT权限。

mysql grant create user on *.* to ‘p1′@’localhost’;

或:mysql grant insert on *.* to p1@localhost;

8. insert

必须有insert的权限,才可以使用insert into ….. values….

9. alter

必须有alter的权限,才可以使用alter table

alter table shop modify dealer char(15);

10. alter routine

必须具有alter routine的权限,才可以使用{alter |drop} {procedure|function}

mysqlgrant alter routine on pyt.* to ‘p1′@’ localhost ‘;

mysql drop procedure pro_shop;

Query OK, 0 rows affected (0.00 sec)

mysql revoke alter routine on pyt.* from ‘p1′@’localhost’;

[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt

mysql drop procedure pro_shop;

ERROR 1370 (42000): alter routine command denied to user ‘p1′@’localhost’ for routine ‘pyt.pro_shop’

11. update

必须有update的权限,才可以使用update table

mysql update shop set price=3.5 where article=0001 and dealer=’A';

12. delete

必须有delete的权限,才可以使用delete from ….where….(删除表中的记录)

13. drop

必须有drop的权限,才可以使用drop database db_name; drop table tab_name;

drop view vi_name; drop index in_name;

14. show database

通过show database只能看到你拥有的某些权限的数据库,除非你拥有全局SHOW DATABASES权限。

对于p1@localhost用户来说,没有对mysql数据库的权限,所以以此身份登陆查询时,无法看到mysql数据库:

mysql show databases;

+——————–+

| Database |

+——————–+

| information_schema|

| pyt |

| test |

+——————–+

15. show view

必须拥有show view权限,才能执行show create view。

mysql grant show view on pyt.* to p1@localhost;

mysql show create view v_shop;

16. index

必须拥有index权限,才能执行[create |drop] index

mysql grant index on pyt.* to p1@localhost;

mysql create index ix_shop on shop(article);

mysql drop index ix_shop on shop;

17. excute

执行存在的Functions,Procedures

mysql call pro_shop1(0001,@a);

+———+

| article |

+———+

| 0001 |

| 0001 |

+———+

mysql select @a;

+——+

| @a |

+——+

| 2 |

+——+

18. lock tables

必须拥有lock tables权限,才可以使用lock tables

mysql grant lock tables on pyt.* to p1@localhost;

mysql lock tables a1 read;

mysql unlock tables;

19. references

有了REFERENCES权限,用户就可以将其它表的一个字段作为某一个表的外键约束。

20. reload

必须拥有reload权限,才可以执行flush [tables | logs | privileges]

mysql grant reload on pyt.* to p1@localhost;

ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

mysql grant reload on *.* to ‘p1′@’localhost’;

Query OK, 0 rows affected (0.00 sec)

mysql flush tables;

21. replication client

拥有此权限可以查询master server、slave server状态。

mysql show master status;

ERROR 1227 (42000): Access denied; you need the SUPER,REPLICATION CLIENT privilege for this operation

mysql grant Replication client on *.* to p1@localhost;

或:mysql grant super on *.* to p1@localhost;

mysql show master status;

+——————+———-+————–+——————+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+——————+———-+————–+——————+

| mysql-bin.000006 | 2111 | | |

+——————+———-+————–+——————+

mysql show slave status;

22. replication slave

拥有此权限可以查看从服务器,从主服务器读取二进制日志。

mysql show slave hosts;

ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation

mysql show binlog events;

ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation

mysql grant replication slave on *.* to p1@localhost;

mysql show slave hosts;

Empty set (0.00 sec)

mysqlshow binlog events;

+—————+——-+—————-+———–+————-+————–+

| Log_name | Pos | Event_type | Server_id| End_log_pos|Info | +—————+——-+————–+———–+————-+—————+

| mysql-bin.000005 | 4 | Format_desc | 1 | 98 | Server ver: 5.0.77-log, Binlog ver: 4 | |mysql-bin.000005|98|Query|1|197|use `mysql`; create table a1(i int)engine=myisam|

……………………………………

23. Shutdown

关闭MySQL:

[mysql@mydev ~]$ mysqladmin shutdown

重新连接:

[mysql@mydev ~]$ mysql

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

[mysql@mydev ~]$ cd /u01/mysql/bin

[mysql@mydev bin]$ ./mysqld_safe

[mysql@mydev bin]$ mysql

24. grant option

拥有grant option,就可以将自己拥有的权限授予其他用户(仅限于自己已经拥有的权限)

mysql grant Grant option on pyt.* to p1@localhost;

mysql grant select on pyt.* to p2@localhost;

25. file

拥有file权限才可以执行 select ..into outfile和load data infile…操作,但是不要把file, process, super权限授予管理员以外的账号,这样存在严重的安全隐患。

mysql grant file on *.* to p1@localhost;

mysql load data infile ‘/home/mysql/pet.txt’ into table pet;

26. super

这个权限允许用户终止任何查询;修改全局变量的SET语句;使用CHANGE MASTER,PURGE MASTER LOGS。

mysql grant super on *.* to p1@localhost;

mysql purge master logs before ‘mysql-bin.000006′;

27. process

通过这个权限,用户可以执行SHOW PROCESSLIST和KILL命令。默认情况下,每个用户都可以执行SHOW PROCESSLIST命令,但是只能查询本用户的进程。

mysql show processlist;

+—-+——+———–+——+———+——+——-+——————+

| Id | User | Host | db | Command | Time | State | Info |

+—-+——+———–+——+———+——+——-+——————+

| 12 | p1 | localhost | pyt | Query | 0 | NULL | show processlist |

+—-+——+———–+——+———+——+——-+——————+

另外,

管理权限(如 super, process, file等)不能够指定某个数据库,on后面必须跟*.*

mysql grant super on pyt.* to p1@localhost;

ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

mysql grant super on *.* to p1@localhost;

Query OK, 0 rows affected (0.01 sec)

mysql怎么将grant priv的权限

grant 权限 on 数据库对象 to 用户

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

grant select on testdb.* to common_user@’%’

grant insert on testdb.* to common_user@’%’

grant update on testdb.* to common_user@’%’

grant delete on testdb.* to common_user@'%'

或者

grant select, insert, update, delete on testdb.* to common_user@’%’

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

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

grant create on testdb.* to developer@’192.168.0.%’;

grant alter on testdb.* to developer@’192.168.0.%’;

grant drop on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 外键权限。

grant references on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 索引权限。

grant index on testdb.* to developer@’192.168.0.%’;

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

grant create view on testdb.* to developer@’192.168.0.%’;

grant show view on testdb.* to developer@’192.168.0.%’;

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

grant create routine on testdb.* to developer@’192.168.0.%’; — now, can show procedure status

grant alter routine on testdb.* to developer@’192.168.0.%’; — now, you can drop a procedure

grant execute on testdb.* to developer@’192.168.0.%’;

三、grant 普通 DBA 管理某个 MySQL 数据库的权限。

grant all privileges on testdb to dba@’localhost’

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

四、grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to dba@’localhost’

五、MySQL grant 权限,分别可以作用在多个层次上。

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

grant select on *.* to dba@localhost; — dba 可以查询 MySQL 中所有数据库中的表。

grant all on *.* to dba@localhost; — dba 可以管理 MySQL 中的所有数据库

2. grant 作用在单个数据库上:

grant select on testdb.* to dba@localhost; — dba 可以查询 testdb 中的表。

3. grant 作用在单个数据表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;

这里在给一个用户授权多张表时,可以多次执行以上语句。例如:

grant select(user_id,username) on smp.users to mo_user@’%’ identified by ‘123345′;

grant select on smp.mo_sms to mo_user@’%’ identified by ‘123345′;

4. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost;

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

grant execute on procedure testdb.pr_add to ‘dba’@'localhost’

grant execute on function testdb.fn_add to ‘dba’@'localhost’

六、查看 MySQL 用户权限

查看当前用户(自己)权限:

show grants;

查看其他 MySQL 用户权限:

show grants for zhangkh@localhost;

七、撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

grant all on *.* to dba@localhost;

revoke all on *.* from dba@localhost;

八、MySQL grant、revoke 用户权限注意事项

1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

grant select on testdb.* to dba@localhost with grant option;

这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

mysql授权表共有5个表:user、db、host、tables_priv和columns_priv。

授权表的内容有如下用途:

user表

user表列出可以连接服务器的用户及其口令,并且它指定他们有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。例如,如果你启用了DELETE权限,在这里列出的用户可以从任何表中删除记录,所以在你这样做之前要认真考虑。

db表

db表列出数据库,而用户有权限访问它们。在这里指定的权限适用于一个数据库中的所有表。

host表

host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限,这可能比单独使用db好些。这个表不受GRANT和REVOKE语句的影响,所以,你可能发觉你根本不是用它。

tables_priv表

tables_priv表指定表级权限,在这里指定的一个权限适用于一个表的所有列。

columns_priv表

columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列。

注:

对于GRANT USAGE ON,查看手册有如下介绍和实例:

mysql GRANT USAGE ON *.* TO ‘zhangkh’@'localhost’;

一个账户有用户名zhangkh,没有密码。该账户只用于从本机连接。未授予权限。通过GRANT语句中的USAGE权限,你可以创建账户而不授予任何权限。它可以将所有全局权限设为’N'。假定你将在以后将具体权限授予该账户。

如何用phpmyadmin设置mysql数据库用户的权限

安装好以后,来到 phpMyAdmin 的首页如下图所示:

二、创建用户(如何您之前已经创建好用户,可以省略这一步)

点击上图所示的权限后,可以看到如下图所示:

点击“添加新用户”这个连接,添加一个新的数据库用户名

如下图所示:

三、权限设置

如下图所示:

编辑 hellen 这个用户的权限

如下图所示:

其中增加用户的同时也可以填写用户的权限,也可以添加完用户以后再编辑用户的权限

下面对这些权限进行详细的描述:

1、数据部分

SELECT:是指允许读取数据

INSERT:是指允许插入和替换数据

UPDATE:是指允许更改数据

DELETE:是指允许删除数据

FILE:是指允许从数据中导入数据,以及允许将数据导出至文件

2、结构部分

CTEATE:允许创建新的数据库和表

ALTER:允许修改现有表的结构

INDEX:允许创建和删除索引

DROP:允许删除数据库和表

CREATE TEMPORARY TABLES:允许创建暂时表

CREATE VIEW:允许创建新的视图

SHOW VIEW:允许查询试图

CREATE ROUTINE:允许创建新的存储过程

ALTER ROUTINE:允许修改存储过程

EXECUTE:允许执行查询

管理和资源限制这里不详细说明了,一般是管理员 root 帐号全部有的权限,上面所讲的是普通的用户权限。

四、重新加载授权表

修改完用户权限以后需要回到 phpMyAdmin 的首页,重新加载一下授权

如下图所示:

只要了解用户的权限设置,相信您对数据库的管理操作一定会得心应手了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。