mysql--用户管理

1.用户定义-用户构成

用户名+主机域唯一指定一个用户

2.用户作用

用户登录-连接数据库
管理数据库及数据

3.创建用户

定义用户名
    用户名+主机域
    定义密码
权限
    对不同的对象进行权限或角色定义
-- 一次性创建用户并授权
    grant 权限 on 权限范围(对象) to 用户  identified by '密码'
    grant all on *.* to 'anyux'@'10.0.0.52' identified by 'anyux'
4.角色
--单个权限
select
update
insert
drop
create
---权限集合 all (所有权限,除授权权限外
replication slave (从库权限
5.范围
*.*
    所有数据库对象
test.*
    所有test数据库对象
test.test
    test数据库下的test表

6.用户

'anyux'@'localhost'
指定本地连接
'anyux'@'10.0.0.53'
指定ip段
'anyux'@'10.0.0.%'
指定一个网段的ip
'anyux'@'10.0.0.5%'
指定限定ip
'anyux'@'%'
指定所有网段可以通过anyux用户名连接数据库危险
危险
'anyux'@'10.0.0.1__'
指定连接范围为100-199,_(下划线)表示匹配一个字符
指定的是客户端的连接
要求:
1.用户只能过10.网段访问,用户名为oldboy密码为123,
2.用户只能对oldboy下的对象进行增、删、改、查
grant select,insert,update,create on oldboy.* to 'oldboy'@'10.0.0.%' identified by '123'
    查看指定用户权限
查看用户权限
show grants for 'oldboy'@'10.0.0.%';

7.删除用户

--方法1#(推荐)
drop user 'oldboy'@'10.0.0.%'
--方法2
delete from mysql.user where user='oldboy' and 'host'='10.0.0.%'
flush privileges
--这种方式删除需要使用

  --创建准系统超级管理员
  grant all on *.* to 'sys'@'localhost' identified by '123'

 

8.找回密码

--skip-grant-talbes
启动跳过授权表
--skip-networking
禁止网络连接

--启动数据库服务--以无密码形式连接
/application/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking  &
--此情况下与授权有关的命令无法执行
grant
revoke
drop user
create user
--使用DML修改5.6数据库 使用password作为密码字段
--修改密码
update mysql.user set password=password('123456') where user='sys' and 'host' = 'localhost'
--更新表
flush privileges;
--使用DML修改5.7数据库
--唯一的区别是列字段变化了 authentication_string 作为密码字段desc mysql.user;
mysql.user表字段修改,可以点击这里,查看5.7的用户表字段

9.注意事项

    不要在多个级别上授权
    mysql中权限最终是通过综合-来获得最大的权限
    只使用一个授权即可

10.权限回收

回收权限-revoke
revoke 权限 on 范围 to 用户 
revoke drop,delete on *.* to 'sys'@'localhost'
原文地址:https://www.cnblogs.com/anyux/p/8127322.html