Mysql之DCL数据控制语言

Mysql

DCL数据控制语言

数据控制语言(DCL:Data Control Language)是用来设置或者更改数据库用户或角色权限的语句,这些语句包括GRANT、DENY、REVOKE等语句

限制root用户指定ip登录

查看root用户可以在哪台机器登录

select user,host from mysql.user where user='root';

+------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | localhost |
+------+-----------+

修改mysql库里边的user表

 update mysql.user set host='localhost' where user='root';

刷新权限

flush privileges;
用户密码

修改用户密码分三种方法:

  1. 第一种
// set password for 用户@ip = password('密码');
set password for root@localhost = password('root');
  1. 第二种
// mysqladmin -u用户 -p旧密码 password 新密码;
mysqladmin -urootmysqladmin -uroot -proot password;
  1. 第三种
update mysql.user set authentication_string=password('root') where user='root' and host='localhost';

忘记密码

  1. 第一步

修改配置文件my.cnf (默认在/etc/my.cnf),在[mysqld]下面加上 skip-grant-tables (跳过权限的意思)

  1. 第二步

重启mysql服务

  1. 第三步

mysql -uroot -p 无需密码登录进入

  1. 第四步

修改密码

创建新用户并限制ip网段登录

创建用户的语法

create user 'username'@'host' identified by 'password';

username:你将创建的用户名

host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机 登陆,可以使用通配符%

password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

创建用户语法

创建一个mitday用户,并指定登录密码:123456,可以在任何一台远程主机都可以登录

create user 'mitday'@'%' identified by '123456';

创建一个mitday用户,并指定登录密码:为空,指定在120网段的机器登录

create user 'mitday'@'120.%.%.%' identified by '';

查看权限

select * from mysql.user where user='mitday';
 
show grants for 'mitday'@'%';

/* USAGE:无权限的意思 */
+------------------------------------+
| Grants for mitday@%                |
+------------------------------------+
| GRANT USAGE ON *.* TO 'mitday'@'%' |
+------------------------------------+



show grants for 'root'@'localhost';

/* WITH GRANT OPTION:表示这个用户拥有grant权限,即可以对其他用户授权 */
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+

删除用户语法

// drop user 'username'@'host';
drop user 'mitday'@'%';
delete from mysql.user where user = 'mitday';
库表权限授权与回收
授权语法
grant 权限1,权限2..... on 数据库对象 to '用户'@'host' identified by 'password';

对现有用户进行授权:对现有用户mitday授予所有库所有表所有权限

grant all privileges on *.*  to 'mitday';
  1. all privileges:代表所有权限
  2. . :代表所有库所有表

对没有的用户进行授权:创建一个新用户chairman授予student库的所有权限,登录密码123456,任何一台主机登录

grant all privileges on student.* to 'chairman'@'%' identified by '123456';

对没有的用户进行授权:创建一个新用户chairman授予student库的study表 查与修改权限,登录密码123456,任何一台主机登录

grant select,update on student.study to 'chairman'@'%' identified by '123456'

对没有的用户进行授权:对用户chairman授予student库的study表insert 权限,登录密码123456,任何一台主机登录

grant insert on student.study to 'chairman'@'%' identified by '123456';
回收语法
revoke 权限1,权限2..... on 数据库对象 from '用户'@'host';

回收mitday用户的所有权限(注意:并没有回收它的登录权限)

revoke all privileges on *.*  from 'mitday' @ '%';

flush privileges;

回收pig用户的所有权限(并回收它的登录权限)

delete from mysql.user where user='pig';

flush privileges;

回收chairman用户对student库的study的查与修改权限

revoke select,update on student.study from 'chairman'@'%';

flush privileges;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值