在mysql中,用户是怎么定义的?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#在MySQL中,一个完整的用户是: '用户'@'主机域' 1.用户名定义方法: 1)如果是数字为用户名,需要加引号 mysql> create user '123'@'locahost'; 2)如果是字符为用户名,不需要加引号 mysql> create user lhd@'locahost'; 2.主机域的定义方式: 1)localhost 2)127.0.0.1 3)db01 4)::1 5)% 6)10.0.0.% (10.0.0.1-255) 7)10.0.%.% 8)10.%.%.% 9)10.0.0.5% (10.0.0.50-59) 10)10.0.0.0/255.255.255.0 #可以设置,但是不能使用该用户连接 11)10.0.0.0/24 |
1.用户管理
创建用户并设置密码
1 2 |
mysql> create user lhd@'172.16.1.%' identified by '123'; Query OK, 0 rows affected (0.02 sec) |
查看用户
1 2 3 4 5 6 7 8 9 10 |
mysql> select user,host from mysql.user; +--------+------------------------+ | user | host | +--------+------------------------+ | qiudao | 10.0.0.0/24 | | lhd | 10.0.0.0/255.255.255.0 | | lhd | 172.16.1.% | | root | localhost | +--------+------------------------+ 4 rows in set (0.00 sec) |
删除用户
1 2 |
mysql> drop user qiudao@'10.0.0.0/24'; Query OK, 0 rows affected (0.00 sec) |
修改用户密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#1.方式一:命令行修改密码 [root@db02 /service]# mysqladmin -uroot -p password Enter password: 123456 New password: 123 Confirm new password: 123 #2.方式二:授权的方式修改密码 mysql> grant all on *.* to lhd@'10.0.0.0/255.255.255.0' identified by '123456'; Query OK, 0 rows affected (0.00 sec) #3.方式三:更新数据库密码 mysql> update mysql.user set password=PASSWORD('123456') where user='lhd' and host='172.16.1.%'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 #4.方式四:直接设置密码 mysql> set password=PASSWORD('123456'); Query OK, 0 rows affected (0.00 sec) |
2.权限管理
权限管理的命令
1 2 3 4 5 6 7 8 9 10 11 12 |
mysql> grant all on *.* to root@'172.16.1.51' identified by '123'; grant all on *.* to root@'172.16.1.51' identified by '123'; grant #授权命令 all #所有权限 on #在。。。上 *.* #所有库.(包含)所有表 to #给。。 root@'172.16.1.51' #定义的用户 '用户名'@'主机域' identified #设置密码 by #是 '123'; #密码 |
查看所有权限
1 2 3 4 5 6 7 8 9 |
#1.回收权限 mysql> revoke select on *.* from root@'172.16.1.51'; Query OK, 0 rows affected (0.00 sec) #2.查看用户的权限 mysql> show grants for root@'172.16.1.51'; #3.所有权限包括: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, GRANT |
3.授权的对象
作用对象
1 2 3 4 5 6 7 8 9 10 11 |
grant all on *.* to root@'172.16.1.%' identified by '123'; #所有库下的所有表 grant all on mysql.* to root@'172.16.1.%' identified by '123'; #mysql库下的所有表,单库授权 grant all on mysql.user to root@'172.16.1.%' identified by '123'; #单表授权 #单列授权,企业称之为脱敏,脱敏:脱离敏感信息 1.普通用户只能查看用户名字 mysql> grant select(user) on mysql.user to diaosi@'172.16.1.%' identified by '123'; 2.VIP用户可以查看用户用户较多的信息 mysql> grant select(user,host) on mysql.user to diaosi@'172.16.1.%' identified by '123'; 3.超级VIP可以查看用户所有的信息 mysql> grant select on mysql.user to diaosi@'172.16.1.%' identified by '123'; |
企业里一般我们如何给权限?
1 2 3 4 5 6 7 8 9 10 11 12 |
#开发要一个用户登录数据库 1.跟开发沟通: 1)你要操作的是哪个库哪个表 2)你要对这个表进行哪些操作 3)你从哪个IP连过来 4)你对用户名的要求 5)你对密码有什么要求 6)你的用户要用多久 7)发邮件 2.一般怎么授权 grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456'; 3.开发要root用户怎么办? |
4.权限实践
准备数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#创建wordpress数据库 create database wordpress; #使用wordpress库 use wordpress; #创建t1、t2表 create table t1 (id int); create table t2 (id int); #创建blog库 create database blog; #使用blog库 use blog; #创建t1表 create table tb1 (id int); |
授权
1 2 3 4 5 6 7 8 9 10 |
#授权wordpress@'10.0.0.5%'这个用户对所有库所有表有查询权限,密码是123 #授权wordpress可以通过10.0.0.5%这个网段连接数据库对所有库所有表有查询权限,密码是123 1.grant select on *.* to wordpress@'10.0.0.5%' identified by '123'; #授权wordpress@'10.0.0.5%'对wordpress库下的所有表有增删改的权限,密码是123 #授权wordpress可以通过10.0.0.5%这个网段连接数据库对wordpress库下的所有表有增删改的权限,密码是123 2.grant insert,delete,update on wordpress.* to wordpress@'10.0.0.5%' identified by '123'; #授权wordpress@'10.0.0.5%'对wordpress下面的t1表有所有权限,密码是123 3.grant all on wordpress.t1 to wordpress@'10.0.0.5%' identified by '123'; |
请问
1 2 3 4 |
一个客户端程序使用wordpress用户登陆到10.0.0.51的MySQL后, 1.对t1表的管理能力? #所有权限 2.对t2表的管理能力? #增删改查 3.对tb1表的管理能力? #只有查 |
授权总结
1 2 3 4 5 6 7 |
1.不同级别授权,权限是相加关系 2.不推荐在多级别定义重复的权限 3.常见的授权方式是单库授权 grant select,insert,update on mysql.* to dev@'172.16.1.51' identified by 'Dev@123456'; 4.如果涉及到用户的敏感信息,可以使用脱敏,单列授权 5.查看用户的权限 mysql> show grants for root@'172.16.1.%'; |